久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何處理 JavaScript 文件中的本地化?

How to handle localization in JavaScript files?(如何處理 JavaScript 文件中的本地化?)
本文介紹了如何處理 JavaScript 文件中的本地化?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我希望將 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進(jìn)程中創(chuàng)建子窗口時(shí)如何修復(fù) BrowserWindow 不是構(gòu)造函數(shù)錯(cuò)誤) - IT屋-程序員軟件開發(fā)技術(shù)
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應(yīng)用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個(gè) loadURL 上執(zhí)行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內(nèi)使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應(yīng)用程序中訪問相機(jī)/網(wǎng)絡(luò)攝像頭?)
主站蜘蛛池模板: 日韩一区二区三区在线播放 | 色屁屁在线观看 | 国产精品永久 | 国产69精品久久久久777 | 午夜影院在线观看免费 | 狠狠伊人 | 亚洲一区二区久久久 | 欧美最猛黑人xxxx黑人 | 精品免费观看 | 狠狠婷婷综合久久久久久妖精 | 欧美激情a∨在线视频播放 成人免费共享视频 | 日韩免费| 69性欧美高清影院 | 一区二区成人 | 亚洲天堂一区 | 日韩久久久久 | 国产精品1区 | 欧美视频在线播放 | 亚洲欧美一区二区三区国产精品 | 国产欧美精品一区二区 | www.中文字幕.com | 狠狠插天天干 | 日韩免费在线视频 | 午夜一区二区三区视频 | 亚洲欧美一区二区三区1000 | 亚洲人成一区二区三区性色 | 欧美v在线 | 91影院在线观看 | 国内精品久久久久久影视8 最新黄色在线观看 | 久久久久国产 | 欧美韩一区二区 | 日韩精品a在线观看图片 | 久久99视频这里只有精品 | 91一区二区在线观看 | 欧美一级黄| 精品视频导航 | 国产探花在线精品一区二区 | 成年人网站免费视频 | 色资源在线 | 天堂网av在线 | 91精品久久久久久久久久 |