如何使用 route_translator gem 而不需要语言环境?
How Do I Use the route_translator gem and not require the Locale?
当我需要语言环境时,我能够成功地使用 route_translator gem。当我在两个语言环境之间切换时,我 (en:fr) 单击任何 link 并显示正确的视图和 link 翻译都没有问题。
这是我的 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 中的配置。但我还没有看到任何看起来可以解决我的问题的东西。配置的描述没有说清楚任何让我认为它们会解决我的问题的内容。
我遇到了同样的问题...解决方案。
将文件 ..\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
当我需要语言环境时,我能够成功地使用 route_translator gem。当我在两个语言环境之间切换时,我 (en:fr) 单击任何 link 并显示正确的视图和 link 翻译都没有问题。
这是我的 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 中的配置。但我还没有看到任何看起来可以解决我的问题的东西。配置的描述没有说清楚任何让我认为它们会解决我的问题的内容。
我遇到了同样的问题...解决方案。
将文件 ..\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