Class: Lazier::I18n

Inherits:
Object
  • Object
show all
Defined in:
lib/lazier/i18n.rb

Overview

Provides an easy way to localized messages in a class.

Constant Summary

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (I18n) initialize(locale = nil, root: :lazier, path: nil)

Creates a new I18n object.

Parameters:

  • locale (Symbol) (defaults to: nil)

    The locale to use. Defaults to the current locale.

  • root (Symbol)

    The root level of the translation.

  • path (String)

    The path where the translations are stored.



41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

- (I18n::Backend) backend (readonly)

Returns The backend used for translations.

Returns:

  • (I18n::Backend)

    The backend used for translations.



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lazier/i18n.rb', line 17

class I18n
  attr_accessor :locale
  attr_reader :root, :path, :backend

  # The default locale for new instances.
  mattr_accessor :default_locale

  # Returns the singleton instance of the settings.
  #
  # @param locale [Symbol] The locale to use for translations. Default is the current system locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [I18n] The singleton instance of the i18n.
  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] The locale to use. Defaults to the current locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  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

  # Reloads all the I18n translations.
  def reload
    # Extract the backend to an attribute
    ::I18n.backend.load_translations
  end

  # Gets the list of available translation for a locale.
  #
  # @param locale [Symbol] The locale to list. Defaults to the current locale.
  # @return [Hash] The available translations for the specified locale.
  def translations(locale = nil)
    locale ||= @locale
    @backend.send(:translations)[locale.to_sym] || {}
  end

  # Sets the current locale.
  #
  # @param value [Symbol] The locale to use for translations. Default is the current system locale.
  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.
  def locales
    ::I18n.available_locales
  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.
  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
  alias_method :t, :translate

  # Localize a message in a specific locale.
  #
  # @param message [String|Symbol] The message to localize.
  # @param locale [String|Symbol] The new locale to use for localization.
  # @param args [Array] Optional arguments to localize the message.
  # @return [String] The localized message.
  def translate_in_locale(locale, message, *args)
    with_locale(locale) { translate(message, *args) }
  end
  alias_method :tl, :translate_in_locale

  # Temporary sets a different locale and execute the given block.
  #
  # @param locale [String|Symbol] The new locale to use for localization.
  def with_locale(locale)
    old_locale = self.locale

    begin
      self.locale = locale
      return yield
    ensure
      self.locale = old_locale
    end
  end

  private

  # :nodoc:
  OSX_DETECTION = "defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'"

  # :nodoc:
  def system_locale
    platform = Lazier.platform

    rv = [:java, :osx, :posix].include?(platform) ? send("system_locale_#{Lazier.platform}") : nil
    raise(RuntimeError) if rv.blank?
    rv
  rescue
    "en"
  end

  # :nodoc:
  def system_locale_java
    Java.java.util.Locale.getDefault.toString
  end

  # :nodoc:
  def system_locale_osx
    `#{OSX_DETECTION}`.strip
  end

  # :nodoc:
  def system_locale_posix
    ENV["LANG"]
  end

  # :nodoc:
  def setup_backend
    ::I18n.load_path += Dir["#{@path}/*.yml"]
    ::I18n.load_path.uniq!
    ::I18n.exception_handler = ::Lazier::Exceptions::TranslationExceptionHandler.new
    reload

    @backend = ::I18n.backend
  end
end

- (String|Symbol|nil) locale

Returns The current locale.

Returns:

  • (String|Symbol|nil)

    The current locale.



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lazier/i18n.rb', line 17

