Module: Baltix::I18n
- Included in:
- Spec::Rpm
- Defined in:
- lib/baltix/i18n.rb
Class Method Summary collapse
- .default_locale ⇒ Object
- .defaulted_locales ⇒ Object
- .is_default?(locale) ⇒ Boolean
- .locale_tree(locale) ⇒ Object
- .locale_trees ⇒ Object
- .locales ⇒ Object
- .t(path, options = {}) ⇒ Object
- .t!(path, options = {}) ⇒ Object
Instance Method Summary collapse
Class Method Details
.default_locale ⇒ Object
9 10 11 12 13 |
# File 'lib/baltix/i18n.rb', line 9 def default_locale @default_locale ||= locales.sort do |x, y| x == "en_US.UTF-8" && -1 || y == "en_US.UTF-8" && 1 || 0 end.first end |
.defaulted_locales ⇒ Object
21 22 23 |
# File 'lib/baltix/i18n.rb', line 21 def defaulted_locales @defaulted_locales ||= locales.map { |locale| locale == default_locale && "" || locale } end |
.is_default?(locale) ⇒ Boolean
50 51 52 |
# File 'lib/baltix/i18n.rb', line 50 def is_default? locale default_locale == locale.to_s end |
.locale_tree(locale) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/baltix/i18n.rb', line 25 def locale_tree locale return locale_trees[locale] if locale_trees[locale] file = File.join(File.dirname(__FILE__), "..", "..", "locale", "#{locale}.yaml") YAML.load(IO.read(file)) end |
.locale_trees ⇒ Object
5 6 7 |
# File 'lib/baltix/i18n.rb', line 5 def locale_trees @locale_trees ||= {} end |
.locales ⇒ Object
15 16 17 18 19 |
# File 'lib/baltix/i18n.rb', line 15 def locales @locales ||= Dir[File.join(File.dirname(__FILE__), "..", "..", "locale", "*.yaml")].map do |file| File.basename(file, ".yaml") end end |
.t(path, options = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/baltix/i18n.rb', line 54 def t path, = {} t!(path, ) rescue NoMethodError nil end |
.t!(path, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/baltix/i18n.rb', line 33 def t! path, = {} parts = path.to_s.split(".") locale = [:locale].blank? && default_locale || [:locale] #binding.pry line = parts.reduce(locale_tree(locale)) { |r, part| [ r[part] ].flatten.first } line.gsub(/%\w+/) do |str| /%(?<name>\w+)/ =~ str if [:binding].local_variable_defined?(name) [:binding].local_variable_get(name) elsif [:binding].instance_variable_defined?("@#{name}") [:binding].instance_variable_get(:"@#{name}") end end end |