1

below is an example of menu in french, I will also have it in another five more languages:

<a href="/<%= @lang %>/">Jeux</a>
<a href="/<%= @lang %>/get-the-player">Obtenir le lecteur</a>
<a href="/<%= @lang %>/about">À propos</a>
<a href="/<%= @lang %>/faq">FAQ</a>
<a href="/<%= @lang %>/contact">Contact</a>

at the moment the menu are in a relevant pages (.html.erb), by using the code listed below

<%= content_for :menu do %>    
    <a href="/<%= @lang %>/">Jeux</a>
    <a href="/<%= @lang %>/get-the-player">Obtenir le lecteur</a>
    <a href="/<%= @lang %>/about">À propos</a>
    <a href="/<%= @lang %>/faq">FAQ</a>
    <a href="/<%= @lang %>/contact">Contact</a>
<% end %>

once I have done above I have got the reference on the application.html.erb

<%= yield :menu %>

it can be done exactly the same way for other languages, however, is there a way where I could avoid the repetition of the code. perhaps makes the controller to go and find for it and render it in exact place ? Help please,

thanks in advance!

===========================================================================

    **CHECK BELOW FOR SOLUTIONS THAT I HAVE PROVIDED**
prusswan
  • 6,853
  • 4
  • 40
  • 61
RajG
  • 832
  • 2
  • 15
  • 26

3 Answers3

3

Have a look at the built in internationalization (i18n) API.

Edit: Another way is to use localized views, as described in the ruby on rails i18n guides. For example, you could name your files index.fr.html.erb and index.en.html.erb. Then, depending on your I18n.locale settings, the appropriate view is rendered.

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • I have got a sample working for that! but any other solutions ? – RajG Nov 06 '12 at 16:35
  • Why would you want to use somerhing else? The i18n API is well tested, maintainable, extendable and as easy as it gets. – Patrick Oscity Nov 06 '12 at 18:31
  • thank you very much for you effort, currently I am editing quite a large site, your input is much appreciated. – RajG Nov 07 '12 at 14:37
  • **@padde** in the end I have utilized internationalization API, much easier :) – RajG Nov 15 '12 at 11:35
  • **@padde** if possible could you help me out on this [question](http://stackoverflow.com/questions/13310812/set-locale-automatically-in-ruby-on-rails) please? – RajG Nov 15 '12 at 12:07
1

If you have a lot of this to do on your site, I suggest you take a look at Google Closure Templates (Soy). It has a localization framework that you can use to accomplish that.

If you use it then you write a Soy template to generate the menus and (other parts of the site) and use the localization capabilities to display the appropriate language for the user.

Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78
0

This is what needs to be done. On my _header.html.erb (or application.html.erb depending on how you have done it), I have created an if statement like this listed below:

<% if params[:lang]=="es" %>
    <a href="/<%= @lang %>/">Jeux</a>
    <a href="/<%= @lang %>/get-the-player">Obtenir le lecteur</a>
    ..........
    ..........
<% end %>


<% if params[:lang]=="en" %>
    <a href="/<%= @lang %>/">Games</a>
    <a href="/<%= @lang %>/get-the-player">Get the Player</a>
    ..........
    ..........
<% end %>

By this you can manipulate to have more if I/you wish. Yes Guys you could do this with internationalization API, however this time I am editing a big application which could kill more time plus makes it less productive. So The above source code could be an alternative way. :). Thank You guys for all your input on this one.

RajG
  • 832
  • 2
  • 15
  • 26
  • you (or another developer) will be thankful, when he dives into the code in a year or so, and finds that your application sticks to the standards instead of trying to re-invent the wheel. Using i18n is easy. And above all: it's maintainable. – Patrick Oscity Nov 07 '12 at 15:10
  • What if you decided to add a `class` attribute to your `a` tags? You would have to do this stupid thing for _every single language_ over and over again. – Patrick Oscity Nov 07 '12 at 15:12
  • If you really don't want to use the i18n api, then at least split the different laguages into separate files and name them `_header.es.html`, `_header.fr.html`, and so on. Rails will figure out the rest for you. – Patrick Oscity Nov 07 '12 at 15:13
  • Oh and please do not extend your question inside an answer, place additional info below your original question and mark it as edit. – Patrick Oscity Nov 07 '12 at 15:15
  • i already have class attribute for them, i just did not decide to post them here, not relevant! I do not really see a point of splitting it into different headers. Also I implemented 'i18ni' but the base_page processor that i have under the page_processor controller did not react to different locale (a whole lot of different mess there). I know it is easier to implement il8n and rightly so I have implemented for another application. but at this time I do no really have time :( to work around it. – RajG Nov 07 '12 at 15:30
  • also can you direct me to any examples of native ruby running on AWS Elastic Bean stalk as AWS seems to support native ruby ?? – RajG Nov 07 '12 at 15:32
  • Why would you even store the same value in `params[:lang]` and `@lang`? This is bad style. – Patrick Oscity Nov 07 '12 at 15:32
  • Sorry, i have no experience with AWS, i always use Heroku. – Patrick Oscity Nov 07 '12 at 15:32
  • Ok, how do you set the language in the first place? – Patrick Oscity Nov 07 '12 at 15:33
  • by default the site should go to lang='en', so in index method/action @lang=en is declared. But this will be changed to where header would automatically decide which locale it is. look at this website antixgames.com this is project i am working on ;). – RajG Nov 07 '12 at 15:37
  • if you wish you could download the AGP plugins on your mobile or in your pc to play any game, unlimited trail ! – RajG Nov 07 '12 at 15:40
  • @padde in the end I have utilized internationalization API, much easier :) – RajG Nov 15 '12 at 11:35