Internationalizing Static Pages with Rails

It seems to me that I missed something very simple, and I continue to spin my wheels on this issue.

Currently, my program is running internationalization. Translation work and routes work great. 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 in the application points to the correct localized route. For example, if I choose "French" as my language, the links point to the corresponding "(/:locale)/controller(.:format)". However, despite the changes that I made in the application, my links for "O" and "FAQ" do not point to "../fr/static/about" and always point to "/ static / about."

To change the situation when I run rake routes, I see: "GET (/:locale)/static/:permalink(.:format) of the page # show {: locale => / en | fr /}"

and when I manually type "../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 would be very grateful for any help ... it was only one of those days.

Edit: Thanks Terry for turning on the submissions.

 <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> 
+6
source share
2 answers

You must provide a name for your how-to route:

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

And then connect the assembly link with the routing helpers ( route_name _path):

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

This helper automatically adds the current locale to the path.

For a list of all routes with names, use the rake routes console command.

Good luck

+4
source

Interesting. Ended up solving this problem with 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 personally have never used url_for, so I'm not sure if this is the most elegant solution, however I'm glad it works!

Edit: Actually, returning to the code, this led to very poor results. It worked from root_url and most other paths, however if the current path was / fr / static / about _us or / fr / static / faq, the lines above were actually called "/ fr / static / static / about_us" or "/ fr / static / static / faq "- fixed 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> 

Let's move on to another problem now, but if anyone has a more graceful way to do this, feel free to contribute. Probably review this problem early next week.

+1
source

Source: https://habr.com/ru/post/950847/


All Articles