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.
Class Method Details
.call(response, options = {}) ⇒ Object
Enables being plugged into Dispatcher::Action::FILTER
64 65 66 67 68 69 70 |
# File 'lib/ramaze/tool/localize.rb', line 64 def call(response, = {}) return response unless trait[:enable] return response if response.body.nil? return response if response.body.respond_to?(:read) response.body = localize_body(response.body, ) response end |
.default_language ⇒ Object
alias for trait
192 193 194 |
# File 'lib/ramaze/tool/localize.rb', line 192 def default_language trait[:default_language] end |
.dictionary ⇒ Object
Returns the dictionary used for translation.
134 135 136 137 |
# File 'lib/ramaze/tool/localize.rb', line 134 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.
174 175 176 177 178 179 180 181 182 |
# File 'lib/ramaze/tool/localize.rb', line 174 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
186 187 188 |
# File 'lib/ramaze/tool/localize.rb', line 186 def languages trait[:languages] end |
.load(*locales) ⇒ Object
Load given locales from disk and save it into the dictionary.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ramaze/tool/localize.rb', line 141 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.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ramaze/tool/localize.rb', line 96 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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ramaze/tool/localize.rb', line 76 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ramaze/tool/localize.rb', line 111 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.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ramaze/tool/localize.rb', line 159 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 |