Module: I18n

Defined in:
lib/wikicloth/i18n.rb

Class Method Summary collapse

Class Method Details

.available_localesObject



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

def available_locales
  load_translations
  @@available_locales
end

.default_localeObject



6
7
8
# File 'lib/wikicloth/i18n.rb', line 6

def default_locale
  :en
end

.initialized!Object



74
75
76
# File 'lib/wikicloth/i18n.rb', line 74

def initialized!
  @@initialized = true
end

.initialized?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/wikicloth/i18n.rb', line 70

def initialized?
  @@initialized ||= false
end

.load_pathObject



39
40
41
# File 'lib/wikicloth/i18n.rb', line 39

def load_path
  @@load_paths ||= []
end

.load_path=(val) ⇒ Object



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

def load_path=(val)
  @@load_paths = val
end

.load_translationsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wikicloth/i18n.rb', line 47

def load_translations
  return if initialized?

  @@available_locales = []
  @@translations = {}
  load_path.each do |path|
    Dir[path].each do |file| 
      begin
        data = YAML::load(File.read(file))
        data.each do |key,value|
          @@available_locales << key.to_sym unless @@available_locales.include?(key.to_sym)
          @@translations[key.to_sym] ||= {}
          import_values(key.to_sym,value)
        end
      rescue ArgumentError => err
        puts "error in #{file}: #{err.message}"
      end
    end
  end

  initialized!
end

.localeObject



10
11
12
# File 'lib/wikicloth/i18n.rb', line 10

def locale
  @@locale ||= default_locale
end

.locale=(val) ⇒ Object



14
15
16
# File 'lib/wikicloth/i18n.rb', line 14

def locale=(val)
  @@locale = val
end

.t(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wikicloth/i18n.rb', line 18

def t(*args)
  options  = args.last.is_a?(Hash) ? args.pop : {}
  key      = args.shift

  load_translations
  use_locale = @@translations[locale].nil? || @@translations[locale].empty? ? default_locale : locale

  if !@@translations[use_locale].nil? && @@translations[use_locale].has_key?(key)
    add_vars(@@translations[use_locale][key], options)
  elsif use_locale != default_locale && @@translations[default_locale].has_key?(key)
    add_vars(@@translations[default_locale][key], options)
  else
    "translation missing: #{locale}, #{key}"
  end
end

.with_locale(tmp_locale = nil) ⇒ Object

Executes block with given I18n.locale set.



79
80
81
82
83
84
85
86
87
# File 'lib/wikicloth/i18n.rb', line 79

def with_locale(tmp_locale = nil)
  if tmp_locale
    current_locale = self.locale
    self.locale    = tmp_locale
  end
  yield
ensure
  self.locale = current_locale if tmp_locale
end