i18n.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. require 'i18n/version'
  2. require 'i18n/exceptions'
  3. require 'i18n/interpolate/ruby'
  4. module I18n
  5. autoload :Backend, 'i18n/backend'
  6. autoload :Config, 'i18n/config'
  7. autoload :Gettext, 'i18n/gettext'
  8. autoload :Locale, 'i18n/locale'
  9. autoload :Tests, 'i18n/tests'
  10. RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :rescue_format]
  11. RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/
  12. class << self
  13. # Gets I18n configuration object.
  14. def config
  15. Thread.current[:i18n_config] ||= I18n::Config.new
  16. end
  17. # Sets I18n configuration object.
  18. def config=(value)
  19. Thread.current[:i18n_config] = value
  20. end
  21. # Write methods which delegates to the configuration object
  22. %w(locale backend default_locale available_locales default_separator
  23. exception_handler load_path).each do |method|
  24. module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
  25. def #{method}
  26. config.#{method}
  27. end
  28. def #{method}=(value)
  29. config.#{method} = (value)
  30. end
  31. DELEGATORS
  32. end
  33. # Tells the backend to reload translations. Used in situations like the
  34. # Rails development environment. Backends can implement whatever strategy
  35. # is useful.
  36. def reload!
  37. config.backend.reload!
  38. end
  39. # Translates, pluralizes and interpolates a given key using a given locale,
  40. # scope, and default, as well as interpolation values.
  41. #
  42. # *LOOKUP*
  43. #
  44. # Translation data is organized as a nested hash using the upper-level keys
  45. # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
  46. # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
  47. #
  48. # Translations can be looked up at any level of this hash using the key argument
  49. # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
  50. # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
  51. #
  52. # Key can be either a single key or a dot-separated key (both Strings and Symbols
  53. # work). <em>E.g.</em>, the short format can be looked up using both:
  54. # I18n.t 'date.formats.short'
  55. # I18n.t :'date.formats.short'
  56. #
  57. # Scope can be either a single key, a dot-separated key or an array of keys
  58. # or dot-separated keys. Keys and scopes can be combined freely. So these
  59. # examples will all look up the same short date format:
  60. # I18n.t 'date.formats.short'
  61. # I18n.t 'formats.short', :scope => 'date'
  62. # I18n.t 'short', :scope => 'date.formats'
  63. # I18n.t 'short', :scope => %w(date formats)
  64. #
  65. # *INTERPOLATION*
  66. #
  67. # Translations can contain interpolation variables which will be replaced by
  68. # values passed to #translate as part of the options hash, with the keys matching
  69. # the interpolation variable names.
  70. #
  71. # <em>E.g.</em>, with a translation <tt>:foo => "foo %{bar}"</tt> the option
  72. # value for the key +bar+ will be interpolated into the translation:
  73. # I18n.t :foo, :bar => 'baz' # => 'foo baz'
  74. #
  75. # *PLURALIZATION*
  76. #
  77. # Translation data can contain pluralized translations. Pluralized translations
  78. # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
  79. #
  80. # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
  81. # pluralization rules. Other algorithms can be supported by custom backends.
  82. #
  83. # This returns the singular version of a pluralized translation:
  84. # I18n.t :foo, :count => 1 # => 'Foo'
  85. #
  86. # These both return the plural version of a pluralized translation:
  87. # I18n.t :foo, :count => 0 # => 'Foos'
  88. # I18n.t :foo, :count => 2 # => 'Foos'
  89. #
  90. # The <tt>:count</tt> option can be used both for pluralization and interpolation.
  91. # <em>E.g.</em>, with the translation
  92. # <tt>:foo => ['%{count} foo', '%{count} foos']</tt>, count will
  93. # be interpolated to the pluralized translation:
  94. # I18n.t :foo, :count => 1 # => '1 foo'
  95. #
  96. # *DEFAULTS*
  97. #
  98. # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
  99. # I18n.t :foo, :default => 'default'
  100. #
  101. # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
  102. # translation for <tt>:foo</tt> was found:
  103. # I18n.t :foo, :default => :bar
  104. #
  105. # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
  106. # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
  107. # I18n.t :foo, :default => [:bar, 'default']
  108. #
  109. # *BULK LOOKUP*
  110. #
  111. # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
  112. # I18n.t [:foo, :bar]
  113. #
  114. # Can be used with dot-separated nested keys:
  115. # I18n.t [:'baz.foo', :'baz.bar']
  116. #
  117. # Which is the same as using a scope option:
  118. # I18n.t [:foo, :bar], :scope => :baz
  119. #
  120. # *LAMBDAS*
  121. #
  122. # Both translations and defaults can be given as Ruby lambdas. Lambdas will be
  123. # called and passed the key and options.
  124. #
  125. # E.g. assuming the key <tt>:salutation</tt> resolves to:
  126. # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
  127. #
  128. # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
  129. #
  130. # It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
  131. # a cache layer is put in front of I18n.translate it will generate a cache key
  132. # from the argument values passed to #translate. Therefor your lambdas should
  133. # always return the same translations/values per unique combination of argument
  134. # values.
  135. def translate(*args)
  136. options = args.last.is_a?(Hash) ? args.pop : {}
  137. key = args.shift
  138. backend = config.backend
  139. locale = options.delete(:locale) || config.locale
  140. handling = options.delete(:throw) && :throw || options.delete(:raise) && :raise # TODO deprecate :raise
  141. raise I18n::ArgumentError if key.is_a?(String) && key.empty?
  142. result = catch(:exception) do
  143. if key.is_a?(Array)
  144. key.map { |k| backend.translate(locale, k, options) }
  145. else
  146. backend.translate(locale, key, options)
  147. end
  148. end
  149. result.is_a?(MissingTranslation) ? handle_exception(handling, result, locale, key, options) : result
  150. end
  151. alias :t :translate
  152. def translate!(key, options={})
  153. translate(key, options.merge(:raise => true))
  154. end
  155. alias :t! :translate!
  156. # Transliterates UTF-8 characters to ASCII. By default this method will
  157. # transliterate only Latin strings to an ASCII approximation:
  158. #
  159. # I18n.transliterate("Ærøskøbing")
  160. # # => "AEroskobing"
  161. #
  162. # I18n.transliterate("日本語")
  163. # # => "???"
  164. #
  165. # It's also possible to add support for per-locale transliterations. I18n
  166. # expects transliteration rules to be stored at
  167. # <tt>i18n.transliterate.rule</tt>.
  168. #
  169. # Transliteration rules can either be a Hash or a Proc. Procs must accept a
  170. # single string argument. Hash rules inherit the default transliteration
  171. # rules, while Procs do not.
  172. #
  173. # *Examples*
  174. #
  175. # Setting a Hash in <locale>.yml:
  176. #
  177. # i18n:
  178. # transliterate:
  179. # rule:
  180. # ü: "ue"
  181. # ö: "oe"
  182. #
  183. # Setting a Hash using Ruby:
  184. #
  185. # store_translations(:de, :i18n => {
  186. # :transliterate => {
  187. # :rule => {
  188. # "ü" => "ue",
  189. # "ö" => "oe"
  190. # }
  191. # }
  192. # )
  193. #
  194. # Setting a Proc:
  195. #
  196. # translit = lambda {|string| MyTransliterator.transliterate(string) }
  197. # store_translations(:xx, :i18n => {:transliterate => {:rule => translit})
  198. #
  199. # Transliterating strings:
  200. #
  201. # I18n.locale = :en
  202. # I18n.transliterate("Jürgen") # => "Jurgen"
  203. # I18n.locale = :de
  204. # I18n.transliterate("Jürgen") # => "Juergen"
  205. # I18n.transliterate("Jürgen", :locale => :en) # => "Jurgen"
  206. # I18n.transliterate("Jürgen", :locale => :de) # => "Juergen"
  207. def transliterate(*args)
  208. options = args.pop if args.last.is_a?(Hash)
  209. key = args.shift
  210. locale = options && options.delete(:locale) || config.locale
  211. raises = options && options.delete(:raise)
  212. replacement = options && options.delete(:replacement)
  213. config.backend.transliterate(locale, key, replacement)
  214. rescue I18n::ArgumentError => exception
  215. raise exception if raises
  216. handle_exception(exception, locale, key, options)
  217. end
  218. # Localizes certain objects, such as dates and numbers to local formatting.
  219. def localize(object, options = {})
  220. locale = options.delete(:locale) || config.locale
  221. format = options.delete(:format) || :default
  222. config.backend.localize(locale, object, format, options)
  223. end
  224. alias :l :localize
  225. # Executes block with given I18n.locale set.
  226. def with_locale(tmp_locale = nil)
  227. if tmp_locale
  228. current_locale = self.locale
  229. self.locale = tmp_locale
  230. end
  231. yield
  232. ensure
  233. self.locale = current_locale if tmp_locale
  234. end
  235. # Merges the given locale, key and scope into a single array of keys.
  236. # Splits keys that contain dots into multiple keys. Makes sure all
  237. # keys are Symbols.
  238. def normalize_keys(locale, key, scope, separator = nil)
  239. separator ||= I18n.default_separator
  240. keys = []
  241. keys.concat normalize_key(locale, separator)
  242. keys.concat normalize_key(scope, separator)
  243. keys.concat normalize_key(key, separator)
  244. keys
  245. end
  246. # making these private until Ruby 1.9.2 can send to protected methods again
  247. # see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
  248. private
  249. # Any exceptions thrown in translate will be sent to the @@exception_handler
  250. # which can be a Symbol, a Proc or any other Object unless they're forced to
  251. # be raised or thrown (MissingTranslation).
  252. #
  253. # If exception_handler is a Symbol then it will simply be sent to I18n as
  254. # a method call. A Proc will simply be called. In any other case the
  255. # method #call will be called on the exception_handler object.
  256. #
  257. # Examples:
  258. #
  259. # I18n.exception_handler = :default_exception_handler # this is the default
  260. # I18n.default_exception_handler(exception, locale, key, options) # will be called like this
  261. #
  262. # I18n.exception_handler = lambda { |*args| ... } # a lambda
  263. # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
  264. #
  265. # I18n.exception_handler = I18nExceptionHandler.new # an object
  266. # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
  267. def handle_exception(handling, exception, locale, key, options)
  268. case handling
  269. when :raise
  270. raise(exception.respond_to?(:to_exception) ? exception.to_exception : exception)
  271. when :throw
  272. throw :exception, exception
  273. else
  274. case handler = options[:exception_handler] || config.exception_handler
  275. when Symbol
  276. send(handler, exception, locale, key, options)
  277. else
  278. handler.call(exception, locale, key, options)
  279. end
  280. end
  281. end
  282. def normalize_key(key, separator)
  283. normalized_key_cache[separator][key] ||=
  284. case key
  285. when Array
  286. key.map { |k| normalize_key(k, separator) }.flatten
  287. else
  288. keys = key.to_s.split(separator)
  289. keys.delete('')
  290. keys.map! { |k| k.to_sym }
  291. keys
  292. end
  293. end
  294. def normalized_key_cache
  295. @normalized_key_cache ||= Hash.new { |h,k| h[k] = {} }
  296. end
  297. # DEPRECATED. Use I18n.normalize_keys instead.
  298. def normalize_translation_keys(locale, key, scope, separator = nil)
  299. puts "I18n.normalize_translation_keys is deprecated. Please use the class I18n.normalize_keys instead."
  300. normalize_keys(locale, key, scope, separator)
  301. end
  302. # DEPRECATED. Please use the I18n::ExceptionHandler class instead.
  303. def default_exception_handler(exception, locale, key, options)
  304. puts "I18n.default_exception_handler is deprecated. Please use the class I18n::ExceptionHandler instead " +
  305. "(an instance of which is set to I18n.exception_handler by default)."
  306. exception.is_a?(MissingTranslation) ? exception.message : raise(exception)
  307. end
  308. end
  309. end