简体   繁体   English

Javascript资源文件(.js)与资源文件(.resx)

[英]Javascript resource file (.js) vs resource file (.resx)

I'm wondering what's the best solution in my case. 我想知道什么是最好的解决方案。 I got the following setup: 我有以下设置:

  • ASP page with usercontrols 带有用户控件的ASP页面
  • Knockout.js Knockout.js
  • WCF services called by js js调用的WCF服务
  • Multilangual Multilangual

I'm doing json back and forth calls between my webservice and my usercontrol. 我正在Web服务和用户控件之间进行json来回调用。 Since the application needs to be multilangual, I thought about storing some of my resources in a javascript file (sent from the webservice and messages to users), so that when for example an error is thrown in the service, I return a error code (=ID) (example: "TECH_4125") and get the resource from my javascript file in the correct language, with help of the lcid. 由于应用程序需要是多语言的,因此我考虑将一些资源存储在javascript文件中(从Web服务和消息发送给用户),以便例如在服务中引发错误时,我返回错误代码( = ID)(例如:“ TECH_4125”),并在lcid的帮助下以正确的语言从我的javascript文件中获取资源。

Since javascript files are stored in cache, I thought this would be the best way to do this (to improve performance??? -> but of course you have more resources than needed on each page) Another reason to use this is because we can reuse some of the resources on other pages. 由于javascript文件存储在缓存中,所以我认为这将是实现此目的的最佳方法(以提高性能????->当然,您在每个页面上的资源都比需要的要多)。另一个原因是因为我们可以重用其他页面上的某些资源。

Example of a javascript resources JavaScript资源示例

  • resources-2067.js 资源-2067.js

     var MessageCode={"NOT_0003":{"Label":"Geen resultaten gevonden","Display":"1 - Geen resultaten gevonden"}}; 
  • resources-2057.js 资源-2057.js

     var MessageCode={"NOT_0003":{"Label":"No results found","Display":"1- No results found"}}; 
  • resources-1031.js 资源-1031.js

     var MessageCode={"NOT_0003":{"Label":"Aucun résultat trouvé","Display":"1- Aucun résultat trouvé"}}; 

(+ I'm getting my resource label with for example: MessageCode.NOT_0003.Label) (+我正在获取资源标签,例如:MessageCode.NOT_0003.Label)

What would you prefer and why? 您想要什么,为什么?

As a first principle I think you should use things for what they are made for. 作为首要原则,我认为您应该将事物用于其用途。 JavaScript files are for JavaScript code and not data storage. JavaScript文件用于JavaScript代码,而不用于数据存储。

If you have not many of these, then I guess it's ok to create a config.js where you store you generic error messages (When errors are about your Javascript code). 如果您没有很多,那么我想可以创建一个config.js在其中存储常规错误消息(当错误与您的Javascript代码有关时)。

But in your case you seem to have many cultures you are supporting and for each you have many error messages. 但是在您的情况下,您似乎支持多种文化,每种文化都有许多错误消息。 Furthermore your errors are about database data such as contact not found etc. 此外,您的错误与数据库数据有关,例如找不到联系人等。

So you need to Send those errors as a part of your Ajax response to the client. 因此,您需要将这些错误作为Ajax响应的一部分发送给客户端。 Your JavaScript then should be passing those error messages to the user. 然后,您的JavaScript应该将这些错误消息传递给用户。

You can determine what culture those responses should be in using either user Id, visited country url etc. which is an application wide constant and can be mapped to a culture. 您可以使用用户ID,访问的国家/地区网址等来确定这些响应应采用的文化,这是应用程序范围内的常量,可以映射到文化。

What you want to end up with is a JavaScript that preferably has no knowledge of culture but simply passing relevant error messages based on your error codes. 您最终想要得到的是一个JavaScript,它最好不了解文化,而只是根据您的错误代码传递相关的错误消息。

Conclusion: 结论:

keep your resx files or store in db as return appropriate error message to the client based on the culture without involving the client side. 保留resx文件或将其存储在db中,以根据区域性将适当的错误消息返回给客户端,而无需涉及客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM