Internationalization and localization in Rails. - ruby-on-rails

I am trying to wrap my head around what localization means here. I've been reading these docs:
Internationalization is a complex problem. Natural languages differ in
so many ways (e.g. in pluralization rules) that it is hard to provide
tools for solving all problems at once. For that reason the Rails I18n
API focuses on:
providing support for English and similar languages out of the box
making it easy to customize and extend everything for other languages
As part of this solution, every static string in the Rails framework -
e.g. Active Record validation messages, time and date formats - has
been internationalized, so localization of a Rails application means
"over-riding" these defaults.
What does that mean? What does localization mean here?
I think this makes sense:
The default en.yml locale in this directory contains a sample pair of
translation strings:
en: hello: "Hello world" This means, that in the :en locale, the key
hello will map to the Hello world string. Every string inside Rails is
internationalized in this way, see for instance Active Model
validation messages in the activemodel/lib/active_model/locale/en.yml
file or time and date formats in the
activesupport/lib/active_support/locale/en.yml file. You can use YAML
or standard Ruby Hashes to store translations in the default (Simple)
backend.
The I18n library will use English as a default locale, i.e. if you
don't set a different locale, :en will be used for looking up
translations.

In the bolded statement,
validations have been "internationalized" (read: supports customization for localisation)
so localization of a Rails applica.... (read: the activity of localising your application is by means of overriding the default values provided by the built in internationalization functionality.)
Will revise my answer if it doesn't help.

Related

Rails I18n overlay for user

We're using Rails with I18n for a web application running for users in multiple countries. To handle different languages we're setting the locale based on the TLD. Now, we have an important customer in one country that requires custom translations. To handle this, we would like to overlay a few translations for that specific customer.
Right now we're loading the locales as usual, but also add a folder to the I18n.load_path if the session belongs to the specific customer and then run I18n.backend.reload!. This works well, and we only have to add the keys that needs custom translations for the customer in question. What's less great is that this changes the translations for everybody using the same language.
What's the recommended way to do this in Rails?
I sorted this out by adding a custom locale for which I added a fallback to the language in question. Works well.

Creating a Rails "localisation" that is not region or language specific?

I'm quite familiar with how Rails handles internationalisation with regards multiple languages and formatting of dates, currencies and alike.
Extending this idea, is there way to do this with custom sets of vocabulary that are not different languages or regional settings.
For example, in an app which is used by orgnisations, you might have tools to allow "employees" to communicate with their "managers". Is there a way to substitute the vocabulary for, say, "students" and "teachers", on, an organisation-by-organisation basis, while leaving the model, controller and view code all intact?
You can just use standard i18n localisation.
The Rails guide even gives examples for adding the Pirate language: http://guides.rubyonrails.org/i18n.html#adding-translations
Create your per-organisation languages and set the locale appropriately.
Assuming you need your i18n translations to be a little more dynamic, you may wish to take a look at one of the DB backend gems for i18n. Such as this one: https://github.com/svenfuchs/i18n-active_record

Best practice in making Rails applications multilingual

Hello everyone :D I've just started learning Rails and currently I have one concern.
I am building a Rails web site which needs to be translated to 4 languages. What would be the most practical and convenient method to do it?
I've read the main goal would be to make separate folders for each language and copy all the views for each language. But I would still have notice messages on English inside my controller so how would I handle that. Routes are also my concern. Should I have 4 different routes for 4 different translated Views.
What do you recommend to handle this problem? I didn't find anything concrete online.
Thank you for your suggestions!
for your notice messages you can do
def create
if user.save
flash[:notice] = t(:user_was_successfully_created)
redirect_to users_users_path
else
render :new
end
end
you should not have 4 different routs
Rails Internationalization (I18n) API
take a look at this link http://guides.rubyonrails.org/i18n.html
Rails Internationalization (I18n) API
The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.
The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits.1
So, in the process of internationalizing your Rails application you have to:
Ensure you have support for i18n.
Tell Rails where to find locale dictionaries.
Tell Rails how to set, preserve and switch locales.
In the process of localizing your application you'll probably want to do the following three things:
Replace or supplement Rails' default locale — e.g. date and time formats, month names, Active Record model names, etc.
Abstract strings in your application into keyed dictionaries — e.g. flash messages, static text in your views, etc.
Store the resulting dictionaries somewhere.
This guide will walk you through the I18n API and contains a tutorial on how to internationalize a Rails application from the start.
After reading this guide, you will know:

Is it possible to handle text translations for data stored in database tables?

I am using Ruby on Rails 3.1.0 and I would like to know if it is possible to handle text translations for data stored in database tables. Is it possible? If so, how?
For example, if I have a database table column named Title and in that I have a record with the Title value set\stored to "car", I would like to show
the text "automobile" for italian people;
the text "auto" for german people;
and so on...
For user generated content it's best to store the language with the content.
You can then have your users create "copies" of this data with a different language reference.
Depending on how much data you need to change per table a different gems might be suitable for your needs. I18n gem is NOT a good choice here, as it's meant for static content. See: https://www.ruby-toolbox.com/categories/i18n
Ruby can help you translate the words from one language to another, but only by providing hashes and structures or code algorithms that allow you to do lookups. By itself it knows nothing of the meaning of words, and cannot translate words.
For your purpose, you should build a table of translations, with the primary language words in a primary field, and their translations in secondary fields, one field for each language. Then you can quickly look up all the words for a form in German, or Italian, and substitute them into the form.
You can setup your rails application for internationalization using Rails Internationalization (i18n) API. You would need to pass and set the locale. The locale files would reside in config/locales. As mentioned by the tin Man, the locale file is a key:value file. Here you can override custom rails defaults.
Locale can be set from params and also from domain name. The Rails guide is actually the best place to start with in my opinion.
http://guides.rubyonrails.org/i18n.html
As already mentioned in the other answers, you'll want to use the rails i18n functionality to get this to work.
There are however alternative ways of storing your translations. This railscast describes how to set up a redis-based translation backend, as well as links to a whole bunch of different backends.
If you just want to store your translations in a database using ActiveRecord, I'd suggest you have a look at i18n-active_record.
With these plugins you can easily store your translations in the database, and have your application look them up for you using I18n.t('some_translation_key').

Implementing globalization in Rails

While I have experience developing Rails apps in English, I am a blank slate when it comes to handling globalization, so please don't shoot me in the head if my question 'doesn't make sense' :)
I have been asked to add multi language feature to a part of a Rails app that I am working on. Initially its only going to be 2 languages, French and German. The content that will be translated (which is in English now) is rendered using partials at the moment hence I am getting a bit inclined on creating partials with different languages and then based on the users language selection call the relevant partial - Would you recommend this approach?
Although it seems a heavy weight solution for this particular purpose, but I am also looking at the Rails Globalize plugin. This seems to appeal if I look at the long term gains, something like what if later I am asked to translate the entire app.
Any insights on what would be a proper structured approach to handle globalization in Rails?
Many Thanks
Have you had a look at the i18n (internationalization) API that is in Rails itself as of 2.2? It makes it easy to store your language translation files as .yml files, so a fr.yml and a de.yml or whatever. It also steps through different approaches of making languages accessible via browser prefs, or URLs, subdomains, etc. In your HTML template files you use symbols to specify the keys for the string you've translated. At least in my basic usage and testing, it was very easy to work with.
I preffer using translator instead of Rail's i18n, since the former handles "scoping of translation" automatically. I recommend you give it a look.
This is a great article on how to use Ruby's GetText to localise you Rails App.

Resources