Setup acts_as_taggable_on with globalize3

Recently I had to setup a Rails app with localization support for static and dynamic content.

Static content was localized with Rails locale files and the seiten gem. For the dynamic content I used Sven Fuchs globalize3 gem which lets you localize all or just a few attributes of your database objects.

Because the app is using acts-as-taggable-on for tagging different models and globalize3 I think it was worth sharing how to get these two gems setup to work together.
The setup is really easy and shouldn't take longer than a few minutes. So let's dive in.

First, create an initializer file called config/initializers/acts_as_taggable_on.rb and add the following content:

Run rake db:migrate and you're all setup. Because it wasn't necessary for me to translate all tags I added a fallback, you find more about this in the globalize3 Readme.

Now you should be able to translate your acts_as_taggable_on tags:

I18n.locale=:detag=ActsAsTaggableOn::Tag.find_by_name("Allgemein")I18n.locale=:entag.name="General"tag.save# The tag name should now be different when you switch between the localesI18n.locale=:entag.name# => "General"I18n.locale=:detag.name# => "Allgemein"