我觉得我错过了一些非常简单的东西,我一直在解决这个问题。
我目前在我的应用程序中进行了国际化。翻译工作和路线工作完美。至少,除了到我的两个静态页面(“关于”和“常见问题”页面)的路由之外,大多数站点都可以正常工作。
整个应用程序中的每个其他链接都指向正确的本地化路线。例如,如果我选择“法语”作为我的语言,则链接指向相应的“(/:locale)/controller(.:format)”。然而,尽管我在整个应用程序中进行了更改,但我的“关于”和“常见问题解答”链接拒绝指向“../fr/static/about”并始终指向“/static/about”。
更奇怪的是,当我运行 rake 路线时,我看到:
"GET (/:locale)/static/:permalink(.:format) pages#show {:locale=>/en|fr/}"
当我手动输入“../fr/static/about”时,页面翻译得很好。
我的路线文件:
devise_for :users
scope "(:locale)", :locale => /en|fr/ do
get 'static/:permalink', :controller => 'pages', :action => 'show'
resources :places, only: [:index, :show, :destroy]
resources :homes, only: [:index, :show]
match '/:locale' => 'places#index'
get '/'=>'places#index',:as=>"root"
end
我的应用程序 Controller :
before_filter :set_locale
def set_locale
I18n.locale=params[:locale]||I18n.default_locale
end
def default_url_options(options={})
logger.debug "default_url_options is passed options: #{options.inspect}\n"
{ :locale => I18n.locale }
end
和我的页面 Controller :
class PagesController < ApplicationController
before_filter :validate_page
PAGES = ['about_us', 'faq']
def show
render params[:permalink]
end
def validate_page
redirect_to :status => 404 unless PAGES.include?(params[:permalink])
end
end
我会非常感谢任何帮助......这只是那些日子之一。
编辑:感谢特里慢跑我包括意见。
<div class="container-fluid nav-collapse">
<ul class="nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><%= t(:'navbar.about') %><b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%=link_to t(:'navbar.about_us'), "/static/about_us"%></li>
<li><%=link_to t(:'navbar.faq'), "/static/faq"%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
</ul>
</li>
最佳答案
您应该通过“as”为您的路线命名:
scope "(:locale)", :locale => /en|fr/ do
get 'static/:permalink', to: 'pages#show', as: :static
然后在 View 中使用路由助手( route_name _path)构建链接:
<li><%=link_to t('navbar.about_us'), static_path(permalink: "about_us") %></li>
此帮助程序自动将当前语言环境添加到路径。
要获取带有名称的所有路线的列表,请使用 rake 路控制台命令。
祝你好运!
关于ruby-on-rails - 使用 Rails 国际化静态页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18002924/