class I18n
  attr_accessor :locale
  attr_reader :root, :path, :backend

  # The default locale for new instances.
  mattr_accessor :default_locale

  # Returns the singleton instance of the settings.
  #
  # @param locale [Symbol] The locale to use for translations. Default is the current system locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [I18n] The singleton instance of the i18n.
  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] The locale to use. Defaults to the current locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  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

  # Reloads all the I18n translations.
  def reload
    # Extract the backend to an attribute
    ::I18n.backend.load_translations
  end

  # Gets the list of available translation for a locale.
  #
  # @param locale [Symbol] The locale to list. Defaults to the current locale.
  # @return [Hash] The available translations for the specified locale.
  def translations(locale = nil)
    locale ||= @locale
    @backend.send(:translations)[locale.to_sym] || {}
  end

  # Sets the current locale.
  #
  # @param value [Symbol] The locale to use for translations. Default is the current system locale.
  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.
  def locales
    ::I18n.available_locales
  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.
  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
  alias_method :t, :translate

  # Localize a message in a specific locale.
  #
  # @param message [String|Symbol] The message to localize.
  # @param locale [String|Symbol] The new locale to use for localization.
  # @param args [Array] Optional arguments to localize the message.
  # @return [String] The localized message.
  def translate_in_locale(locale, message, *args)
    with_locale(locale) { translate(message, *args) }
  end
  alias_method :tl, :translate_in_locale

  # Temporary sets a different locale and execute the given block.
  #
  # @param locale [String|Symbol] The new locale to use for localization.
  def with_locale(locale)
    old_locale = self.locale

    begin
      self.locale = locale
      return yield
    ensure
      self.locale = old_locale
    end
  end

  private

  # :nodoc:
  OSX_DETECTION = "defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'"

  # :nodoc:
  def system_locale
    platform = Lazier.platform

    rv = [:java, :osx, :posix].include?(platform) ? send("system_locale_#{Lazier.platform}") : nil
    raise(RuntimeError) if rv.blank?
    rv
  rescue
    "en"
  end

  # :nodoc:
  def system_locale_java
    Java.java.util.Locale.getDefault.toString
  end

  # :nodoc:
  def system_locale_osx
    `#{OSX_DETECTION}`.strip
  end

  # :nodoc:
  def system_locale_posix
    ENV["LANG"]
  end

  # :nodoc:
  def setup_backend
    ::I18n.load_path += Dir["#{@path}/*.yml"]
    ::I18n.load_path.uniq!
    ::I18n.exception_handler = ::Lazier::Exceptions::TranslationExceptionHandler.new
    reload

    @backend = ::I18n.backend
  end
end

- (String) path (readonly)

Returns The path where the translations are stored.

Returns:

  • (String)

    The path where the translations are stored.



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lazier/i18n.rb', line 17

class I18n
  attr_accessor :locale
  attr_reader :root, :path, :backend

  # The default locale for new instances.
  mattr_accessor :default_locale

  # Returns the singleton instance of the settings.
  #
  # @param locale [Symbol] The locale to use for translations. Default is the current system locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [I18n] The singleton instance of the i18n.
  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] The locale to use. Defaults to the current locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  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

  # Reloads all the I18n translations.
  def reload
    # Extract the backend to an attribute
    ::I18n.backend.load_translations
  end

  # Gets the list of available translation for a locale.
  #
  # @param locale [Symbol] The locale to list. Defaults to the current locale.
  # @return [Hash] The available translations for the specified locale.
  def translations(locale = nil)
    locale ||= @locale
    @backend.send(:translations)[locale.to_sym] || {}
  end

  # Sets the current locale.
  #
  # @param value [Symbol] The locale to use for translations. Default is the current system locale.
  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.
  def locales
    ::I18n.available_locales
  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.
  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
  alias_method :t, :translate

  # Localize a message in a specific locale.
  #
  # @param message [String|Symbol] The message to localize.
  # @param locale [String|Symbol] The new locale to use for localization.
  # @param args [Array] Optional arguments to localize the message.
  # @return [String] The localized message.
  def translate_in_locale(locale, message, *args)
    with_locale(locale) { translate(message, *args) }
  end
  alias_method :tl, :translate_in_locale

  # Temporary sets a different locale and execute the given block.
  #
  # @param locale [String|Symbol] The new locale to use for localization.
  def with_locale(locale)
    old_locale = self.locale

    begin
      self.locale = locale
      return yield
    ensure
      self.locale = old_locale
    end
  end

  private

  # :nodoc:
  OSX_DETECTION = "defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'"

  # :nodoc:
  def system_locale
    platform = Lazier.platform

    rv = [:java, :osx, :posix].include?(platform) ? send("system_locale_#{Lazier.platform}") : nil
    raise(RuntimeError) if rv.blank?
    rv
  rescue
    "en"
  end

  # :nodoc:
  def system_locale_java
    Java.java.util.Locale.getDefault.toString
  end

  # :nodoc:
  def system_locale_osx
    `#{OSX_DETECTION}`.strip
  end

  # :nodoc:
  def system_locale_posix
    ENV["LANG"]
  end

  # :nodoc:
  def setup_backend
    ::I18n.load_path += Dir["#{@path}/*.yml"]
    ::I18n.load_path.uniq!
    ::I18n.exception_handler = ::Lazier::Exceptions::TranslationExceptionHandler.new
    reload

    @backend = ::I18n.backend
  end
end

- (Symbol) root (readonly)

Returns The root level of the translation.

Returns:

  • (Symbol)

    The root level of the translation.



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lazier/i18n.rb', line 17

