Class: Ramaze::Tool::Localize
- Extended by:
- Ramaze::Trinity
- Defined in:
- lib/ramaze/tool/localize.rb
Direct Known Subclasses
Class Method Summary collapse
-
.call(response, options = {}) ⇒ Object
Enables being plugged into Dispatcher::Action::FILTER.
-
.default_language ⇒ Object
alias for trait.
-
.dictionary ⇒ Object
Returns the dictionary used for translation.
-
.file_for(locale) ⇒ Object
Call trait with the passed locale if it reponds to that, otherwise we call #to_s and % with the locale on it.
-
.languages ⇒ Object
alias for trait.
-
.load(*locales) ⇒ Object
Load given locales from disk and save it into the dictionary.
-
.localize(str, locale) ⇒ Object
Localizes a single ‘entity’.
-
.localize_body(body, options) ⇒ Object
Localizes a response body.
-
.set_session_locale ⇒ Object
Sets session to one of the languages defined in the dictionary.
-
.store(*locales) ⇒ Object
Stores given locales from the dictionary to disk.
Methods included from StateAccessor
each, #state_accessor, #state_reader, #state_writer
Class Method Details
.call(response, options = {}) ⇒ Object
Enables being plugged into Dispatcher::Action::FILTER
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ramaze/tool/localize.rb', line 64 def call(response, = {}) return response unless trait[:enable] body = [] response.body.each do |chunk| body << localize_body(chunk, ) end response.body = body.join end |
.default_language ⇒ Object
alias for trait
196 197 198 |
# File 'lib/ramaze/tool/localize.rb', line 196 def default_language trait[:default_language] end |
.dictionary ⇒ Object
Returns the dictionary used for translation.
138 139 140 141 |
# File 'lib/ramaze/tool/localize.rb', line 138 def dictionary trait[:languages].map! {|x| x.to_s }.uniq! trait[:dictionary] || load(*languages) end |
.file_for(locale) ⇒ Object
Call trait with the passed locale if it reponds to that, otherwise we call #to_s and % with the locale on it.
178 179 180 181 182 183 184 185 186 |
# File 'lib/ramaze/tool/localize.rb', line 178 def file_for(locale) file_source = trait[:file] if file_source.respond_to?(:call) file = file_source.call(locale) else file = file_source.to_s % locale end end |
.languages ⇒ Object
alias for trait
190 191 192 |
# File 'lib/ramaze/tool/localize.rb', line 190 def languages trait[:languages] end |
.load(*locales) ⇒ Object
Load given locales from disk and save it into the dictionary.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ramaze/tool/localize.rb', line 145 def load(*locales) Log.debug "loading locales: #{locales.inspect}" dict = trait[:dictionary] || {} locales.each do |locale| begin dict[locale] = YAML.load_file(file_for(locale)) rescue Errno::ENOENT dict[locale] = {} end end trait[:dictionary] = dict end |
.localize(str, locale) ⇒ Object
Localizes a single ‘entity’. If a translation in the chosen language is not available, it falls back to the default language.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ramaze/tool/localize.rb', line 100 def localize(str, locale) dict = dictionary dict[locale] ||= {} dict[default_language] ||= {} trans = dict[locale][str] ||= dict[default_language][str] ||= str rescue Object => ex Log.error(ex) str end |
.localize_body(body, options) ⇒ Object
Localizes a response body. It reacts to a regular expression as given in trait. Every ‘entity’ in it will be translated, see ‘localize` for more information.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ramaze/tool/localize.rb', line 80 def localize_body(body, ) locale = if languages.include?(response["Content-Language"]) response["Content-Language"] else (session[:LOCALE] || set_session_locale).to_s end body.gsub!(trait[:regex]) do localize($1, locale) unless $1.to_s.empty? end store(locale, default_language) if trait[:collect] body end |
.set_session_locale ⇒ Object
Sets session to one of the languages defined in the dictionary. It first tries to honor the browsers accepted languages and then falls back to the default language.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ramaze/tool/localize.rb', line 115 def set_session_locale session[:LOCALE] = default_language accepted_langs = request.locales << default_language mapping = trait[:mapping] dict = dictionary accepted_langs.each do |language| if mapped = mapping.find{|k,v| k === language } language = mapped[1] end if dict.key?(language) session[:LOCALE] = language break end end session[:LOCALE] end |
.store(*locales) ⇒ Object
Stores given locales from the dictionary to disk.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ramaze/tool/localize.rb', line 163 def store(*locales) locales.uniq.compact.each do |locale| file = file_for(locale) data = dictionary[locale].ya2yaml Log.dev "saving localized to: #{file}" File.open(file, 'w+'){|fd| fd << data } end rescue Errno::ENOENT => e Log.error e end |