Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
README.rdoc | Fri Jan 23 08:08:38 -0800 2009 | |
| |
Rakefile | Fri Jan 23 04:29:23 -0800 2009 | |
| |
init.rb | Fri Jan 23 07:58:33 -0800 2009 | |
| |
lib/ | Fri Jan 23 08:08:38 -0800 2009 | |
| |
tasks/ | Fri Jan 23 07:58:33 -0800 2009 |
Mr. T
Another attempt at making calls to I18n#translate a bit simpler. This one is for views and controllers.
The features until now:
- Translate in the scope of the view or controller, with proper fallback.
- Write missing keys to a yml file.
- A complete list of which keys get added to your log, so you can easily check which fallbacks you could use.
- A rake task to combine all translations in a file per locale.
- A rake task to compare translations and register missing keys among all available locales.
Planned features:
- Maybe add google translate service, like amatsuda’s i18n_generators.
- Add automatic scoping for ActiveRecord. Calling t(:something) might presume it’s a human_attribute_name.
- Add writing missing translation data to all calls to I18n#t.
- A way to ask Mr T to have pity on you and configure it to your liking.
Installation
This plugin is built for Rails 2.2.2 and greater. Mr T should be indifferent to which backend you use. Installation is straight forward enough:
./script/plugin install git://github.com/iain/mr.-t.git
You need to install ya2yaml if you want to be UTF8-safe. To install, add this to your config/environment.rb:
config.gem "ya2yaml"
And run one or both:
rake gems:install rake gems:unpack
Usage
Translating in views or controllers
A normal case would be that the both the new- and the edit-action of a controller renders the form partial. Most of the names will be the same, but not always. Mr. T have his defaults in such a manner that you can easily manage this.
# app/views/posts/_form.html.erb
<%= t("Shut up, fool!") %>
will look up these keys:
- posts.new.shut_up_fool # or posts.edit.save, depending on which action was run
- posts._form.shut_up_fool
- posts.__common.shut_up_fool
- shut_up_fool
When working from the controller, it will be:
- posts.new.shut_up_fool
- posts.__common.shut_up_fool
- shut_up_fool
Notice that any special characters from the key will be removed, when used as translation lookup key, but the original key will be defaulted to, and will be stored in the missing locale file.
And finally it’ll serve the original string as a default, but not without writing to config/locales/missing_en.yml (depending on your locale of course).
The log will display which fallbacks have been used, so you easily check out where you can place your translations.
Mr. T says (from /app/views/posts/_form.html.haml:3): ["posts.new.shut_up_fool", "posts._form.shut_up_fool", "posts.__common.shut_up_fool", :shut_up_fool, "Shut up, fool!"]
Notice: The t() method will be overridden by Mr. T, but the translate method doensn’t. Use translate if you wish to use the old behavior. Also, setting the default option, will disable Mr T’s special abilities and perform a regular lookup, without complicated fallbacks.
Rake tasks
When you provide the proper translations in your missing locale files, you can add them to the combined locale files (en.yml nl.yml, de.yml, etc) using the rake task:
rake i18n:combine
This will include all translations per locale, even the ones Rails provides and put them in their proper locale file. Old locale files will be renamed to oldfile.yml.bak where oldfile is of course the name it used to have.
To quickly make sure that every available language has all missing locales, run:
rake i18n:compare
This will write all missing translation keys to missing_en.yml (depending on the locale it’s missing from).
Notice
The code is still rather scatchy and needs to be refactored and tested. Some work still needs to be done. Any suggestions and additions is of course very welcome.
Copyright © 2009 Iain Hecker, released under the MIT license







