Rails i18n和javascript中的路由 [英] Rails i18n and routes in javascript

查看:70
本文介绍了Rails i18n和javascript中的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时在JavaScript中提供Rails本地化文件会非常方便。对于路线助手也是如此。

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

我的问题:


  1. 还有其他插件/宝石在做类似的事情,比如上面两个吗?

  2. Rails中的正确方法是什么?元标记?其他数据属性?

感谢任何输入!

推荐答案

我使用你列出的第一个插件(i18n-js)并高度推荐它。它完全符合你的要求 - 让javascript可以使用本地化 - 非常简单。

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.

它的核心有两件事:


  • 从本地化文件自动生成messages.js文件

  • 提供与Rails I18n API非常相似的Javascript API。 / li>
  • autogenerates a messages.js file from your localization files
  • provides a Javascript API very similar to the Rails I18n API.

您为messages.js添加< script> 标记,然后在您可以写的其他.js文件 I18n.t('some.i18n.path',{interpolation_key:value,...}),这将返回翻译后的字符串。

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.

这篇关于Rails i18n和javascript中的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