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

backend[R]
locale[RW]
path[R]
root[R]

Public Class Methods

instance(locale = nil, root: :lazier, path: nil, force: false) click to toggle source

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
new(locale = nil, root: :lazier, path: nil) click to toggle source

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

locale=(value) click to toggle source

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
locales() click to toggle source

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
reload() click to toggle source

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
t(message, **args)
Alias for: translate
tl(locale, message, *args)
Alias for: translate_in_locale
translate(message, **args) click to toggle source

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
Also aliased as: t
translate_in_locale(locale, message, *args) click to toggle source

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
Also aliased as: tl
translations(locale = nil) click to toggle source

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
with_locale(locale) { || ... } click to toggle source

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