如何使用route_translator gem而不需要Locale?

时间:2015-05-19 17:07:15

标签: ruby-on-rails localization routes internationalization rails-i18n

当我需要语言环境时,我能够成功使用route_translator gem。当我在两个语言环境之间进行更改时(en:fr),单击任何链接并显示正确的视图和链接翻译都没有问题。

这是我的config/routes.rb文件,其中我的路线按预期工作。

MyRailsApp::Application.routes.draw do  

  scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do  
#  scope "(/:locale)", locale: /#{I18n.available_locales.join("|")}/ do

#    resources :pages

    localized do
#      resources :pages
      match "/about",      to: "pages#about", via: "get"
      match "/clients",    to: "pages#clients", via: "get"
      match "/contact",    to: "pages#contact", via: "get"
      match "/manage",     to: "pages#manage", via: "get"
      match "/media",      to: "pages#media", via: "get"
      match "/privacy",    to: "pages#privacy", via: "get"
      match "/system",     to: "pages#system", via: "get"
      match "/testpage",   to: "pages#testpage", via: "get"
    end

  end

  root                     to: "pages#home", via: :get
  match "/:locale" => "pages#home", via: :get, :as => "locale_root"

end

以下是app/controllers/application_controller.rb中的区域设置逻辑,如果找不到区域设置,则会将区域设置默认为 en

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_filter :set_locale

  def default_url_options(options={})
    { locale: I18n.locale }
  end

  private

    def set_locale
      I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || I18n.default_locale || 'en'
      cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale 
    end

end

rake routes

   about_fr GET  /:locale/qui_sommes_nous(.:format) pages#about {:locale=>"fr"}
   about_en GET  /:locale/about(.:format)           pages#about {:locale=>"en"}
 clients_fr GET  /:locale/clients(.:format)         pages#clients {:locale=>"fr"}
 clients_en GET  /:locale/clients(.:format)         pages#clients {:locale=>"en"}
 contact_fr GET  /:locale/contactez_nous(.:format)  pages#contact {:locale=>"fr"}
 contact_en GET  /:locale/contact(.:format)         pages#contact {:locale=>"en"}
  manage_fr GET  /:locale/gerer(.:format)           pages#manage {:locale=>"fr"}
  manage_en GET  /:locale/manage(.:format)          pages#manage {:locale=>"en"}
   media_fr GET  /:locale/edition(.:format)         pages#media {:locale=>"fr"}
   media_en GET  /:locale/media(.:format)           pages#media {:locale=>"en"}
 privacy_fr GET  /:locale/confidentialite(.:format) pages#privacy {:locale=>"fr"}
 privacy_en GET  /:locale/privacy(.:format)         pages#privacy {:locale=>"en"}
  system_fr GET  /:locale/systeme(.:format)         pages#system {:locale=>"fr"}
  system_en GET  /:locale/system(.:format)          pages#system {:locale=>"en"}
testpage_fr GET  /:locale/testpage(.:format)        pages#testpage {:locale=>"fr"}
testpage_en GET  /:locale/testpage(.:format)        pages#testpage {:locale=>"en"}
       root GET  /                                  pages#home
locale_root GET  /:locale(.:format)                 pages#home

当我将范围语句更改为第二个语句时出现问题。我可以显示locale_root路径并切换语言环境没有问题。如果我在en中显示另一个视图,我可以将语言环境切换为fr并成功显示法语视图。但是,当我尝试切换回en时,绘制的路线包括 / fr / en 而不是 / en

`rake routes` with `(/:locale)`:

   about_fr GET  /fr(/:locale)/qui_sommes_nous(.:format) pages#about {:locale=>"fr"}
   about_en GET  (/:locale)/about(.:format)              pages#about {:locale=>"en"}
 clients_fr GET  /fr(/:locale)/clients(.:format)         pages#clients {:locale=>"fr"}
 clients_en GET  (/:locale)/clients(.:format)            pages#clients {:locale=>"en"}
 contact_fr GET  /fr(/:locale)/contactez_nous(.:format)  pages#contact {:locale=>"fr"}
 contact_en GET  (/:locale)/contact(.:format)            pages#contact {:locale=>"en"}
  manage_fr GET  /fr(/:locale)/gerer(.:format)           pages#manage {:locale=>"fr"}
  manage_en GET  (/:locale)/manage(.:format)             pages#manage {:locale=>"en"}
   media_fr GET  /fr(/:locale)/edition(.:format)         pages#media {:locale=>"fr"}
   media_en GET  (/:locale)/media(.:format)              pages#media {:locale=>"en"}
 privacy_fr GET  /fr(/:locale)/confidentialite(.:format) pages#privacy {:locale=>"fr"}
 privacy_en GET  (/:locale)/privacy(.:format)            pages#privacy {:locale=>"en"}
  system_fr GET  /fr(/:locale)/systeme(.:format)         pages#system {:locale=>"fr"}
  system_en GET  (/:locale)/system(.:format)             pages#system {:locale=>"en"}
testpage_fr GET  /fr(/:locale)/testpage(.:format)        pages#testpage {:locale=>"fr"}
testpage_en GET  (/:locale)/testpage(.:format)           pages#testpage {:locale=>"en"}
       root GET  /                                       pages#home
locale_root GET  /:locale(.:format)                      pages#home

我遇到问题的唯一途径是localize do语句中的路径。当我在 en fr 之间切换时,它们包括我所有可用的语言环境。

我查看了GitHub中的配置。但我没有看到任何看起来可以解决我的问题。对配置的描述没有明确说明,这让我觉得它们会解决我的问题。

1 个答案:

答案 0 :(得分:1)

我有同样的问题......解决方案。

将文件..\config\routes.rb更改为:

MyRailsApp::Application.routes.draw do  

    localized do

      match "/about",      to: "pages#about", via: "get"
      match "/clients",    to: "pages#clients", via: "get"
      match "/contact",    to: "pages#contact", via: "get"
      match "/manage",     to: "pages#manage", via: "get"
      match "/media",      to: "pages#media", via: "get"
      match "/privacy",    to: "pages#privacy", via: "get"
      match "/system",     to: "pages#system", via: "get"
      match "/testpage",   to: "pages#testpage", via: "get"
      match "/",           to: "pages#home", via: "get", :as => :locale_root
    end
    root to: "pages#home", via: :get

end

您只需要移除scope周围的localized

还要将以下内容添加到application.rb文件中。

RouteTranslator.config do |config|
  config.force_locale = true
  config.locale_param_key = :locale
end

有了这个,您将以以下路线结束:

about_fr GET  /fr/qui_sommes_nous(.:format) pages#about {:locale=>"fr"}
about_en GET  /en/about(.:format)           pages#about {:locale=>"en"}
...
root_fr GET  /fr                                pages#home {:locale=>"fr"}
root_en GET  /en                                pages#home {:locale=>"en"}
root    GET  /                                  pages#home