简体   繁体   中英

How Do I Use the route_translator gem and not require the Locale?

I'm able to successfully use the route_translator gem when I require a locale. When I change between the two locales I have (en:fr) I have no problems clicking any of the links and displaying the correct views and link translations.

Here is my config/routes.rb file where my routes work as expected.

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

Here is the locale logic in app/controllers/application_controller.rb which defaults the locale to en if no locale is found.

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

The problem comes when I change the scope statement to the second one. I can display the locale_root path and switch locales with no problem. If I display another view in en I can switch the locale to fr and successfully display the view in French. However when I attempt to switch back to en the route drawn includes /fr/en instead of /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

The only routes that I have problems with are those within the localize do statement. They include all my available locales when I switch between en and fr .

I have looked at the configurations in GitHub. but I have not seen anything that looks like could solve my problem. The descriptions of the configurations don't say anything clearly that makes me think they will solve my problem.

I had the same problem... the solution.

Change your file ..\\config\\routes.rb to this:

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

You just need to remove the scope around the localized .

Also add the following to your application.rb file.

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

With this you will end with the following routes:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM