Country codes in the i18n localization library for Rails

The powerful Ruby on Rails Localization library is truly awesome. We have discussed in other ocassions about it and also about ways of localizing the javascripts .

Today we have another item to discuss. It is about geographic/country specific versions of a language that you would like to offer to your users.

In this question, a user is asking how to manage codes such as en-US, en-GB, es-ES, i.e. language codes that include also a geographic code of the language variation that is used in a region or country:

Set the locale you want:

I18n.locale = ‘en_US’

And use fallbacks to sort out the specific differences you need to cover

config.i18n.default_locale = ‘en’
config.i18n.fallbacks = {
‘en_US’ => ‘en’,
‘en_GB’ => ‘en’,
‘de_DE’ => ‘de’,
‘de’ => ‘en’

The issue may be if you are using urls with the locale in order to manage the different versions. Because in a url you should not use underscores (even if Wikipedia does and quite successfully!!), see why you should not in this article:

So the urls would not be as clean. Maybe a solution is to use /en/ for all english content urls and underneath keep the geographic specific info en_EN or en_US.

Any suggestion? thanks for participating

How to translate javascript strings in Rails – .js localization

Hi there!

Today i have achieved the configuration of a great gem that will help you to translate your javascript strings.

It is called

I was following the instructions but a couple of things did not work. Both in application.rb

1. Library

In the instructions it says to add the following line:

config.middleware.use I18n::JS::Middleware

But in my case it did not work. I searched and found this alternative:

config.middleware.use SimplesIdeias::I18n::Middleware

(Thanks for the hint here:

2. Configuration

I had to change this line

config.assets.initialize_on_precompile = false

and set it to true

config.assets.initialize_on_precompile = true

3. Inform js of the current locale
Another important thing I had to do was to add a couple of lines to the application.html.erb

I18n.defaultLocale = "";
I18n.locale = "";

And then it started working nicely.

As an example, in the .js file I put

var alertMsg = I18n.t('posts.alert');

And that corresponds to

alert: "Alert message"

How this works internally I am not sure. I have realised that the key file is translations.js that is automatically created under public/assets

Translations.js gets automatically updated, and you can also update it manually with:

$ rake i18n:js:export

I have also evidenced that the Rails cache can spoil the party. So if you notice erratic behaviour do clear the cache:

rake tmp:clear (I have had similar issues in the past…)

Hope it helped! Thanks to Nando Vieira for the amazing work!

Translating WordPress themes

There are a few things to take into account:

1. Gettext the strings:
WordPress uses an opensource solution called GetText, that has a php version included with the WordPress package

For example:

<?php printf( __( 'View all posts by %s ', 'twentyeleven' ), get_the_author() ); ?>

A complete guide can be found here:

2. Generating the .po file

Name it like the language you are using. For example, Spanish from Spain is es_ES.po. Put all the strings there. Following the example

#: content.php:57
msgid "View all posts by %s "
msgstr "Ver todas las entradas por %s "

3. Put the .mo file into the /languages directory of your theme

Following the example,

Like in many other occasions, the best thing may be to follow an example. So, the guys at WordPress with the help of the community have translated the included themes such as TwentyTwelve here:

Thanks to Roger for this post in StackOverflow:

Textmate 2 alpha problem with .yml yaml file

I have experienced problems with .yml files used in localization files for Rails. The files are somehow corrupt, because Textmate 2 is saving the file with weird codes for spaces/tabs. i have not been able to identify exactly why, but I discovered that the problem was being caused by Textmate because I tried with TextWrangler and it got solved.

Bug info:

TextMate version 2.0-alpha.9443.


 ~/Desktop% irb
    2.0.0-p247 :001 > require ‘YAML’
     => true 
    2.0.0-p247 :002 > YAML.load_file(‘en.yml’)
    Psych::SyntaxError: (en.yml): found a tab character that violate intendation while scanning a plain scalar at line 134 column 13