我能夠在需要語言環境時成功使用route_translator gem。當我在兩個語言環境之間切換時(en:fr),點擊任何鏈接並顯示正確的視圖和鏈接翻譯都沒有問題。如何使用route_translator gem並且不需要Locale?
這是我的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
區域設置邏輯缺省的語言環境來烯如果沒有語言環境中找到。
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
我遇到問題的唯一途徑就是本地化do語句中的問題。當我在en和fr之間切換時,它們包括我所有可用的語言環境。
我看了一下GitHub中的配置。但我還沒有看到任何看起來可以解決我的問題的東西。對配置的描述沒有說清楚,這讓我認爲他們會解決我的問題。
工作太棒了!謝謝!現在我想看看我是否可以使用它,並且不需要默認en的區域設置。 –