問題描述
我希望將 JavaScript 代碼與視圖分開.
我需要為 JavaScript 生成的簡單圖像按鈕實(shí)現(xiàn)本地化:
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" />
本地化標(biāo)題的最佳技術(shù)是什么?
What's the best technique to localize the title of it?
PS:我發(fā)現(xiàn) Ayende 的解決方案.這是正確的方向.
PS: I found a solution by Ayende. This is the right direction.
我得到了提供 Controller.Resource('foo')
擴(kuò)展方法的本地化助手類.
I got Localization helper class which provides the Controller.Resource('foo')
extension method.
我正在考慮擴(kuò)展它(幫助程序),以便它可以按名稱返回指定控制器的所有 JavaScript 資源(來自 App_LocalResources
中的ClientSideResources"子文件夾).然后 - 在 BaseController
中調(diào)用它,將它添加到 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
.
這是個(gè)好主意嗎?
推薦答案
編輯
考慮將必要的本地化資源寫入 JavaScript 對象(哈希),然后使用它來查找動(dòng)態(tài)創(chuàng)建的對象.我認(rèn)為這比回到服務(wù)器進(jìn)行翻譯要好.這類似于通過 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";
如果沒有參數(shù),它將輸出整個(gè)翻譯哈希.使用參數(shù)使您能夠?yàn)槊總€(gè)視圖自定義它.
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() { ... } );
});
});
實(shí)際上,只要 JavaScript 代碼在容器中本地化,我就不會太在意將 JavaScript 代碼排除在我的視圖之外.通常我會設(shè)置我的母版頁有 4 個(gè)內(nèi)容區(qū)域:標(biāo)題、頁眉、主要和腳本.標(biāo)題、頁眉和主要內(nèi)容位于您所期望的位置,而腳本區(qū)域位于正文的底部.
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 包括在內(nèi),包括任何用于 viewusercontrols 的內(nèi)容,都放入腳本容器中.特定于視圖的 JavaScript 代碼位于包含之后.我根據(jù)需要將共享代碼重構(gòu)回腳本.我曾考慮使用控制器方法來整理腳本包含,即使用單個(gè)請求包含多個(gè)腳本,但尚未解決.
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 代碼分開以提高可讀性,但也允許我根據(jù)需要輕松地將模型或視圖數(shù)據(jù)注入到 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.
這篇關(guān)于如何處理 JavaScript 文件中的本地化?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!