Module: Baltix::I18n

Included in:
Spec::Rpm
Defined in:
lib/baltix/i18n.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_localeObject



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_localesObject



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

Returns:

  • (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_treesObject



5
6
7
# File 'lib/baltix/i18n.rb', line 5

def locale_trees
   @locale_trees ||= {}
end

.localesObject



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, options = {}
   t!(path, options)
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, options = {}
   parts = path.to_s.split(".")
   locale = options[:locale].blank? && default_locale || options[:locale]
   #binding.pry
   line = parts.reduce(locale_tree(locale)) { |r, part| [ r[part] ].flatten.first }

   line.gsub(/%\w+/) do |str|
      /%(?<name>\w+)/ =~ str

      if options[:binding].local_variable_defined?(name)
         options[:binding].local_variable_get(name)
      elsif options[:binding].instance_variable_defined?("@#{name}")
         options[:binding].instance_variable_get(:"@#{name}")
      end
   end
end

Instance Method Details

#t(path, options = {}) ⇒ Object



61
62
63
# File 'lib/baltix/i18n.rb', line 61

def t path, options = {}
   Baltix::I18n.t(path, options)
end