Module: I18n
- Defined in:
- lib/wikicloth/i18n.rb
Class Method Summary collapse
- .available_locales ⇒ Object
- .default_locale ⇒ Object
- .initialized! ⇒ Object
- .initialized? ⇒ Boolean
- .load_path ⇒ Object
- .load_path=(val) ⇒ Object
- .load_translations ⇒ Object
- .locale ⇒ Object
- .locale=(val) ⇒ Object
- .t(*args) ⇒ Object
-
.with_locale(tmp_locale = nil) ⇒ Object
Executes block with given I18n.locale set.
Class Method Details
.available_locales ⇒ Object
34 35 36 37 |
# File 'lib/wikicloth/i18n.rb', line 34 def available_locales load_translations @@available_locales end |
.default_locale ⇒ Object
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
70 71 72 |
# File 'lib/wikicloth/i18n.rb', line 70 def initialized? @@initialized ||= false end |
.load_path ⇒ Object
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_translations ⇒ Object
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.}" end end end initialized! end |
.locale ⇒ Object
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) = 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].has_key?(key) add_vars(@@translations[use_locale][key], ) elsif use_locale != default_locale && @@translations[default_locale].has_key?(key) add_vars(@@translations[default_locale][key], ) 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 |