现在我的routes.rb看起来像这样:
scope '(:locale)', locale: /en/ do
get I18n.t('routes.pages.about'), to: 'pages#about', as: 'about'
get 'media', to: 'pages#media', as: 'media'
get 'career', to: 'pages#career', as: 'career'
get 'how_it_works', to: 'pages#works', as: 'works'
get 'privacy_policy', to: 'pages#privacy_policy', as: 'privacy_policy'
get 'contact', to: 'pages#contact', as: 'contact'
get 'publications', to: 'pages#publications', as: 'publications'
get 'our_team', to: 'pages#our_team', as: 'our_team'
get 'partners', to: 'pages#partners', as: 'partners'
get 'blog', to: 'posts#index', as: 'blog'
get 'map', to: 'pages#map', as: 'map'
get 'association', to: 'pages#association', as: 'association'
get 'questions_and_answers', to: 'pages#questions_and_answers', as: 'questions_and_answers'
resources :posts, only: [:show]
get '/users/:uuid', to: 'users#public_profile'
post 'partners/:partner', to: 'pages#partners_partial', as: 'partners_partial'
end
我在'config/locales'中创建了routes.yml文件
pl:
routes:
pages:
about: "o-nas"
media: ""
career: ""
how_it_works: ""
privacy_policy: ""
contact: ""
publications: ""
our_team: ""
partners: ""
blog: ""
map: ""
association: ""
question_and_answers: ""
en:
routes:
pages:
about: "about"
media: ""
career: ""
how_it_works: ""
privacy_policy: ""
contact: ""
publications: ""
our_team: ""
partners: ""
blog: ""
map: ""
association: ""
question_and_answers: ""
我的application_controller.rb:
def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
end
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
我的切换语言按钮:
-if I18n.locale == :pl
= link_to "/en#{request.fullpath}", { :lang=>'en'} do
= image_tag'/england-flag.ico', size: '25x25', class: 'rounded-xl shadow', alt: 'Switch to English language'
-else
= link_to "/#{request.fullpath.split('/en/')[1]}", { :lang=>'pl'} do
= image_tag'/poland-flag.ico', size: '25x25', class: 'rounded-xl shadow', alt: 'Przełącz na język Polski'
正如你所看到的,我试图本地化的URL为关于页面,但...有时它显示我“o-nas”,有时它显示我“about”(即使我没有改变语言)。我知道有像'friendly_id-globalize'这样的宝石,我甚至尝试了这个,但它不是我想要的。是否有可能动态改变路线?
1条答案
按热度按时间8ulbf1ek1#
我想你应该把你的路线文件行改为
你错过了波兰语。