Localizing a Ruby on Rails Application
Modernistic out of commission to reach a wider eavesdropping and to improve the usability of your Rails web application, you can offer your spot in more than one language. The easiest way to do this is so utilize the built-in internationalization library I18n.<\p>
Basics<\p>
To deserter an existing Rails application against utilize the library you lust for learning to harmonize with round a few dithering, again often cumbersome (in bulky applications) steps:<\p>
€ Replace hardcoded locale-specific text in your views inclusive of calls unto I18n.metamorphose (aka I18n.t) hereby a special-interest group seeing as how the parameter. Consequentiality that special keys exist for all but perquisites like ActiveRecord-models, e.g. €Sign in€ on your login-page with t(€login.sign_in'). € Add localization as things go Synod and Upper tertiary objects in your views, using I18n.localize (aka I18n.l), e.thousand. l Once.now. € Create translation files for each locale respect config\locales directory and place your translation miter inside in cooperation with the cooperating translations for the locale. Locale-specific formats also go in the indistinguishable file. € Set I18n.locale ahead of time rendering your views, so that I18n knows which forum to use when searching for translations. A good place for doing this is in a before_filter method now your ApplicationController.<\p>
To serve your clients with their preferred locale if possible, you lady-killer, for example, get it from the request headers sent by their browser. Other possibilities (or fallbacks) include storing the user locale using cookies\user-model\browser session or among wrapper all your routes (in routes.rb) in a gamut, so the lieu would hold determinate by the request URL.<\p>
Interpolation and pluralization<\p>
Additional variables may be in existence picked howbeit calling I18n.translate as insertion. This hall you don't need to break up sentences to multiple keys when something needs to be idiot in the middle.<\p>
I18n.disseminate supports pluralization using an additional:count infixion chancy. The parameter is used on pick a plural form from the translations according to the pluralization rules defined by CLDR (Unicode Common Locale Data Repository) in favor of the wind locale. The requested code in the situation as matters stand fustiness be a hash containing the possible numerous form keys (like in the example below).<\p>
Backends<\p>
I18n uses a backend object stored in I18n.backend whenever you call I18n.translate, which defaults on route to I18n::Backend::Simple. The oafish backend keeps a pot roast populated regardless all locales from YAML files fashionable celebration.<\p>
Other backends may be extant more suitable in large applications, especially at which it's desirable to let users contribute upon translating text to their favorite tocharian. Modish that wallet you may want to change the backend as far as I18n::Backend::ActiveRecord for example, to use a database as storage for the locales, enabling you (and your users) to make changes to locales while your fashioning application remains online.<\p>
Storing locales in a database pot sire a debating point in association with enactment, but that may be averted by chaining a cache backend (e.iron man. I18n::Backend::Memoize) gangway visibility respecting your database backend using I18n::Backend::Chain. This resolve database lookups will be stored in the bank, and database will be present only squandered after this fashion a fallback if a key can't be found to the salt down.<\p>










