Module: Lazier::I18n
Overview
Provides an easy way to localized messages in a class.
Instance Attribute Summary (collapse)
-
- (Object) i18_root
readonly
Returns the value of attribute i18_root.
-
- (String|Symbol|nil) i18n_locale
readonly
The current locale.
-
- (String) i18n_locales_path
readonly
The path where the translations are stored.
-
- (Symbol) i18n_root
readonly
The root level of the translation.
Instance Method Summary (collapse)
-
- (R18N::Translation) i18n
Get the list of available translation for the current locale.
-
- (R18n::Translation) i18n=(locale)
Set the current locale for messages.
-
- (Object) i18n_setup(root, path)
Setup all I18n translations.
Instance Attribute Details
- (Object) i18_root (readonly)
Returns the value of attribute i18_root
18 19 20 |
# File 'lib/lazier/i18n.rb', line 18 def i18_root @i18_root end |
- (String|Symbol|nil) i18n_locale (readonly)
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 |
# File 'lib/lazier/i18n.rb', line 16 module I18n attr_reader :i18n_locale attr_reader :i18_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_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 || "").to_s locales = [locale, ENV["LANG"], R18n::I18n.system_locale].collect { |l| find_locale_in_path(l.to_s, path)} locales << "en" # Add English as a fallback locales = locales.uniq.compact begin translation = R18n::I18n.new(locales, path).t.send((@i18n_root || "").to_s) raise ArgumentError if translation.is_a?(R18n::Untranslated) translation rescue raise Lazier::Exceptions::MissingTranslation.new(locales, path) end 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[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first end end |
- (String) i18n_locales_path (readonly)
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 |
# File 'lib/lazier/i18n.rb', line 16 module I18n attr_reader :i18n_locale attr_reader :i18_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_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 || "").to_s locales = [locale, ENV["LANG"], R18n::I18n.system_locale].collect { |l| find_locale_in_path(l.to_s, path)} locales << "en" # Add English as a fallback locales = locales.uniq.compact begin translation = R18n::I18n.new(locales, path).t.send((@i18n_root || "").to_s) raise ArgumentError if translation.is_a?(R18n::Untranslated) translation rescue raise Lazier::Exceptions::MissingTranslation.new(locales, path) end 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[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first end end |
- (Symbol) i18n_root (readonly)
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 |
# File 'lib/lazier/i18n.rb', line 16 module I18n attr_reader :i18n_locale attr_reader :i18_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_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 || "").to_s locales = [locale, ENV["LANG"], R18n::I18n.system_locale].collect { |l| find_locale_in_path(l.to_s, path)} locales << "en" # Add English as a fallback locales = locales.uniq.compact begin translation = R18n::I18n.new(locales, path).t.send((@i18n_root || "").to_s) raise ArgumentError if translation.is_a?(R18n::Untranslated) translation rescue raise Lazier::Exceptions::MissingTranslation.new(locales, path) end 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[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first end end |
Instance Method Details
- (R18N::Translation) i18n
Get the list of available translation for the current locale.
33 34 35 |
# File 'lib/lazier/i18n.rb', line 33 def i18n @i18n ||= i18n_load_locale(nil) end |
- (R18n::Translation) i18n=(locale)
Set the current locale for messages.
41 42 43 44 |
# File 'lib/lazier/i18n.rb', line 41 def i18n=(locale) @i18n_locale = locale @i18n = i18n_load_locale(locale) end |
- (Object) i18n_setup(root, path)
Setup all I18n translations.
25 26 27 28 |
# File 'lib/lazier/i18n.rb', line 25 def i18n_setup(root, path) @i18n_root = root.to_sym @i18n_locales_path = path end |