5

我覺得我錯過了一些非常簡單的事情,並且我一直在關注這個問題。使用Rails進行靜態頁面的國際化

我目前的國際化工作貫穿我的應用程序。翻譯工作和路線完美地工作。至少,除了通往我的兩個靜態頁面(我的「關於」和「常見問題」頁面)的路線以外,大多數網站都可以工作。

整個應用程序的其他所有鏈接指向適當的本地化路線。例如,如果我選擇「法語」作爲我的語言,鏈接指向適當的「(/:locale)/controller(.:format)」。「但是,儘管我在整個應用程序中進行了更改,但「關於」和「常見問題解答」的鏈接拒絕指向「../fr/static/about」,並始終指向「/ static/about」。

爲了讓事情變得更奇怪,當我運行耙路線時,我看到: 「GET(/:locale)/static/:permalink(.:format)pages#show {:locale =>/en | fr /}」

當我手動輸入「../fr/static/about」時,頁面翻譯完美。

我的routes文件:

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 

我的ApplicationController:

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 

和我的頁面控制器:

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> 
+0

呈現鏈接的視圖如何? –

回答

3

你應該給一個名稱,路線由 「是」:

scope "(:locale)", :locale => /en|fr/ do 
    get 'static/:permalink', to: 'pages#show', as: :static 

而且具有路由傭工(ROUTE_NAME _path),然後在視圖構建鏈接:自動

<li><%=link_to t('navbar.about_us'), static_path(permalink: "about_us") %></li> 

這個助手將當前語言環境添加到路徑。

獲取所有名稱爲耙路線的路線列表控制檯命令。

祝你好運!

+0

這很完美 - 非常感謝你的回答! 抱歉花了這麼長時間才批准,今天下午剛剛嘗試了改變。 – Gavin

1

有趣。最終通過在視圖中使用url_for解決這個問題:

<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'), url_for('static/about_us')%></li> 
     <li><%=link_to t(:'navbar.faq'),  url_for('static/faq')%></li> 
     <li><%=link_to t(:'navbar.blog'),  '#' %></li> 
     </ul> 
    </li> 

我個人從來沒有使用url_for,所以我不知道這是最優雅的解決方案,但是我很高興,它的工作原理!

編輯: 實際上回到代碼,這產生了一個非常糟糕的結果。它從root_url和大多數其他路徑運行,但是如果當前路徑是/ fr/static/about_us或/ fr/static/faq,上面的行實際上稱爲「/ fr/static/static/about_us」或「/ fr/static /靜態/ FAQ」 - 與修正:

<ul class="dropdown-menu"> 
      <% if request.fullpath == '/fr/static/about_us' or request.fullpath == '/fr/static/faq' %> 
      <li><%=link_to t(:'navbar.about_us'), url_for('/fr/static/about_us')%></li> 
      <li><%=link_to t(:'navbar.faq'),  url_for('/fr/static/faq')%></li> 
      <li><%=link_to t(:'navbar.blog'),  '#' %></li> 
      <% elsif request.fullpath == '/en/static/about_us' or request.fullpath == '/en/static/faq' %> 
      <li><%=link_to t(:'navbar.about_us'), url_for('/en/static/about_us')%></li> 
      <li><%=link_to t(:'navbar.faq'),  url_for('/en/static/faq')%></li> 
      <li><%=link_to t(:'navbar.blog'),  '#' %></li> 
      <% elsif request.fullpath == '/static/about_us' or request.fullpath == '/static/faq' %> 
      <li><%=link_to t(:'navbar.about_us'), url_for('/en/static/about_us')%></li> 
      <li><%=link_to t(:'navbar.faq'),  url_for('/en/static/faq')%></li> 
      <li><%=link_to t(:'navbar.blog'),  '#' %></li> 
      <% else %> 
      <li><%=link_to t(:'navbar.about_us'), url_for('static/about_us')%></li> 
      <li><%=link_to t(:'navbar.faq'),  url_for('static/faq')%></li> 
      <li><%=link_to t(:'navbar.blog'),  '#' %></li> 
      <%end%> 
      </ul> 

移動到另一個問題了,但如果任何人有這樣隨意貢獻更優雅的方式。下週初可能會重新檢查這個問題。