class I18n
  attr_accessor :locale
  attr_reader :root, :path, :backend

  # The default locale for new instances.
  mattr_accessor :default_locale

  # Returns the singleton instance of the settings.
  #
  # @param locale [Symbol] The locale to use for translations. Default is the current system locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [I18n] The singleton instance of the i18n.
  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] The locale to use. Defaults to the current locale.
  # @param root [Symbol] The root level of the translation.
  # @param path [String] The path where the translations are stored.
  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

  # Reloads all the I18n translations.
  def reload
    # Extract the backend to an attribute
    ::I18n.backend.load_translations
  end

  # Gets the list of available translation for a locale.
  #
  # @param locale [Symbol] The locale to list. Defaults to the current locale.
  # @return [Hash] The available translations for the specified locale.
  def translations(locale = nil)
    locale ||= @locale
    @backend.send(:translations)[locale.to_sym] || {}
  end

  # Sets the current locale.
  #
  # @param value [Symbol] The locale to use for translations. Default is the current system locale.
  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.
  def locales
    ::I18n.available_locales
  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.
  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
  alias_method :t, :translate

  # Localize a message in a specific locale.
  #
  # @param message [String|Symbol] The message to localize.
  # @param locale [String|Symbol] The new locale to use for localization.
  # @param args [Array] Optional arguments to localize the message.
  # @return [String] The localized message.
  def translate_in_locale(locale, message, *args)
    with_locale(locale) { translate(message, *args) }
  end
  alias_method :tl, :translate_in_locale

  # Temporary sets a different locale and execute the given block.
  #
  # @param locale [String|Symbol] The new locale to use for localization.
  def with_locale(locale)
    old_locale = self.locale

    begin
      self.locale = locale
      return yield
    ensure
      self.locale = old_locale
    end
  end

  private

  # :nodoc:
  OSX_DETECTION = "defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'"

  # :nodoc:
  def system_locale
    platform = Lazier.platform

    rv = [:java, :osx, :posix].include?(platform) ? send("system_locale_#{Lazier.platform}") : nil
    raise(RuntimeError) if rv.blank?
    rv
  rescue
    "en"
  end

  # :nodoc:
  def system_locale_java
    Java.java.util.Locale.getDefault.toString
  end

  # :nodoc:
  def system_locale_osx
    `#{OSX_DETECTION}`.strip
  end

  # :nodoc:
  def system_locale_posix
    ENV["LANG"]
  end

  # :nodoc:
  def setup_backend
    ::I18n.load_path += Dir["#{@path}/*.yml"]
    ::I18n.load_path.uniq!
    ::I18n.exception_handler = ::Lazier::Exceptions::TranslationExceptionHandler.new
    reload

    @backend = ::I18n.backend
  end
end

Class Method Details

+ (I18n) instance(locale = nil, root: :lazier, path: nil, force: false)

Returns the singleton instance of the settings.

Parameters:

  • locale (Symbol) (defaults to: nil)

    The locale to use for translations. Default is the current system locale.

  • root (Symbol)

    The root level of the translation.

  • path (String)

    The path where the translations are stored.

  • force (Boolean)

    If to force recreation of the instance.

Returns:

  • (I18n)

    The singleton instance of the i18n.



31
32
33
34
# 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

Instance Method Details

- (Array) locales

Get the list of available translation for a locale.

Returns:

  • (Array)

    The list of available locales.



78
79
80
# File 'lib/lazier/i18n.rb', line 78

def locales
  ::I18n.available_locales
end

- (Object) reload

Reloads all the I18n translations.



53
54
55
56
# File 'lib/lazier/i18n.rb', line 53

def reload
  # Extract the backend to an attribute
  ::I18n.backend.load_translations
end

- (String) translate(message, **args) Also known as: t

Localize a message.

Parameters:

  • message (String|Symbol)

    The message to localize.

  • args (Array)

    Optional arguments to localize the message.

Returns:

  • (String)

    The localized message.



87
88
89
90
91
92
93
94
95
96
# 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

- (String) translate_in_locale(locale, message, *args) Also known as: tl

Localize a message in a specific locale.

Parameters:

  • message (String|Symbol)

    The message to localize.

  • locale (String|Symbol)

    The new locale to use for localization.

  • args (Array)

    Optional arguments to localize the message.

Returns:

  • (String)

    The localized message.



105
106
107
# File 'lib/lazier/i18n.rb', line 105

def translate_in_locale(locale, message, *args)
  with_locale(locale) { translate(message, *args) }
end

- (Hash) translations(locale = nil)

Gets the list of available translation for a locale.

Parameters:

  • locale (Symbol) (defaults to: nil)

    The locale to list. Defaults to the current locale.

Returns:

  • (Hash)

    The available translations for the specified locale.



62
63
64
65
# File 'lib/lazier/i18n.rb', line 62

def translations(locale = nil)
  locale ||= @locale
  @backend.send(:translations)[locale.to_sym] || {}
end

- (Object) with_locale(locale)

Temporary sets a different locale and execute the given block.

Parameters:

  • locale (String|Symbol)

    The new locale to use for localization.



113
114
115
116
117
118
119
120
121
122
# 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