Module: Lazier::I18n

Included in:
Configuration, Localizer, Settings
Defined in:
lib/lazier/i18n.rb

Overview

Provides an easy way to localized messages in a class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#i18n_localeString|Symbol|nil (readonly)

Returns The current locale.

Returns:

  • (String|Symbol|nil)

    The current locale.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lazier/i18n.rb', line 16

module I18n
  attr_reader :i18n_locale
  attr_reader :i18n_root
  attr_reader :i18n_locales_path

  # Setup all I18n translations.
  #
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  def i18n_setup(root, path)
    ::I18n.enforce_available_locales = true
    @i18n_root = root.to_sym
    @i18n_locales_path = path
  end

  # Get the list of available translation for the current locale.
  #
  # @return [R18N::Translation] The translation object.
  def i18n
    @i18n ||= i18n_load_locale(nil)
  end

  # Set the current locale for messages.
  #
  # @param locale [String|Symbol|nil] The new locale. Default is the current system locale.
  # @return [R18n::Translation] The new translation object.
  def i18n=(locale)
    @i18n_locale = locale
    @i18n = i18n_load_locale(locale)
  end

  private
    # Loads a locale for messages.
    #
    # @param locale [Symbol] The new locale. Default is the current system locale.
    # @return [R18n::Translation] The new translation object.
    def i18n_load_locale(locale)
      path = @i18n_locales_path || ""
      locales = validate_locales([locale], path)

      begin
        tokens = @i18n_root.to_s.split(/[:.]/)
        translation = tokens.reduce(R18n::I18n.new(locales, path).t) {|accu, token| accu.send(token) }
        raise ArgumentError if translation.is_a?(R18n::Untranslated)
        translation
      rescue
        raise Lazier::Exceptions::MissingTranslation.new(locales, path)
      end
    end

    # Validates locales for messages.
    #
    # @param locales [Array] The list of locales to validate. English is added as fallback.
    # @param path [String] The path where look into.
    # @return [Array] The list of valid locales.
    def validate_locales(locales, path)
      (locales + [ENV["LANG"], R18n::I18n.system_locale, "en"]).select { |l| find_locale_in_path(l, path)}.uniq.map(&:to_s)
    end

    # Find a locale file in a path.
    #
    # @param locale [String] The locale to find.
    # @param path [String] The path where look into.
    # @return [String|nil] The version of the locale found or `nil`, if nothing was found.
    def find_locale_in_path(locale, path)
      locale ? [locale, locale[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first : nil
    end
end

#i18n_locales_pathString (readonly)

Returns The path where the translations are stored.

Returns:

  • (String)

    The path where the translations are stored.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lazier/i18n.rb', line 16

module I18n
  attr_reader :i18n_locale
  attr_reader :i18n_root
  attr_reader :i18n_locales_path

  # Setup all I18n translations.
  #
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  def i18n_setup(root, path)
    ::I18n.enforce_available_locales = true
    @i18n_root = root.to_sym
    @i18n_locales_path = path
  end

  # Get the list of available translation for the current locale.
  #
  # @return [R18N::Translation] The translation object.
  def i18n
    @i18n ||= i18n_load_locale(nil)
  end

  # Set the current locale for messages.
  #
  # @param locale [String|Symbol|nil] The new locale. Default is the current system locale.
  # @return [R18n::Translation] The new translation object.
  def i18n=(locale)
    @i18n_locale = locale
    @i18n = i18n_load_locale(locale)
  end

  private
    # Loads a locale for messages.
    #
    # @param locale [Symbol] The new locale. Default is the current system locale.
    # @return [R18n::Translation] The new translation object.
    def i18n_load_locale(locale)
      path = @i18n_locales_path || ""
      locales = validate_locales([locale], path)

      begin
        tokens = @i18n_root.to_s.split(/[:.]/)
        translation = tokens.reduce(R18n::I18n.new(locales, path).t) {|accu, token| accu.send(token) }
        raise ArgumentError if translation.is_a?(R18n::Untranslated)
        translation
      rescue
        raise Lazier::Exceptions::MissingTranslation.new(locales, path)
      end
    end

    # Validates locales for messages.
    #
    # @param locales [Array] The list of locales to validate. English is added as fallback.
    # @param path [String] The path where look into.
    # @return [Array] The list of valid locales.
    def validate_locales(locales, path)
      (locales + [ENV["LANG"], R18n::I18n.system_locale, "en"]).select { |l| find_locale_in_path(l, path)}.uniq.map(&:to_s)
    end

    # Find a locale file in a path.
    #
    # @param locale [String] The locale to find.
    # @param path [String] The path where look into.
    # @return [String|nil] The version of the locale found or `nil`, if nothing was found.
    def find_locale_in_path(locale, path)
      locale ? [locale, locale[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first : nil
    end
end

#i18n_rootSymbol (readonly)

Returns The root level of the translation.

Returns:

  • (Symbol)

    The root level of the translation.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lazier/i18n.rb', line 16

module I18n
  attr_reader :i18n_locale
  attr_reader :i18n_root
  attr_reader :i18n_locales_path

  # Setup all I18n translations.
  #
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  def i18n_setup(root, path)
    ::I18n.enforce_available_locales = true
    @i18n_root = root.to_sym
    @i18n_locales_path = path
  end

  # Get the list of available translation for the current locale.
  #
  # @return [R18N::Translation] The translation object.
  def i18n
    @i18n ||= i18n_load_locale(nil)
  end

  # Set the current locale for messages.
  #
  # @param locale [String|Symbol|nil] The new locale. Default is the current system locale.
  # @return [R18n::Translation] The new translation object.
  def i18n=(locale)
    @i18n_locale = locale
    @i18n = i18n_load_locale(locale)
  end

  private
    # Loads a locale for messages.
    #
    # @param locale [Symbol] The new locale. Default is the current system locale.
    # @return [R18n::Translation] The new translation object.
    def i18n_load_locale(locale)
      path = @i18n_locales_path || ""
      locales = validate_locales([locale], path)

      begin
        tokens = @i18n_root.to_s.split(/[:.]/)
        translation = tokens.reduce(R18n::I18n.new(locales, path).t) {|accu, token| accu.send(token) }
        raise ArgumentError if translation.is_a?(R18n::Untranslated)
        translation
      rescue
        raise Lazier::Exceptions::MissingTranslation.new(locales, path)
      end
    end

    # Validates locales for messages.
    #
    # @param locales [Array] The list of locales to validate. English is added as fallback.
    # @param path [String] The path where look into.
    # @return [Array] The list of valid locales.
    def validate_locales(locales, path)
      (locales + [ENV["LANG"], R18n::I18n.system_locale, "en"]).select { |l| find_locale_in_path(l, path)}.uniq.map(&:to_s)
    end

    # Find a locale file in a path.
    #
    # @param locale [String] The locale to find.
    # @param path [String] The path where look into.
    # @return [String|nil] The version of the locale found or `nil`, if nothing was found.
    def find_locale_in_path(locale, path)
      locale ? [locale, locale[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first : nil
    end
end

Instance Method Details

#i18nR18N::Translation

Get the list of available translation for the current locale.

Returns:

  • (R18N::Translation)

    The translation object.



34
35
36
# File 'lib/lazier/i18n.rb', line 34

def i18n
  @i18n ||= i18n_load_locale(nil)
end

#i18n=(locale) ⇒ R18n::Translation

Set the current locale for messages.

Parameters:

  • locale (String|Symbol|nil)

    The new locale. Default is the current system locale.

Returns:

  • (R18n::Translation)

    The new translation object.



42
43
44
45
# File 'lib/lazier/i18n.rb', line 42

def i18n=(locale)
  @i18n_locale = locale
  @i18n = i18n_load_locale(locale)
end

#i18n_setup(root, path) ⇒ Object

Setup all I18n translations.

Parameters:

  • root (Symbol)

    The root level of the translation.

  • path (String)

    The path where the translations are stored.



25
26
27
28
29
# File 'lib/lazier/i18n.rb', line 25

def i18n_setup(root, path)
  ::I18n.enforce_available_locales = true
  @i18n_root = root.to_sym
  @i18n_locales_path = path
end