#338 Globalize3
Rails has great internationalization (I18n) support making it easy to translate static text into other languages, but how do we translate database content? Learn how using Globalize 3 in this episode.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
If you don't have a lot of languages but just two or three or so, you could keep things simpler with multiple columns in the same table. We wrote Traco for that recently, inspired by an earlier plugin called translatable_columns.
What about tr8n ?
https://github.com/berk/tr8n
We fork this gem and rewrite to more flexible globalize3
No more need to write
create_translation_table!
in migrations.Just write in AR model
translates "title:string", "content:text"
and callrake db:globalize:up
.It automatically synchronize translated columns with database (create/drop table or add/change/remove column).
Good stuff - I have the same question as this guy, regarding how to integrate ElasticSearch into a model that uses Globalize3.
Is it also possible to use realiable validations?
Thanks so much for this episode :), great stuff
Hello guys and Ryan,
I was having a great challenge with Globalize3 regarding translating models on a website. Globalize3 relies on models created by a base locale set by I18n.locale or config.i18n.default_locale. Now lets have this situation when video above has three locales, WK, DE and EN (EN set as default). I have my fallback set to the en locale.
When I enter articles in english all goes fine, all the entries are now English for all locales. But for example when i try to make an article in WK language only, Globalize wont fall back to that language when the user browses the English or German language. For example when I view the page in German it still falls back to the English locale which is not set. Any suggestions how to fall back to the locale in which the Model is originally created when other locales are not set?
So my goal was; when a Article is created in a locale (WK) other then the default_locale (EN), the application needs to show that locale (WK) in the article as fallback locale. Even when i enter a translation for the default locale (EN) and the user requests the page in German (DE) the article is shown in the (WK) locale.
What i did is create an base_locale attribute to the Article model and tried overriding the read_attributes globalize uses. Here is some code
This idea worked quite fine, but i had really issues with updating and generating models. Maybe someone got a better idea?
I couldn't figure out this puzzle so i stopped it 3 months ago. Since this railscast is about globalize, I thought maybe we can solve it..
Since you are already overwriting stuff you could try this in an initializer
Non tested and found at stackoverflow but seems like it could work. Checkout the thread to see more details
http://stackoverflow.com/questions/7942450/is-it-possible-to-make-rails-i18n-locales-fallback-to-each-other
Nice advice to put it in a initializer.
I also came across this solution a time ago. This works for some cases.
When you have a large website 10 or more locales its not handy to define a fallback for each locale. The point is you have to define the fallbacks static.
I am using an Article to explain my problem but a more suitable case is when you have an Festival. An Festival is held in a Country and has a language that is the base language for the Festival. The Festival is held in England so the organisation enters the data in the English language. The organisation decides also to translate the translate the website in the WK language. Now a German guys enters the website, what i dont want is that the website shows the Festival in the next best fallback language but the language its originally created with. Because a Festival can also be entered in the WK language(original) and English language (translation) then a German user needs to see the Festival information in WK.
Then static fallbacks wont help me..
You could check if the model exists for the current locale (I think there is a method for that - would have to look it up) and if not redirect to the existing locale. Or add a text in that case saying something like "This Article doesn't exist in your language". And then you could link to all available language versions of that article. Clicking on one of the links, will change the app to the selected locale. That is also better in terms of usability compared to fallbacks, as it's not nice to have for example a russian article appear within a layout with english texts.
I have solved this same problem using a column where I store the original locale, and overriding a method set in Globalize3::ActiveRecord::InstanceMethods. Here is the code:
It works fine. I don't know if there is any downside...
It should probably be noted that at the moment there is a small glitch (regarding accessors / mass assignment) that prevents Globalize3 from working fully and seamlessly with rails 3.2.3. It will be fixed very soon, though!
Yeah, I've banged my head on it!
Let we know as soon as a fix for that will be available.
By now, I'm using the patch suggested here:
https://github.com/svenfuchs/globalize3/issues/128
Thank you!
Awesome railscast, as always. Does have any information on how to seed the translation table?
Does anyone know of a way to add translations on the fly as in say a user with admin rights creating a new locale?
This routes config will have redirect loop error. When you go to http://localhost:3000/abc you will see it.
So i suggest config routes like this to avoid redirect loop error.
This is probably a dumb question but, does using Globalize3 mean our original table (say Articles) now has empty columns for attributes that are translated and stored in the translation table?
Hi! I have the following problem: When I navigate across the site my locale changes to default. For Example when I am on index page and hit wookie and then go to another page my locale changes back to :en. I just can't get why?
my model is:
``` class Post < ActiveRecord::Base
attr_accessible :text, :title
translates :title, :text
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
class Translation
attr_accessible :locale
end
end ```
And application controller
``` before_filter :set_locale
private
def set_locale
I18n.locale= params[:locale] if params[:locale].present?
end ```
Thanks in advice!
btw it happens only with the resources path like this. when I visit post views
Anybody has any tips on how to seed the database with the translations?
Hi guys,
I'm following the tutorial, but I'm blocked to the migration which doesn't work.
I've created a question on StackOverflow, but haven't any clue yet.
I wonder if anyone can help me solving this issue. My code is here
Thanks in advance
Hi Lionel,
I also had problems with the migration.
Maybe this https://github.com/globalize/globalize/issues/261 could help you.
Works fine for me.
Hi, I was wondering if the field in the translation table must be the same as the one in the model. Say I want to translate description_en and would wish to have a field description_se on translation table. And further be able to access both attributes from rails admin.
Thanks Ryan for this great episode. We are waiting for your comeback.
Hi there! You probably have a typo in a date part of a migration name.
Maybe there should be "201204100000", not "291204100000"?