This fork is only for Rails3. Master require a current_translator with locales authorization. The rails3 branch is without this requirements. For Rails2, check dhh/tolk
Ruby JavaScript
Clone or download
Pull request Compare This branch is 68 commits ahead, 1 commit behind dhh:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Tolk is a Rails 3 engine designed to facilitate the translators doing the dirty work of translating your application to other languages.

* Requirements

This branch requires a current_translator. Based on the current_translator, authorization will be made to only allow authorized translators to make updates on a specific locale.

  # Authorized to edit all locales and to add new ones => ["all"])
  # Authorized to edit French and German => ["fr", "de"])

You might then consider an initializer like :
  Tolk::ApplicationController.authenticator = proc {

* Can be improved, help / patch welcome
I've went through a strange issue with the authentication, so I forced it by using directly into tolk application_controller :

  def authenticate
    # FIXME: Why this doesn't work? Loading order ?
    # self.authenticator.bind(self).call if self.authenticator && self.authenticator.respond_to?(:call)

For a branch without authorization, check (no extra features) or (allow to search and scope by keys)

* Installation & Setup

To install add the following to your Gemfile:

  gem 'tolk'

To setup just run:

  $ rake tolk:setup

* Usage

Tolk treats I18n.default_locale as the master source of strings to be translated. If you want the master source to be different from I18n.default_locale, you can override it by setting Tolk::Locale.primary_locale_name. Developers are expected to make all the changes to the master locale file ( en.yml by default ) and treat all the other locale.yml files as readonly files.

As tolk stores all the keys and translated strings in the database, you need to ask Tolk to update it's database from the primary yml file :

  $ rake tolk:sync
The above will fetch all the new keys from en.yml and put them in the database. Additionally, it'll also get rid of the deleted keys from the database and reflect updated translations - if any.

If you already have data in your non primary locale files, you will need to import those to Tolk as a one time thing :

  $ rake tolk:import

Upon visiting - you will be presented with different options like creating new locale or providing translations for the existing locales. Once done with translating all the pending strings, you can write back the new locales to filesystem :

  $ rake tolk:dump_all

This will generate yml files for all non primary locales and put them in #{Rails.root}/config/locales/ directory by default.

You can use the dump_all method defined in Tolk::Locale directly and pass directory path as the argument if you want the generated files to be at a different location :

  $ script/runner "Tolk::Locale.dump_all('/Users/lifo')"

You can even download the yml file using Tolk web interface by appending '.yml' to the locale url. E.g

* Authentication

If you want to authenticate users who can access Tolk, you need to provide <tt>Tolk::ApplicationController.authenticator</tt> proc. For example :

  # config/initializers/tolk.rb
  Tolk::ApplicationController.authenticator = proc {
    authenticate_or_request_with_http_basic do |user_name, password|
      user_name == 'translator' && password == 'transpass'

Authenticator proc will be run from a before filter in controller context.

* Handling blank and non-string values

Tolk speaks YAML for non strings values. If you want to enter a nil values, you could just enter '~'. Similarly, for an Array value, you could enter :

- Sun
- Mon

And Tolk will take care of generating the appropriate entry in the YAML file.