class Lazier::I18n
Provides an easy way to localized messages in a class.
@attribute locale
@return [String|Symbol|nil] The current locale.
@attribute [r] root
@return [Symbol] The root level of the translation.
@attribute [r] path
@return [String] The path where the translations are stored.
@attribute [r] backend
@return [I18n::Backend] The backend used for translations.
Attributes
Public Class Methods
Returns the singleton instance of the settings.
@param locale [Symbol|NilClass] The locale to use for translations. Default is the current system locale. @param root [Symbol] The root level of the translation. @param path [String|NilClass] The path where the translations are stored. @param force [Boolean] Whether to force recreation of the instance. @return [I18n] The singleton instance of the i18n.
# File lib/lazier/i18n.rb, line 31 def self.instance(locale = nil, root: :lazier, path: nil, force: false) @instance = nil if force @instance ||= new(locale, root: root, path: path) end
Creates a new I18n
object.
@param locale [Symbol|NilClass] The locale to use. Defaults to the current locale. @param root [Symbol] The root level of the translation. @param path [String|NilClass] The path where the translations are stored.
# File lib/lazier/i18n.rb, line 41 def initialize(locale = nil, root: :lazier, path: nil) Lazier.load_object path ||= Lazier::ROOT + "/locales" @root = root.to_sym @path = File.absolute_path(path.to_s) setup_backend self.locale = (locale || Lazier::I18n.default_locale || system_locale).to_sym end
Public Instance Methods
Sets the current locale.
@param value [Symbol] The locale to use for translations. Default is the current system locale.
# File lib/lazier/i18n.rb, line 70 def locale=(value) @locale = value.to_sym ::I18n.locale = @locale end
Get the list of available translation for a locale.
@return [Array] The list of available locales.
# File lib/lazier/i18n.rb, line 78 def locales ::I18n.available_locales end
Reloads all the I18n
translations.
# File lib/lazier/i18n.rb, line 53 def reload # Extract the backend to an attribute ::I18n.backend.load_translations end
Localize a message.
@param message [String|Symbol] The message to localize. @param args [Array] Optional arguments to localize the message. @return [String] The localized message.
# File lib/lazier/i18n.rb, line 87 def translate(message, **args) # PI: Ignore reek on this. message = "#{root}.#{message}" if message !~ /^(\.|::)/ begin ::I18n.translate(message, **args.merge(raise: true)) rescue ::I18n::MissingTranslationData raise Lazier::Exceptions::MissingTranslation, [locale, message] end end
Localize a message in a specific locale.
@param locale [String|Symbol] The new locale to use for localization. @param message [String|Symbol] The message to localize. @param args [Array] Optional arguments to localize the message. @return [String] The localized message.
# File lib/lazier/i18n.rb, line 105 def translate_in_locale(locale, message, *args) with_locale(locale) { translate(message, *args) } end
Gets the list of available translation for a locale.
@param locale [Symbol|NilClass] The locale to list. Defaults to the current locale. @return [Hash] The available translations for the specified locale.
# File lib/lazier/i18n.rb, line 62 def translations(locale = nil) locale ||= @locale @backend.send(:translations)[locale.to_sym] || {} end
Temporary sets a different locale and execute the given block.
@param locale [String|Symbol] The new locale to use for localization.
# File lib/lazier/i18n.rb, line 113 def with_locale(locale) old_locale = self.locale begin self.locale = locale return yield ensure self.locale = old_locale end end