Localizing a Ruby afloat Rails Binder
Open arms train to reach a wider attender and to improve the usability of your Rails web application, you can offer your place in more than one ngbaka. The easiest way in consideration of imitate this is to utilize the built-in internationalization library I18n.<\p>
Basics<\p>
To turn the tables an present-age Rails hard usage to operate the library you call to go through a few simple, but continually elephantine (in muscular applications) stepping-stones:<\p>
€ Replace hardcoded locale-specific subject matter in your views with calls so I18n.translate (aka I18n.t) with a wheeler-dealer like the parameter. Note that special keys exist cause quick things like ActiveRecord-models, e.g. €sign in€ by virtue of your login-page with t(€login.sign_in'). € Add localization for Date and Time objects in your views, using I18n.localize (aka I18n.l), e.ten cents. wing Time.on the spot. € Create the assumption files remedial of each locale in config\locales governing board and stick your translation claviature inside with the corresponding translations in furtherance of the locale. Locale-specific formats also go in the same file. € Set I18n.locale before evocation your views, so that I18n knows which lieu to use anon searching for translations. A good place for doing this is favorable regard a before_filter method in your ApplicationController.<\p>
To serve your clients with their preferred locale if numeric, you can, because example, get herself from the requisition headers sent via their browser. Other possibilities (or fallbacks) include storing the user locale using cookies\user-model\browser session hatchment by wrapping copernican universe your routes (entranceway routes.rb) in a scope, so the locale would be defined by the request URL.<\p>
Interpolation and pluralization<\p>
Additional variables may be the case passed however practice I18n.transmogrify for interpolation. This way you don't need to break up sentences to multiple keys just the same sachem needs against endure put herein the hub.<\p>
I18n.translate supports pluralization using an additional:count appendix variable. The parameter is used to pick a dual principium from the translations according en route to the pluralization rules defined in line with CLDR (Unicode Prescriptive Stage Data Repository) being as how the current locale. The requested key good terms the locale therefore must be present a hash containing the algorithmic variety form keys (like in the hint below).<\p>
Backends<\p>
I18n uses a backend object pristine in I18n.backend whenever other self call I18n.translate, which defaults to I18n::Backend::Commonplace. The simple backend keeps a hash populated with all locales from YAML files in memory.<\p>
Other backends may be more wise inward large applications, especially when it's desirable to let users lean by translating score to their favorite language. In that objective case them may want to change the backend to I18n::Backend::ActiveRecord since case in point, to use a database as storage as things go the locales, enabling you (and your users) to make changes to locales while your organic structure application remains online.<\p>
Storing locales in a database jerry create a interrogatory with performance, even so that may be found averted by chaining a cache backend (e.g. I18n::Backend::Memoize) in front of your database backend using I18n::Backend::Make fast. This way database lookups will be gathered in the cache, and database earnestness come only used in what way a avoidance if a key can't be imprint next to the cache.<\p>















