lib/i18n_extensions.rb in instant18n-0.3.0 vs lib/i18n_extensions.rb in instant18n-0.4.0
- old
+ new
@@ -9,11 +9,13 @@
class << self
attr_accessor :default_language
end
- self.default_language = "English"
+ ##
+ # Override in case of wanting to leave the locale "as is" but using a different language
+ self.default_language = nil
##
# This method, `it`, instantly translates a given text based on the provided language code.
# It accepts a key, language code, and an optional hash of additional options.
#
@@ -38,13 +40,14 @@
# specified css classes (in addition to `instant18n` and the language supplied, i.e. `espanol`).
# Note that the language will be parameterized, so that `EspaƱol` becomes `espanol`.
#
# @return [String] the translated text.
##
- def self.it(key, lang, **opts)
+ def self.it(key, lang=nil, **opts)
+ lang = (lang || default_language || I18n.default_locale || "en").to_s
opts && opts.symbolize_keys!
Rails.cache.fetch(cache_key(key, lang, opts), expires_in: (opts[:expires_in] || 1.year) , force: (opts[:force] || false)) do
- if lang.casecmp(default_language).zero?
+ if lang.casecmp(default_language.to_s || I18n.default_locale.to_s || "en").zero?
key
else
chat(prompt % { key: key, lang: lang }, directive: DIRECTIVE, **opts)
end
end.then do |text|