我希望将 JavaScript 代码与视图分开.
我需要为 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:我发现 Ayende 的解决方案.这是正确的方向.
PS: I found a solution by Ayende. This is the right direction.
我得到了提供 Controller.Resource('foo')
扩展方法的本地化助手类.
I got Localization helper class which provides the Controller.Resource('foo')
extension method.
我正在考虑扩展它(帮助程序),以便它可以按名称返回指定控制器的所有 JavaScript 资源(来自 App_LocalResources
中的ClientSideResources"子文件夹).然后 - 在 BaseController
中调用它,将它添加到 ViewData
并在 Layout
中渲染它.
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 对象(哈希),然后使用它来查找动态创建的对象.我认为这比回到服务器进行翻译要好.这类似于通过 viewdata 添加它,但可能更灵活一些.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.
在视图中:
<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 代码在容器中本地化,我就不会太在意将 JavaScript 代码排除在我的视图之外.通常我会设置我的母版页有 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 代码位于包含之后.我根据需要将共享代码重构回脚本.我曾考虑使用控制器方法来整理脚本包含,即使用单个请求包含多个脚本,但尚未解决.
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 代码分开以提高可读性,但也允许我根据需要轻松地将模型或视图数据注入到 JavaScript 代码中.
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 文件中的本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!