Rails: Internationalization of Javascript Strings?
Why not something simple like:
<script type="text/javascript">
window.I18n = <%= I18n.backend.send(:translations).to_json.html_safe %>
</script>
Then in JS you can do things like:
I18n["en-US"]["alpha"]["bravo"];
I've wrapped mine in an application helper.
def current_translations
@translations ||= I18n.backend.send(:translations)
@translations[I18n.locale].with_indifferent_access
end
Then my call in my application.html.erb looks like this:
<script type="text/javascript">
window.I18n = <%= current_translations.to_json.html_safe %>
</script>
This allows you to avoid having to know the current locale in JavaScript.
I18n["alpha"]["bravo"];
Or
I18n.alpha.bravo;
Balibu is abandoned. Use i18n-js: https://github.com/fnando/i18n-js
Ryan's solution above is perfect, except the backend needs to be initialized if it hasn't been already.
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
# now you can safely dump the translations to json