Reputation: 133
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.
Upvotes: 2
Views: 4760
Reputation: 191
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
Upvotes: 1
Reputation: 438
You'd definetely watch this talk: http://www.youtube.com/watch?v=CTu4iHWGDyE
Upvotes: 1
Reputation: 15266
This is mini-pattern I use. You can check it out: http://developers-note.blogspot.com/2012/01/rails-i18n-good-practice.html
Upvotes: -1
Reputation: 6942
You can use ready_for_i18n plugin that convert your erb to desired form.It saves some time.
Upvotes: 3
Reputation: 5286
Also check out Globalize3, it became a standard for model translations. Very useful.
Upvotes: 3
Reputation: 32037
Check out the Rails Internationalization (I18n) API. It does everything you've described.
Upvotes: 4