kan*_*nna 15 ruby ruby-on-rails internationalization rails-routing ruby-on-rails-3.2
使用Rails 3.2.8与以下宝石的应用程序
gem 'friendly_id', '~> 4.0'
gem 'route_translator'
Run Code Online (Sandbox Code Playgroud)
在/config/initializers/i18n.rb中
TLD_LOCALES = {
"com" => :en,
"jobs" => :en,
"net" => :en,
"in" => :en,
"de" => :de,
"ch" => :de,
"at" => :de,
"br" => :pt,
"ar" => :es,
"cl" => :es,
"mx" => :es
}
Run Code Online (Sandbox Code Playgroud)
在/app/controllers/application_controller.rb中,使用before-filter为每个请求设置区域设置:
before_filter :set_auto_locale
def set_auto_locale
I18n.locale = TLD_LOCALES[request.host.split('.').last]
end
Run Code Online (Sandbox Code Playgroud)
在routes.rb中
localized do
match "label_vacancies/:view_job"=>"job_seekers#view_job"
get "label_aboutus", :to => "home#about_us", :as => "about_us"
end
Run Code Online (Sandbox Code Playgroud)
当用户请求更改语言区域设置时,域下方应根据请求的区域设置加载.
在初始化器中
domain_based_on_locale = {
:en => "xxxxx.com",
:de => "xxxxx.de",
:es => "xxxxx.mx",
:pt => "xxxxx.com.br"
}
Run Code Online (Sandbox Code Playgroud)
在/app/controllers/application_controller.rb中
def set_manual_locale
if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
cookies['locale'] = { :value => params[:locale], :expires => 1.year.from_now }
I18n.locale = params[:locale].to_sym
elsif cookies['locale'] && I18n.available_locales.include?(cookies['locale'].to_sym)
I18n.locale = cookies['locale'].to_sym
end
if domain_based_on_locale[I18n.locale] != request.host
redirect_to "#{request.protocol}#{domain_based_on_locale[I18n.locale]}#{request.fullpath}", :status => :moved_permanently
else
redirect_to root_path
end
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,用户更改以下URL中的语言会导致重定向问题,因为同一页面根据语言具有不同的URL.
Aboutus:
http://xxxxxxx.com/about-us # About us route in English
http://xxxxxxx.de/uber-uns # About us route in German
http://xxxxxxx.mx/quienes-somos # About us route in Spanish
view Job:
http://xxxxxxx.com/jobs/rp-be-company-representante-de-ventas-22042015
http://xxxxxxx.de/ofertas-de-empleo/rp-be-company-representante-de-ventas-22042015
Run Code Online (Sandbox Code Playgroud)
手动语言区域设置更改后,如何在新域中重定向到同一页面.是否可以将运行会话带到新域.谢谢你的帮助.
您需要翻译每个片段request.fullpath(最后一个片段除外,它看起来像资源块)。您可以使用 Rails 的 I18n 手动执行此操作:
current_path = request.fullpath.split('/')
slug = current_path.pop
locale_path = current_path.map{|part| translate(part)}.join('/')
redirect_to "#{request.protocol}#{domain}#{locale_path}#{slug}"
Run Code Online (Sandbox Code Playgroud)
或者,有处理路由转换的 gem:
就会话而言,本身不可能跨域共享 cookie。如果您将语言环境设置为子域(例如de.xxxxx.com),则可以在所有子域之间共享 cookie。许多网站都是通过路径来实现的,例如。xxxxx.com/de/,这完全消除了问题。
支持不同的域需要您手动转移会话。您可以通过以下方式来实现:
xxx123并将其与其附加的会话一起保存在服务器端new.domain/path?token=xxx123仔细考虑传输过程 - 当你做这种事情时很容易引入安全问题。该 SO 线程有一种使用从其他域加载的图像的方法。
| 归档时间: |
|
| 查看次数: |
1215 次 |
| 最近记录: |