How to translate one string from one locale into another using FastGettext? - ruby-on-rails

I use FastGettext in my Rails application.
I need a particular string translated into every single FastGettext.available_locales in my application.
How to do that?
PS: I couldn't find the solution with I18n either. (see answer below)

It seems to me that you can do something like that with I18n :
I18n.available_locales.each do |loc|
# do something with...
I18n.t(keypath, :locale => loc)
end

Related

Use custom locale in devise emails

I am using devise_invitable gem in rails and I have configured a number of locales in my rails app. What I would like to do is to invite a user with a locale I would specify myself. I t could look something like this:
User.invite!(:email => "test#example.com", :locale => 'fr')
This would send an email with the 'fr' as locale , even when I18n.locale would be en.
Is this possible, even with a complete different syntax than the one I am using above ?
Digging, I found that devise_invitable uses the devise mailer, see source. I'm not 100% sure how I18n.t exactly works, but I suppose you know that. So use alias_method_chain to monkey-patch the translate method. To pass the other language, you can choose one:
modify the whole call stack to pass down the variable
use a pseudo-global variable via Thread.current[].
I propose to use 2., make your choice. Then use that variable to return the correct translation.

Internationalization Best Practices / Rails App

I am new to ruby & rails and have started building an application.
My goal is to build this in a way I can easily translate the contents of the rails app and display the website contents in a locale preferred by registered user.
Appreciate any inputs on some of the best practices or references to any documentation to read, to build a web application that can be easily translated?
Thanks,
Krish.
Check out the Rails Internationalization (I18n) API. It does everything you've described.
Also check out Globalize3, it became a standard for model translations. Very useful.
You can use ready_for_i18n plugin that convert your erb to desired form.It saves some time.
You'd definetely watch this talk: http://www.youtube.com/watch?v=CTu4iHWGDyE
Here are some tips or best practices I noted while working through Internationalization of a Rails app. (It's possible that some of them are now outdated).
Before I get into a list here's some clarification between localization and translation that was helpful to me:
Definitions:
App localization and model translations are separate concerns. Figure out which one of those (or both) it is, that you need.
The way I use them here:
App localization: Localization your app to a locale.
Model translation: Translation your model/data into a language.
Example: Give me the french translation (model translation) of the resource in my Spanish site (app localization)
I use Globalize for model translations.
Helpful Tips/ Best Practices:
Some of the following are clearly just helpful tips while working in the Context of Rails + Globalize, some of them might be more than that... possibly best practices.
I18n.locale refers to and sets the locale of the app. When using Globalize, Globalize.locale refers to and sets the locale for model/data translations.
Set both I18n.locale and Globalize.locale on every request. Since these variables are set in Thread, it will avoid some hard-to-replicate bugs.
Set Globalize.locale to I18n.locale right after setting I18n.locale. This allows for model translations to default to the locale of the app. (On my Spanish site, I expect data to be in Spanish, by default).
Change Globalize.locale (and notI18n.locale) to change model translation.
Reset I18n.locale and Globalize.locale after every test. In rspec
RSpec.configure do |config|
config.after(:each) do
I18n.locale = :en
Globalize.locale = :en
end
end
Either use subdomain, or subfolder in the url to refer to the locale of the app.
Use a language parameter to specify the language of the data.
When you work on your rails app, you will probably use default_url_options to use I18n.locale as the default locale parameter for your route / path helpers. However this doesn't work in tests. I picked up a solution from this github issue on rspec-rails.
I monkey patch ActionDispatch::Routing::RouteSet like so:
class ActionDispatch::Routing::RouteSet
def url_for_with_locale_fix(options={})
url_for_without_locale_fix(options.merge(:locale => I18n.locale))
end
alias_method_chain :url_for, :locale_fix
end
Set up a translate helper t as a wrapper around the I18n.t method
module I18nHelper
def t string, options = {}
I18n.t string, options
end
end
RSpec.configure do |config|
config.include I18nHelper
end
This is mini-pattern I use. You can check it out: http://developers-note.blogspot.com/2012/01/rails-i18n-good-practice.html

How to manage Rails 2.3.x i18n locales programatically?

I would like to know if there is a way to insert, alter and remove i18n locale keys programatically (I guess I could use the DB, but I like Rails i18n and want to stay as close to it as possible).
Basically I want to know if there's a way (native, gem, plugin, whatever) to do things like:
I18n.add_locale_key("en", "application.messages.submit_message", "Submit message!")
I18n.add_locale_key("es", "application.messages.submit_message", "Enviar mensaje!")
I18n.remove_locale_key("en", "application.messages.submit_message")
I18n.remove_locale_key("es", "application.messages.submit_message")
As packaged, the Rails I18n API only support defining locale terms via the local .yaml or .rb files. Short of dynamically editing those files at runtime, your best bet is to use the DB functionality of a gem gem like FastGetText.
You could also roll your own solution, of course, but the DB method will likely work for your use case and will result in a smaller time investment.
Here's one way to do it:
>> I18n.backend.store_translations :en, :hello_world => "Hello, world."
=> {:hello_world=>"Hello, world."}
>> I18n.t :hello_world
=> "Hello, world."

How can I make the Rails 3 router localize URLs using localization files?

What I'd like to be able to do is:
in config/routes.rb
resources :posts
in config/locale/en.yml
en:
resources:
posts: "posts"
new: "new"
edit: "edit"
in config/locale/tr.yml
tr:
resources:
posts: "yazilar"
new: "yeni"
edit: "duzenle"
and get
I18n.locale = :en
edit_post_path(3) #=> /posts/3/edit
I18n.locale = :tr
edit_post_path(3) #=> /yazilar/3/duzenle
I'd also like Rails to match any of these routes anytime and pass the associated locale in the params hash such that when I navigate to /yazilar , the request should be routed to the posts#index action with the :tr locale in the params hash.
Any simple or complex way of doing that?
There is also the i18n_routing plugin by Guillaume Luccisano at
http://github.com/kwi/i18n_routing that solves most of those problems
however, it currently lacks translating the action names like ../new and ../edit. Guillaume says he will implement that "soon", though.
Update: Action name translating has been implemented in i18n_routing. Thanks Guillaume. :)
As the mentioned gems are no longer actively maintained, here are some more recent solutions:
gem route_translator
gem rails-translate-routes
both are successors of translate_routes
or a more powerfull (and complex) solution:
routing-filter
The translate_routes plugin by Raul Murciano provides some of the features you are asking for:
http://github.com/raul/translate_routes
It only works for Rails 2.3.x as far as I know though. But at least you can either get some ideas or even fork the plugin and make it work on Rails 3 yourself.

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:
de:
activerecord:
attributes:
user:
login: "Benutzerkennung"
comment: "Bemerkungen"
And in my view this:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
But instead of the translation value ("Benutzerkennung") I am getting the infamous
"translation missing: de, activerecord, attributes, user, login"
Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)
Thanks!
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
To help you debug along the way, use "script/console" and the following statements:
- I18n.locale --> should output the locale you want to examine
- I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found
And btw - the plugin works quite well http://github.com/iain/i18n_label/
if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')
Another method:
<%= label_tag :login, User.human_attribute_name(:login) %>
You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!
gem install -v=2.3.11 rails
You can find this issues documented here: Rails 2.3.9 Release notes

Resources