简体   繁体   中英

Ruby on Rails - I18n in assets

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?

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 -%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM