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



108
109
110
111
112
113
# File 'lib/zena/use/i18n.rb', line 108

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”



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/zena/use/i18n.rb', line 175

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



189
190
191
192
193
194
# File 'lib/zena/use/i18n.rb', line 189

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).
    


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/zena/use/i18n.rb', line 128

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],
# FIXME: This is good to protect templates and other documents but is *NOT* nice when translating a website !!
#        What should we do ?
    params[:node] ? params[:node][:v_lang] : nil,
    # 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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/zena/use/i18n.rb', line 157

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