如何处理本地化的JavaScript文件? [英] How to handle localization in JavaScript files?
问题描述
我想的JavaScript code从视图中分离出来。结果
我得到了落实由JavaScript生成一个简单的图像按钮定位的要求:
I want JavaScript code to be separated from views.
I got the requirement to implement localization for a simple image button generated by JavaScript:
<img src="..." onclick="..." title="Close" />
什么是本地化的称号最好的技术?
What's the best technique to localize the title of it?
PS:我发现<一个href=\"http://ayende.com/Blog/archive/2007/10/07/Handling-javascript-localization-in-Mono-Rail.aspx\">a溶液Ayende 的。这是正确的方向。
PS: I found a solution by Ayende. This is the right direction.
编辑:结果
我得到了本地化的辅助类,它提供了 Controller.Resource('富')
扩展方法。
I got Localization helper class which provides the Controller.Resource('foo')
extension method.
我想扩展它(辅助),所以它可以(在 App_LocalResources文件
从ClientSideResources子文件夹)返回所有的JavaScript资源,通过其名称指定控制器。然后 - 调用它 BaseController
,将其添加到的ViewData code>,呈现在
布局
。
I am thinking about to extend it (helper) so it could return all JavaScript resources (from "ClientSideResources" subfolder in App_LocalResources
) for the specified controller by its name. Then - call it in BaseController
, add it to ViewData
and render it in Layout
.
请问这是个好主意?
推荐答案
修改
考虑编写必要的本地化资源为JavaScript对象(散),然后使用它来查找你的动态创建的对象。我觉得这是不是要回服务器翻译好。这类似于通过可视数据添加它,也可以是稍微更灵活。 FWIW,我可以考虑本地化资源是视图,控制器不属于的一部分。
Consider writing the necessary localized resources to a JavaScript object (hash) and then using it for lookup for your dynamically created objects. I think this is better than going back to the server for translations. This is similar to adding it via viewdata, but may be a little more flexible. FWIW, I could consider the localization resources to be part of the View, not part of the controller.
在View:
<script type="text/javascript"
src='<%= Url.Content( "~/Resources/Load?translate=Close,Open" %>'></script>
这将输出类似:
var local = {};
local.Close = "Close";
local.Open = "Open";
如果没有参数它会输出整个翻译的哈希值。使用参数为您提供了自定义每个视图的能力。
Without arguments it would output the entire translation hash. Using arguments gives you the ability to customize it per view.
然后,您可以使用它在你的JavaScript文件,如:
You would then use it in your JavaScript files like:
$(function(){
$('#button').click( function() {
$("<img src=... title='" + local.Close + "' />")
.appendTo("#someDiv")
.click( function() { ... } );
});
});
其实,我不是太大惊小怪保持我的JavaScript code出自己的看法,只要JavaScript的code是一个容器本地化。通常情况下,我会为我设置母版页了4方面内容:标题,标题,主,和脚本。标题,标题和主要去的地方,你会期望和脚本区在云体的底部。
Actually, I'm not too fussed about keeping my JavaScript code out of my views as long as the JavaScript code is localized in a container. Typically I'll set my master page up with 4 content area: title, header, main, and scripts. Title, header, and main go where you would expect and the scripts area goes at the bottom of the body.
我把所有我的JavaScript包括,包括任何为viewusercontrols,进入脚本容器。查看特定的JavaScript code而来的包括后。根据需要,我重构共享code回脚本。我想过使用一个控制器方法核对脚本包含,也就是包括使用单个请求多个脚本,但还没有得到解决,以那,但。
I put all my JavaScript includes, including any for viewusercontrols, into the scripts container. View-specific JavaScript code comes after the includes. I refactor shared code back to scripts as needed. I've thought about using a controller method to collate script includes, that is, include multiple scripts using a single request, but haven't gotten around to that, yet.
这有保持的JavaScript code分离为可读性的优势,但也允许根据需要我轻松地注入模型或视图数据到JavaScript的code。
This has the advantage of keeping the JavaScript code separate for readability, but also allows me to easily inject model or view data into the JavaScript code as needed.
这篇关于如何处理本地化的JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!