HasAccent is an internalization plugin for Ruby on Rails. It allows you to easily store and retrieve translations of any of your AR model's string/text fields in the database.
Rails 2.1+ is required for this plugin.
- Sitting in your Rails app root folder:
./script/plugin install email@example.com:norbauer/has_accent.git
- Generate the required migrations with:
- Open up
environment.rband create a list of available languages. It's recommended to use the language code instead of the full name:
HasAccent.languages = [:en, :es, :fr]
- The HasAccent's default language is english, this means that HasAccent will treat the actual values stored in the ActiveRecord instance as being in English, but you can override this setting. For example, if you want to make French the default language:
HasAccent.default_language = :fr
- Add the
has_accentcall on any of your ActiveRecord models for which you wish to enable translations, by default all the string/text attributes will be set as translatable.
class Product < ActiveRecord::Base has_accent end
- You can also override these default settings:
class Product < ActiveRecord::Base has_accent :name, :description end
has_accenthas been set, anytime you create a new record, HasAccent will create empty translation stubs for each of the languages. You can access these translations by calling
translationson a record (using the example above):
@product = Product.find_by_id(params[:id]) @translations = @product.translations # Returns all translations @translations = @product.translations.pending # Returns pending translations, those which are just stubs (empty) @translations = @product.translations.dirty # Returns all translations that might be out of date because the original attribute was modified.
The Translation model attributes are:
content - Stores the actual text translation.
translatable_attribute - Stores the name of the attribute that this translation is linked to (Using the example above, either 'name' or 'description')
language - Stores the language name or code, depending on how you set them up during the configuration ('en', 'fr', 'es', etc...)
- Add a
before_filterin any of your Rails controllers to set the current language, ideally in your ApplicationController:
class ApplicationController < ActionController::Base before_filter :set_current_langauge def set_current_language lang = # Here goes the logic where you decide which is the current language HasAcent.current_language = lang end end
- HasAccent will give you a set of dynamic methods that you can use in your views to return the correct translation based on
HasAccent.current_languagesetting. Using the example above and assuming all translations have been entered:
HasAccent.current_language = :en @product.name # => 'Car' @product.translated_name # => 'Car' HasAccent.current_language = :es @product.name # => 'Car' @product.translated_name # => 'Carro' HasAccent.current_language = :fr @product.name # => 'Car' @product.translated_name # => 'Voiture'
Copyright (c) 2008 Norbauer Inc, released under the MIT license
Written by Jose Fernandez and Ryan Norbauer, with support from The Sequoyah Group