Git Product home page Git Product logo

i18n_backend_database's Introduction

A Database Backend For Rails I18N

Stores your translations in the database, rather than yaml files. As you tag items with i18n.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!

  • In implementing this into another project, I realized that the currency support is currently broken. It basically boils down to the i18n gem accessing some of the Rails i18n yaml’s in a different way than others. In this case, it’s requesting a parent key, and expects a hash to be returned containing the children. This is not baked in the plugin at the moment, and any contributions toward this would be well received. Thank you wlorentson!
  • 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.
  

In config/initialisers/i18n.rb

  
    I18n.backend = I18n::Backend::Database.new # registers the backend
    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 ^^
  

In config/routes.rb to register admin panel routes

  
    map.from_plugin 'i18n_backend_database'
  

Use

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


I18n.t("Hello there!")

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


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

I18n.t("Click {{here}} or {{there}}", :here => "link_to(I18n.t('midnite'), croix_path)", :there => "link_to(I18n.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.


I18n.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 I18n.l method. The format used will be :default (see next item for explanation).


I18n.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.


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

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


I18n.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.


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

Images can be translated with the I18n.ta tag


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

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

i18n_backend_database's People

Contributors

dylanz avatar smingins avatar

Watchers

James Cloos avatar Mohan Liu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.