Module: Zena::Use::I18n::ControllerMethods

Defined in:
lib/zena/use/i18n.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



119
120
121
122
123
124
# File 'lib/zena/use/i18n.rb', line 119

def self.included(base)
  FastGettext.add_text_domain 'zena', :path => "#{Zena::ROOT}/locale"
  base.prepend_before_filter { FastGettext.text_domain = 'zena' }
  base.before_filter :set_lang, :check_lang
  base.after_filter  :set_encoding
end

Instance Method Details

#check_langObject

Redirect on lang change “…?lang=de”



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/zena/use/i18n.rb', line 183

def check_lang
  if params[:lang]
    # redirects other controllers (users controller, etc)
    redirect_url = params
    redirect_url.delete(:lang)
    if params[:controller] == 'nodes'
      redirect_to redirect_url.merge(:prefix => prefix) and return false
    else
      redirect_to redirect_url and return false
    end
  end
  true
end

#set_encodingObject



197
198
199
200
201
202
# File 'lib/zena/use/i18n.rb', line 197

def set_encoding
  headers['Content-Type'] ||= 'text/html'
  if headers['Content-Type'].starts_with?('text/') and !headers['Content-Type'].include?('charset=')
    headers['Content-Type'] += '; charset=utf-8'
  end
end

#set_langObject

Choose best language to display content.

  1. ‘test.host/oo?lang=en’ use ‘lang’, redirect without lang

  2. test.host/nodes/12?node=es

  3. ‘test.host/oo’ use visitor

  4. ‘test.host/es’ use prefix ONLY ENABLED FOR html format (see below)

  5. ‘test.host/’ use session

  6. ‘test.host/oo’ use visitor lang

  7. ‘test.host/’ use HTTP_ACCEPT_LANGUAGE

  8. ‘test.host/’ use default language

  9. ‘test.host/es’ the redirect for this rule is called once we are sure the request is

    not for document data (lang in this case can be different from what the visitor is
    visiting due to caching optimization).
    


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/zena/use/i18n.rb', line 139

def set_lang
  if params[:prefix] =~ /^\d+$/
    # this has nothing to do with set_lang...
    # 'test.host/34' --> /en/node34.html
    redirect_to "/#{prefix}/#{params[:prefix]}"
    return false
  end

  chosen_lang = nil
  [
    params[:lang],
    # Avoid redirects for static assets (cached documents).
    request.format == Mime::HTML ? params[:prefix] : nil,
    visitor.is_anon? ? session[:lang] : visitor.lang,
    (request.headers['HTTP_ACCEPT_LANGUAGE'] || '').split(',').sort {|a,b| (b.split(';q=')[1] || 1.0).to_f <=> (a.split(';q=')[1] || 1.0).to_f }.map {|l| l.split(';')[0].split('-')[0] },
    (visitor.is_anon? ? visitor.lang : nil), # anonymous user's lang comes last
  ].compact.flatten.uniq.each do |l|
    if current_site.lang_list.include?(l)
      chosen_lang = l
      break
    end
  end
  set_visitor_lang(chosen_lang || current_site[:default_lang])
  true
end

#set_visitor_lang(l) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/zena/use/i18n.rb', line 165

def set_visitor_lang(l)
  return unless current_site.lang_list.include?(l)
  session[:lang] = l

  if visitor.lang != l && !visitor.is_anon?
    visitor.update_attribute('lang', l)
  else
    visitor.lang = l
  end

  if File.exist?("#{Zena::ROOT}/locale/#{l}/LC_MESSAGES/zena.mo")
    ::I18n.locale = l
  else
    ::I18n.locale = 'en'
  end
end