You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18n.rb 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module Redmine
  18. module I18n
  19. def self.included(base)
  20. base.extend Redmine::I18n
  21. end
  22. def l(*args)
  23. case args.size
  24. when 1
  25. ::I18n.t(*args)
  26. when 2
  27. if args.last.is_a?(Hash)
  28. ::I18n.t(*args)
  29. elsif args.last.is_a?(String)
  30. ::I18n.t(args.first, :value => args.last)
  31. else
  32. ::I18n.t(args.first, :count => args.last)
  33. end
  34. else
  35. raise "Translation string with multiple values: #{args.first}"
  36. end
  37. end
  38. def l_or_humanize(s, options={})
  39. k = "#{options[:prefix]}#{s}".to_sym
  40. ::I18n.t(k, :default => s.to_s.humanize)
  41. end
  42. def l_hours(hours)
  43. hours = hours.to_f
  44. l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
  45. end
  46. def l_hours_short(hours)
  47. l(:label_f_hour_short, :value => ("%.2f h" % hours.to_f))
  48. end
  49. def ll(lang, str, arg=nil)
  50. options = arg.is_a?(Hash) ? arg : {:value => arg}
  51. locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }
  52. ::I18n.t(str.to_s, options.merge(:locale => locale))
  53. end
  54. # Localizes the given args with user's language
  55. def lu(user, *args)
  56. lang = user.try(:language).presence || Setting.default_language
  57. ll(lang, *args)
  58. end
  59. def format_date(date)
  60. return nil unless date
  61. options = {}
  62. options[:format] = Setting.date_format unless Setting.date_format.blank?
  63. ::I18n.l(date.to_date, options)
  64. end
  65. def format_time(time, include_date = true)
  66. return nil unless time
  67. options = {}
  68. options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
  69. time = time.to_time if time.is_a?(String)
  70. zone = User.current.time_zone
  71. local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
  72. (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
  73. end
  74. def day_name(day)
  75. ::I18n.t('date.day_names')[day % 7]
  76. end
  77. def day_letter(day)
  78. ::I18n.t('date.abbr_day_names')[day % 7].first
  79. end
  80. def month_name(month)
  81. ::I18n.t('date.month_names')[month]
  82. end
  83. def valid_languages
  84. ::I18n.available_locales
  85. end
  86. # Returns an array of languages names and code sorted by names, example:
  87. # [["Deutsch", "de"], ["English", "en"] ...]
  88. #
  89. # The result is cached to prevent from loading all translations files
  90. # unless :cache => false option is given
  91. def languages_options(options={})
  92. options = if options[:cache] == false
  93. valid_languages.
  94. select {|locale| ::I18n.exists?(:general_lang_name, locale)}.
  95. map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
  96. sort {|x,y| x.first <=> y.first }
  97. else
  98. ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
  99. languages_options :cache => false
  100. end
  101. end
  102. options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
  103. end
  104. def find_language(lang)
  105. @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
  106. @@languages_lookup[lang.to_s.downcase]
  107. end
  108. def set_language_if_valid(lang)
  109. if l = find_language(lang)
  110. ::I18n.locale = l
  111. end
  112. end
  113. def current_language
  114. ::I18n.locale
  115. end
  116. # Custom backend based on I18n::Backend::Simple with the following changes:
  117. # * lazy loading of translation files
  118. # * available_locales are determined by looking at translation file names
  119. class Backend
  120. (class << self; self; end).class_eval { public :include }
  121. module Implementation
  122. include ::I18n::Backend::Base
  123. # Stores translations for the given locale in memory.
  124. # This uses a deep merge for the translations hash, so existing
  125. # translations will be overwritten by new ones only at the deepest
  126. # level of the hash.
  127. def store_translations(locale, data, options = {})
  128. locale = locale.to_sym
  129. translations[locale] ||= {}
  130. data = data.deep_symbolize_keys
  131. translations[locale].deep_merge!(data)
  132. end
  133. # Get available locales from the translations filenames
  134. def available_locales
  135. @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
  136. end
  137. # Clean up translations
  138. def reload!
  139. @translations = nil
  140. @available_locales = nil
  141. super
  142. end
  143. protected
  144. def init_translations(locale)
  145. locale = locale.to_s
  146. paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
  147. load_translations(paths)
  148. translations[locale] ||= {}
  149. end
  150. def translations
  151. @translations ||= {}
  152. end
  153. # Looks up a translation from the translations hash. Returns nil if
  154. # eiher key is nil, or locale, scope or key do not exist as a key in the
  155. # nested translations hash. Splits keys or scopes containing dots
  156. # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
  157. # <tt>%w(currency format)</tt>.
  158. def lookup(locale, key, scope = [], options = {})
  159. init_translations(locale) unless translations.key?(locale)
  160. keys = ::I18n.normalize_keys(locale, key, scope, options[:separator])
  161. keys.inject(translations) do |result, _key|
  162. _key = _key.to_sym
  163. return nil unless result.is_a?(Hash) && result.has_key?(_key)
  164. result = result[_key]
  165. result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
  166. result
  167. end
  168. end
  169. end
  170. include Implementation
  171. # Adds fallback to default locale for untranslated strings
  172. include ::I18n::Backend::Fallbacks
  173. end
  174. end
  175. end