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

查看:106
本文介绍了如何通过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 中未注释此行:

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}\n"
     { locale: I18n.locale }
   end
end

现在,您的应用程序可以通过以下方式访问:http://localhost:3000/en/projectshttp://localhost:3000/fr/projectshttp://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天全站免登陆