Fetch Rails i18n translations to be rendered in React - ruby-on-rails

I have a React/Rails application which I am setting up i18n translations. Using rails-i18n and i18n.js gems. I have en.yml and es.yml files for english and spanish. The translations seem to work well for this application. I am able to get the translations rendered in the in the view. However, there is a second React/Redux application which makes api calls to this same backend. Is it possible to use the translations I already have in Rails? If so, what is the best way to set the translations?

Related

Where do I find rails locale files for different languages

Where do I find activerecord and date translations for whatever language I'm trying to add to my site?
Rails comes equipped with an I18n framework / API, which like most such frameworks involves defining a message catalog for each supported language. In the case of Rails, catalogs are YAML files under config/locales.
Rails defines standard keys for general ActiveRecord validation messages, but only comes with English translations by default. An alternative to providing translations from scratch yourself is to install the rails-i18n gem (svenfuchs/rails-i18n), which comes bundled with translations for many supported languages. These include both default ActiveRecord messages and assorted other things such as date and currency formats, days of the week, etc. You can mix and match the translations in rails-i18n with your own custom translations of course by adding your own locale files.
Note: The contributors to rails-i18n have done a great job, but not all locales have the full set of ActiveRecord messages, so in some cases you will have to provide your own (or consider contributing a pull request to the project).
EDIT:
The pre-defined catalogs that come with rails-i18n are installed with the gem. Running bundle info rails-i18n will show you the local install path; the locale files are in rails/locale if you want. Rails should find these automatically with further effort. So, in rails console:
foobar> I18n.locale = :tr
=> :tr
foobar> I18n.t 'time.pm'
=> "öğleden sonra"
You can extend or override the defaults by creating a custom locale file in your app's config/locales directory. The I18n search path will check config/locales before moving onto the default, so you only need to supply new or changed translations in your custom locale file.

Modifying i18n locales and after Rails app initializes?

I'm trying to use I18n locales for a multisite application and wondering whether it's at all possible to load some extra translation locale files after the rails app has initialized?
I'm already using the excellent Globalize gem for ActiveRecord model translations, but I essentially want to be able to load the locales for a given site from it's own folder. I'm therefore trying to use something along the following lines in my site loader code:
I18n.load_path = File.join(site.path, 'locales')
Where the site.path variable relates to the current site's configuration & asset directory.
I don't want to rely on adding an i18n initializer to glob through the sites directory for locales for 2 reasons:
I can't guarantee a different site won't use the same translatable string, and I don't want them overwriting each other.
I don't want to have to restart the application each time a new site has been added.
If it helps, my logic to doing it this way allows for some sites to be multi language, and some not, and allows me to be lazy about whether a site is multi-language or not because if I can do the above successfully, I can use I18n.available_locales to determine whether or not to show the lanuage switcher in the admin app etc.

Rails I18n doesn't work with asset pipeline templates in Rails 3.2.11

I need to localize an app, so I created a file for Spanish translations, but the config/locales/es.yml file doesn't seem to be properly loading, or maybe it loads after my templates are evaluated.
First a little context, I'm using the handlebars_assets and haml_assets gems. The latter allows to use the ActionView helpers within the templates in the asset pipeline and it also has access to the application classes, so one could write templates like:
.some_div
= SomeModel.model_name.human
And it works, except that it's returning the fallback name of the model. If I make these kinds of translations in a view though (instead of .hbs.haml template) I get the proper translated string. This is also true if I execute the translation methods in the rails console.
Following the Rails Guides I created the following initializer:
I18n.default_local = :es
Which is working properly if I consider that translating in views renders the expected output. My application is mostly living on the client side, so I really don't use Rails' views, it's a single page application managed with the help of Backbone. All the UI lives in templates that get rendered through the asset pipeline. So my question is, has anyone managed to make I18n work with this setup?

How can I use i18n for my error pages in Rails 3?

I need use the I18n api on my public/404.html page.
If I add to 404.html page:
<%= t('.page_were_looking') %>
I get an error message like:
Cannot use t('.page_were_looking') shortcut because path is not available
How can I use the I18n feature to this page?
Rails supports multilinguality by serving up error pages with a path like public/500.de.html, where de is the locale. It makes no sense, to use i18n for that case when Rails itself is fundamentally broken. If Rails is that much broken to get the right localized error file, it or probably the server will serve public/500.html.
This was introduced in Rails 2.3.1
There is the solution to generate this static files with a rake task, before you deploy the app. Have a look on this gist.

how to internatianalize the ruby on rails site

We have the rails app, content served from the database. Now we need to translate that to multiple languages. What is the best way to the internalization of the ruby on rails application?
You can use the plugin Globalize2 http://github.com/joshmh/globalize2
Try using localized record to add translations for your content.

Resources