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.
Related
We need to start delivering dynamic content in multiple languages with our different apps.
There are two types of localisation that should be considered: UI localisation and Content localisation. UI localisation is the language the interface is delivered in (static text) while Content localisation is the language content is viewed/edited (Rails model)
We basically have 3 type of apps
Mobile apps - hits an API endpoints for CRUD operations
Client Web app - built with backbone.js so also using API endpoints for CRUD operations via backbone models/collections
Admin web app - built using activeadmin (https://github.com/activeadmin/activeadmin)
I should note we are using Rails 3.2, ActiveAdmin 0.6.6 (upgrading needed but not priority :))
To localise the content, we are looking the globalize gem (https://github.com/globalize/globalize)
There doesn't seem to be any issues using globalize when interfacing with the API endpoints. We can use the Accept-Language header and set the I18n.locale property in a before_filter and everything seems to work fine. This means the Mobile apps and Client Web app should be covered.
For ActiveAdmin, this becomes a bit more difficult. For the ActiveAdmin app, we would like to deliver the UI static text in the user's preferred language while allowing them to switch between languages for the content.
The globalize gem leverages the I18n.locale property to determine which locale to update/read for the content translations. Since ActiveAdmin is not using AJAX, setting I18n.locale will also affect the UI static text.
At the moment, this is not a big deal as we only have one locale for the UI (config/locales only contain en yml files). This means we can use the method described at https://github.com/activeadmin/activeadmin/wiki/Specifying-locale and set the I18n.locale property to update the correct dynamic content, via globalize, while the UI interface will continue to be delivered in English
However, thinking to the future, where we potentially add additional languages for the UI, I'm looking for ideas of how to keep the UI locale and the content locale separate. Other languages, such as Java and C#, do this with different properties on their I18n equivalent library but Rails doesn't seem to embrace this
Thoughts?
Thanks in advance
after further investigation, I ended up using activeadmin-globalize gem. Initially, I didn't think it was compatible with the globalize gem based on the documentation but it is
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
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.
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
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').