簡體   English   中英

如何使用route_translator gem而不要求使用語言環境?

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

當我需要語言環境時,我能夠成功使用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語句中的路由。 當我在enfr之間切換時,它們包括我所有可用的語言環境。

我查看了GitHub中的配置。 但我沒有看到任何可以解決我問題的方法。 這些配置的描述並沒有明確說明,這使我認為它們可以解決我的問題。

我有同樣的問題...解決方案。

將文件..\\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

您只需要刪除localized周圍的scope

還將以下內容添加到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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM