如何通过 URL 更改语言环境? [英] How to change the locale through URL?

查看:20
本文介绍了如何通过 URL 更改语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的双语 Rails 4 应用程序中,我有一个像这样的 LocalesController:

In my bi-lingual Rails 4 application I have a LocalesController like this:

class LocalesController < ApplicationController

  def change_locale
    if params[:set_locale]
      session[:locale] = params[:set_locale] 
      url_hash = Rails.application.routes.recognize_path URI(request.referer).path
      url_hash[:locale] = params[:set_locale]
      redirect_to url_hash
    end
  end

end

用户可以通过此表单更改其区域设置:

A User can change his locale through this form:

def locale_switcher
  form_tag url_for(:controller => 'locales', :action => 'change_locale'), :method => 'get', :id => 'locale_switcher' do
  select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s)
end

这行得通.

但是,目前用户无法通过 URL 更改语言.

However, right now there's no way for the user to change the language via the URL.

例如如果用户在页面 www.app.com/en/projects 上,然后手动将 URL 更改为 www.app.com/fr/projects,他 应该看到页面的法语版本,但没有任何反应.

E.g. if a user is on the page www.app.com/en/projects and then manually changes the URL to www.app.com/fr/projects, he should see the French version of the page, but instead nothing happens.

这在许多 Rails 应用程序中可能无关紧要,但在我的应用程序中却非常重要.

This may not matter in many Rails apps but in mine it is quite important.

如何解决?

感谢您的帮助.

推荐答案

这就是我在 Rails 4 应用程序之一中的做法:

This is how I did it in one of Rails 4 applications:

config/routes.rb 中:

Rails.application.routes.draw do
  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    # rest of your routes here
    # for example:
    resources :projects
  end
end

确保在 config/environments/production.rb 中这行没有注释:

make sure in config/environments/production.rb this line is uncommented:

config.i18n.fallbacks = true

如果您希望使用 :en 以外的 default_locale 设置,则在 config/application.rb 中取消注释此行:

If you wish to have a default_locale setup other than :en, then in config/application.rb, uncomment this line:

config.i18n.default_locale = :de # and then :de will be used as default locale

现在,设置的最后一部分,在 ApplicationController 中添加此方法:

Now, last part of your setup, add this method in ApplicationController:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :set_locale

  private
    def set_locale
      I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
      session[:locale] = I18n.locale
    end

   def default_url_options(options={})
     logger.debug "default_url_options is passed options: #{options.inspect}
"
     { locale: I18n.locale }
   end
end

现在,您的应用程序可以通过以下方式访问:http://localhost:3000/en/projectshttp://localhost:3000/fr/projects、或 http://localhost:3000/projects.最后一个 http://localhost:3000/projects 将使用 :en 作为其默认语言环境(除非您在 application.rb 中进行更改).

Now, your application can be accessed as: http://localhost:3000/en/projects, http://localhost:3000/fr/projects, or http://localhost:3000/projects. The last one http://localhost:3000/projects will use :en as its default locale(unless you make that change in application.rb).

这篇关于如何通过 URL 更改语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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