1

I am creating an API-like website with mostly JSON responses and jQuery ajax callbacks. To respond without sending all the text by JSON through the controller action, I want to use I18n in my assets. Since assets are static I would have to precompile a version for each language and then serve the one based on the user language setting in the database.

Currently I don't really have an idea how to approach that. I could just create js files and define all the I18n variables in there instead as javascript - then load the correct one with javascript_include_tag. But that way I give all users access to the complete language file and I already feel like I make my app too open by using JSON responses only. Is there a way to just use I18n in my assets? And how do I precompile and serve them individually?

Miiller
  • 1,063
  • 1
  • 10
  • 29
  • Take a look at the [i18n-js](https://github.com/fnando/i18n-js) gem – Stefan Oct 11 '13 at 23:26
  • @Stefan if I understood it correctly, what the gem basically does is what I want to avoid - providing all I18n translations in one .js file. I don't want users to be able to access and copy all translations at once. But I guess it could be an alternative if there is no other way around... – Miiller Oct 12 '13 at 00:27

1 Answers1

-1

Frankly speaking I didn't get the whole idea. But may be it will be useful for you: you can define I18n in your js files like that:

views/example.html.erb

<%= javascript_tag do %>
  window.variable = '<%= j t('variable') %>';
<% end -%>
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
alex
  • 3,682
  • 3
  • 21
  • 22
  • He stated that he did not want to render the strings in the action/view; and he does not use JS at all, but JSON (i.e., AJAX requests). Unfortunately, in Rails 4, this problem does still not seem to have a good solution. http://stackoverflow.com/questions/9310044/rails-i18n-of-css-file has a nice solution (hack?) for CSS assets, at least. – AnoE Feb 02 '16 at 14:06