Skip to content

Pallinder/i18n_backend_database

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Database Backend For Rails I18N (Now for rails 3!)

Stores your translations in the database, rather than yaml files. As you tag items with t() throughout your code base, all untranslated items are marked and added to the database. An admin panel is provided so translators can quickly translate untranslated text. All lookups occur in a cache store of your choice prior to hitting the database.

DISCLAIMER!

  • The translations_controller is unprotected, and you’ll probably want to add some kind of authorization filter to it to make sure the outside world can’t access it.

Installation

  
  script/generate i18n_backend_database             # add migration
  rake db:migrate                                   # migrate

  rake i18n:populate:load_default_locales           # populate default locales
  rake i18n:populate:from_rails                     # populate the locales and translations tables from all Rails Locale YAML files.
  rake i18n:populate:from_application               # populate the translation tables from translation calls within the application.
  rake i18n:populate:synchronize_translations       # create non-default locale translation records from default locale translations.
  rake i18n:populate:all                            # run all populate tasks.
  rake i18n:translate:google                        # translate all untranslated string values using Google Language Translation API.
  
  rake i18n:import_translations locale=<locale>     # Import the translations from the translations/<locale>.yml
  rake i18n:export_translations                     # Exports all the database translations to the translations/ folder
  

In config/initialisers/i18n.rb

  
    I18n.backend.cache_store = :memory_store   # optional: specify an alternate cache store
    I18n.backend.localize_text_tag = '##'      # optional: specify an alternate localize text tag, the default is ^^
  

Use

All non-user generated text provided by the application needs to be wrapped in a call to I18n.t().


t("Hello there!")

Interpolation is handled by passing in key/value pairs as a value to an interpolation tag ( %{ } ).


t("Hello there %{name}!", :name => "Dylan")

t("Click %{here} or %{there}", :here => "link_to(t('midnite'), croix_path)", :there => "link_to(t('staten_island'), wu_path)")

Pluralization is handled by passing in a “count” key value pair, which is a unique interpolation value.


I18n.t("You are %{count} years old", :count => 100)

Links to external documents that need to be translated should be tagged as well.


t('http://www.elctech.com/core')

All fragment cache view blocks need to have their keys prepended with the current locale.


cache("#{I18n.locale}-footer_#{controller.action_name}")

Date/Time localization is handled by using the localize (l) method. The format used will be :default (see next item for explanation).


l(@user.joined_at)

Date/Time localization can take a format parameter that corresponds to a key in the translations table (the Rails defaults :default, :short, and :long are available). We could in theory create our own like en.date.formats.espn_default.


l(@user.joined_at, :format => :default) l(@user.joined_at, :format => :short) l(@user.joined_at, :format => :espn_default)

Date/Time localization can take a custom format string as well.


l(@user.joined_at, :format => "%B %e, %Y")

Text stored in a database can be localized by tagging the text being stored and then localizing in the view etc.


tlt("is now friends with") => "^^is now friends with^^" lt("shane ^^is now friends with^^ dylan") => "shane ahora es con amigos dylan"

Images can be translated with the I18n.ta tag


<%= image_tag(ta("logos/elc.gif"), :size => "134x75") %>

In this example, for a locale es, there should be an image: public/es/images/logos/elc.gif

About

Database Backend for Rails I18n

Resources

License

Stars

Watchers

Forks

Packages

No packages published