Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internationalization of static pages with Rails

I feel like I'm missing something really simple and I keep spinning my wheels on this problem.

I currently have internationalization working throughout my app. The translations work and the routes work perfectly. At least, most of the site works with the exception of the routes to my two static pages, my "About" and "FAQ" pages.

Every other link throughout the app points to the proper localized route. For example if I select "french" as my language, links point to the appropriate "(/:locale)/controller(.:format)." However, despite the changes I make throughout the app my links for the "About" and "FAQ" refuse to point to "../fr/static/about" and always point to "/static/about."

To make matters stranger, when I run rake routes I see: "GET (/:locale)/static/:permalink(.:format) pages#show {:locale=>/en|fr/}"

and when I manually type in "../fr/static/about" the page translates perfectly.

My Routes file:

 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

My 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

and My Pages 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

I'd be very grateful for any help ... it's just been one of those days.

Edit: Thanks to Terry for jogging me to include views.

<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>
like image 552
Gavin Avatar asked Aug 01 '13 19:08

Gavin


2 Answers

You should give a name to your route by "as":

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

And then in view build link with routing helpers (route_name_path):

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

This helper automatically add current locale to path.

To get list of all routes with names use rake routes console command.

Good luck!

like image 82
Ivan Schneider Avatar answered Oct 06 '22 15:10

Ivan Schneider


Interesting. Ended up solving this problem by using url_for in the view:

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

I've personally never used url_for, so I'm not sure if this is the most graceful solution, however I'm happy that it works!

Edit: Actually going back to code, this produced a really bad result. It worked from root_url and most other paths, however if the current path was /fr/static/about_us or /fr/static/faq the above lines actually called "/fr/static/static/about_us" or "/fr/static/static/faq" - rectified with:

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

Moving on to another problem now, but if anyone has a more graceful way of doing this feel free to contribute. Will probably re-examine this problem early next week.

like image 25
Gavin Avatar answered Oct 06 '22 16:10

Gavin