在Rails 3中在运行时更改语言环境 [英] Change locale at runtime in Rails 3

查看:135
本文介绍了在Rails 3中在运行时更改语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 3应用程序,该应用程序在我的locales文件夹中具有不同的语言.这些文件是en.yml,pu.yml,sp.yml.所有语言都必须转换为各种格式,我需要帮助,以使用户选择自己喜欢的任何一种语言,并提供诸如

I am working on a rails 3 app which has different languages in my locales folder. The files are en.yml, pu.yml, sp.yml. All languages have to be converted to their various format and I need help in making users chose any language of their choice with a link like

<%= link_to "English language", ...%> <%= link_to "spanish", ...%>

当用户选择一种语言时,该语言将被设置为用户的首选语言,从而使用户不必在每次登录后继续选择一种语言.

When a user choses a language, the language is set as the user's preferred language so that the user does not have to keep selecting a language after each login.

推荐答案

添加到ream88的答案中:

Adding to ream88's answer:

<%= link_to "spanish", :controller => 'locale', :action => 'set', :id => 'es' %>

在LocaleController(或任何其他控制器)中

In the LocaleController (or any other controller)

def set
  locale = params[:id]
  raise 'unsupported locale' unless ['es', 'en', ... ].include?(locale)
  current_user.locale = locale
  current_user.save
  redirect_to :back
end

这篇关于在Rails 3中在运行时更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