Translation Center is a multi lingual web engine for Rails apps. It builds a translation center community with translators and admins from your system users.
Contribute in translating your app in different locales using easy web interface.
Avoid updating many yaml files as you will just use the easy web interface to manage translations.
Manage all app translations; collect stats, accept, add, edit, remove translations...etc
- Different roles: translators and admins
- Add new locales easily
- Support right-to-left and left-to-right translations
- Detect new translation keys in code and store them in DB for users to translate
- Inspect translation keys from your application view directly
- Import and Export translations in yaml format
- Switch translations backend between DB and yaml
- Admin dashboard for languages status and activity
- Users can vote up translations
- Default translation in English for keys
TranslationCenter works with Rails 4.x onwards. You can add it to your Gemfile with:
gem 'translation_center'
Run bundle install command.
TranslationCenter depends on Devise, so make sure you installed it successfully https://github.com/plataformatec/devise#starting-with-rails
After you install TranslationCenter and add it to your Gemfile, you need to run the generator:
rails generate translation_center:install en ar de
This will add three languages to the translation center, you need to add them in the config/translation_center.yaml
development:
lang:
en:
name: 'English'
direction: 'ltr'
ar:
name: 'Arabic'
direction: 'rtl'
de:
name: 'German'
direction: 'ltr'
if you don't supply languages for the generator it will support only English.
And run the migrations
rake db:migrate
Add this line to the translator model (typically User
)
acts_as_translator
And then run this rake to get all current translations to the db
rake translation_center:synch
and change the translator_type
in translation_center.yml
to the translator model name (if it is User
keep it commented)
# tranlator_type: 'User'
In routes file add
mount TranslationCenter::Engine => "/translation_center"
By default, all users are admins, you need to define who is the translation center admin. Admin can accept translations, manage translation keys and do more things. To define your admin, you need to override User#can_admin_translations? method like the following....
def can_admin_translations?
self.email == 'admin@tc.com'
end
If you are Using rails 3 you can revert back to tag 1.7.2
- Create a SQL database so your app connects to both MongoDB and this new SQL database.
- Create a new SQL table (ex: translator_users) that will be considered as translator user and has a foreign key for your MongoDB user (ex: mongo_user_id)
- Add
acts_as_translator
to your ActiverRecord user (ex: TranslatorUser) model not Mogno User - Update translation_center.yml to the translator model name if needed
tranlator_type: 'TranslatorUser'
- You also need to add these methods in an initializer in case Devise is not existing in Mongo, for example
translation_authentication.rb
:in the current_user method make sure that you return the ActiveRecord user not the Mongo user because that's what we will use in translation center engine.module TranslationCenter class ApplicationController < ActionController::Base # current_user is needed in views helper_method :current_user def authenticate_user! # redirect to login if user not signed in end def current_user # write code that returns the current ActiveRecord user in session end end end
Add new key to your views, for example:
t('posts.index.title')
When you visit the page where this key is rendered, a new translation key will be stored with name 'index.title' under category 'posts'. You then visit the TranslationCenter and translate the key (by default translations added by admins are considered accepted).
To migrate translations from TranslationCenter database to yaml files
rake translation_center:db2yaml
To migrate translations from yaml files to TranslationCenter database
rake translation_center:yaml2db
If you want to export or import just one locale, provide the locale as an attribute to the rakes, for example:
rake translation_center:yaml2db[ar]
Or you could use the Controls section in the Dashboard page to run these rakes
Imported translations should have a translator. You can edit translator identifier (usually email address) in translation_center.yml
The rake task yaml2db
will create this user if it doesn't exist.
yaml_translator_identifier: 'coder@tc.com'
The imported translations status will be accepted by default. If you want to disable this, comment the following line in translation_center.yaml
yaml2db_translations_accepted: true
You can control the translations source by changing the value of i18n_source
in translation_center.yaml
i18n_source: 'yaml' # can be db or yaml; default is yaml
If your application doesn't use devise for authentication then you have to
provide helper named current_user
that returns the current user in the session and a before_filter named authenticate_user!
that redirects a user
to login page if not already signed in.
You also need to add these methods in an initialize, for example translation_authentication.rb
:
module TranslationCenter
class ApplicationController < ActionController::Base
# current_user is needed in views
helper_method :current_user
def authenticate_user!
# redirect to login if user not signed in
end
def current_user
# write code that returns the current user in session
end
end
end
Email attribute:
Translation Center assumes that the translator model (for example User
) has an email
attribute, this attribute is used when showing translations, activity log in dashboard and updating existing translations.
If your User
model has no email
attribute then change the identifier_type
in your translation_center.yml
to a unique attribute of your translator model (User
), for example to use username instead of email:
identifier_type: 'username' # Uncomment and change the identifier if your User model has no email attribute
Another nice option is the inspector that allows the users to go directly to the key from your application view.
Just set inspector
option in translation_center.yml
to all
if you want to inspect all keys otherwise set it to missing
to inspect only untranslated keys, and then add this line to your application.css
*= require translation_center/inspector
and this line to your application.js
//= require translation_center/inspector
Now when you reload your page you will see a small icon beside your keys.
In the previous image, if you click on the icon of 'Betrachten', you will be directed to the key page, where you can add/edit translations directly.
If you want to add a language to the translation center, you need to run the generator:
rails g translation_center:add_lang es fr
rake db:migrate
You will also need to add the language to config/translation_center.yml
development:
lang:
en:
name: 'English'
direction: 'ltr'
ar:
name: 'Arabic'
direction: 'rtl'
de:
name: 'German'
direction: 'ltr'
When you visit /translation_center
you will see the list of all categories and how many keys they have.
Click on category to view all the keys, keys are divided into untranslated (has no translations), pending (has translations but not approved yet), translated (has accepted translations)
Click on a key to view all translations for that key, then you can add or edit your translation for that key, users can also vote for translations.
As an admin you can accept pending translations that have been added by other users, you can also edit and remove keys.
Admin also can view the dashboard to know the status of every language
or monitor activity of changes to translations.
Watch it on YouTube. http://www.youtube.com/watch?v=BTy6ZI31JmU
Translation Center is maintained and developed by BadrIT
We hope that you will consider contributing to Translation Center. You will usually want to write tests for your changes. To run the test suite, go into Translation Center's top-level directory and run "bundle install" and then "rspec spec"
Help us make this engine better by submitting any bugs or enhancments in the issues page for this project. We also added an error page that will appear to you if any error goes with translation_center.