How to localize existing content in Rails 3? - ruby-on-rails

I have set up a simple Rails app based on Post, Comment example from the default documentation. -> http://edgeguides.rubyonrails.org/getting_started.html
What would be the appropriate way of localizing existing content from these Posts ?
If I assume that Post has a :title and "content, and I would like this content to be entered not only in English, but also in German and Dutch, then how would I achieve this if I want the end result to look similar to this :
http://0.0.0.0:3000/en/posts.json to get all posts in English and respectively,
http://0.0.0.0:3000/de/posts.json for German?
I havent found a gem yet, that would support such multilangual content.
I have seen doe, many existing ways to do so, mostly in applications like Wordpress or Drupal, which are not rails based.
I guess the preferred way would be either storing EN and DE posts in seperate tables (but then how to add new languages?), or to place all translations within the same database table, but seperate translations in a different way, but how to get them easily out in json in the right language?
I am really confused and puzzled why such a "common" problem, is not solved in Rails by default, or maybe I am simply looking in the wrong place.

Globalize3 will do what you want to do. It stores all translations for a particular model in a separate table, with a locale column identifying what the language of a particular translation is. This makes it possible to add new languages without having to migrate the database, so the approach is very scalable.
There are other gems for translating content as well, e.g. Traco is one that I know of, but this one stores the translations in the same table as the model, so when you add new languages you have to migrate each model table. I personally prefer the Globalize3 approach, although it makes the gem itself more complicated internally.

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

Translating entries in the database rails

In our application users can create a set of categories. Those categories each contain products. On the website customers can then see the categories - currently only in one language.
We would like to add translations so that the customers can view the data in different languages. This would mean that the the user has to be able to add translations to the actual data.
Based on this question: Rails I18n via database column I should add a couple of database columns for each locale.
Has anyone solved a similar problem?
I have used globalize3 numerous times and always liked it. Maybe it can help you too.
I had something similar kind of requirement where this episode helped me . I was not changing the language but i wanted to externalize some of the currency parameters as the currency per country changes. Here is my sample application code

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