Class: Ramaze::Tool::Localize

Inherits:
Object
  • Object
show all
Extended by:
Ramaze::Trinity
Defined in:
lib/ramaze/tool/localize.rb

Direct Known Subclasses

Gettext

Class Method Summary collapse

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, options = {})
  return response unless trait[:enable]

  body = []

  response.body.each do |chunk|
    body << localize_body(chunk, options)
  end

  response.body = body.join
end

.default_languageObject

alias for trait



196
197
198
# File 'lib/ramaze/tool/localize.rb', line 196

def default_language
  trait[:default_language]
end

.dictionaryObject

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

.languagesObject

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, options)
  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_localeObject

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