Rails i18n and routes in javascript

拥有回忆 提交于 2019-12-04 17:33:49

问题


Sometimes it would be really handy to have the Rails localization files available in JavaScript. Same is true for for the routes helpers.

I found these two plugins which are exactly doing this:

  • Exposing i18n to JavaScript: http://github.com/fnando/i18n-js
  • Rails Routes in JavaScript: http://tore.darell.no/pages/javascript_routes

My questions:

  1. Are there any other plugins / gems doing similar things like the two above?
  2. What's the right approach in Rails? Meta tag? Additional data- attributes?

Thanks for any input!


回答1:


I use the first plugin you listed (i18n-js) and recommend it highly. It does exactly what you are asking -- make localizations available to javascript -- with very little fuss.

At its core it does two things:

  • autogenerates a messages.js file from your localization files
  • provides a Javascript API very similar to the Rails I18n API.

You include a <script> tag for messages.js and then in your other .js files you can just write I18n.t('some.i18n.path', {interpolation_key: "value", ...}), which will return the translated string.

It also provides some formatting helpers, like for adding commas to numerals. And the author is very responsive to questions and bug reports.

I'm not sure what you are getting at with meta tags or data- attributes. I haven't needed those for internationalization.



来源:https://stackoverflow.com/questions/2860660/rails-i18n-and-routes-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!