What's the best way to keep track of ActiveRecord's i18n validation messages?
I'm constantly forgetti开发者_运维知识库ng to translate things like activerecord.errors.models.user.attributes.name.blank
in all my app's languages. Does anyone have any advice for keeping track of where translations are missing?
One thing you can do is set I18n.config.exception_handler = :raise_i18n_exception
in your test environment. It won't catch everything but it's super easy and better than nothing.
(In config/environments/test.rb it'd be just I18n.exception_handler = :raise_i18n_exception
)
Gettext is de-facto standard in translations. Its very feature-rich and frankly I hate apps trying to reinvent wheel. Thats why I always switch to gettext-rails (or fastgettext) plugins.
It works well with Models: http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html#Models
And ... it solves your problem. Gettext can help you keeping track of untranslated strings - you can configure to scream on you or just return the untranslated string (default).
You can check the translate plugin that adds a little web management console for your translation keys (and shows you which keys are missing and where).
It's probably a bit old but works with Rails 2 and should work even with 3 if you're up to the task of fixing some config issues (or just picking up the pull request that's already available in there).
Sometime ago I was thinking to a simple way to share all common translations across all apps. This railscasts episode gave me an idea on how to do that.
In short you can chain different translation backends and i18n gem will use them in the defined order, so I was thinking to use a Redis backend* to store all common translations (such as AR validation messages) and use that as a fallback backend for all my apps which need i18n.
This approach could solve your issue taking it from another point of view: write translations once and share them forever. Moreover any time you found a missing translation you'll fix it for all the apps which use the shared backend.
I haven't implemented this yet because I had no time to do that, but my idea was to do something like the mentioned translate interface to manage translations and share them using the approach shown in the railscast.
Hope this will help.
*Redis or any other persistent network key-values store would be ok for that task.
精彩评论