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:

ActsAsTaggableOn::Tag.class_eval do
  translates :name

Now create a migration file with the following content:

class TranslateTags < ActiveRecord::Migration
  def self.up
      :name => :string
    }, {
      :migrate_data => true

  def self.down
    ActsAsTaggableOn::Tag.drop_translation_table! :migrate_data => true

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 = :de
tag = ActsAsTaggableOn::Tag.find_by_name("Allgemein")
I18n.locale = :en = "General"

# The tag name should now be different when you switch between the locales
I18n.locale = :en
# => "General"
I18n.locale = :de
# => "Allgemein"