javascript 访问 *.js 文件中的资源属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11473705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing resource property file in *.js file
提问by Vipul
Hi my project is in struts 2. We have written common js files for client side validation. now the problem is that to implement internalization we have to change alert message as per the language. so my question is that is there any way to access resource property in js file. or any one suggest some alternative or example for the same.
嗨,我的项目是在 struts 2 中。我们已经编写了用于客户端验证的通用 js 文件。现在的问题是,要实现内化,我们必须根据语言更改警报消息。所以我的问题是有什么方法可以访问 js 文件中的资源属性。或任何人提出一些替代方案或示例。
回答by kiranvj
Store the alert messages in js files with file names like
将警报消息存储在 js 文件中,文件名类似于
alert_en.js alert_fr.js alert_jp.js
alert_en.js alert_fr.js alert_jp.js
In each file store the alerts like this
在每个文件中存储这样的警报
var ALERT_EMAIL_INCORRECT = "Incorrect email";
var ALERT_USERNAME_INCORRECT = "Incorrect username";
Include file as per the user languages.
根据用户语言包含文件。
OR
或者
You can load messages from the resource bundle using a JSP file and link in your page like this.
您可以使用 JSP 文件和页面中的链接从资源包中加载消息,如下所示。
<script type="text/javascript" src="YOUR-FILE.JSP"></script>
In this JSP file you output JavaScript after reading from resource bundle.
在这个 JSP 文件中,您从资源包中读取后输出 JavaScript。
Check thisalso.
也检查这个。
回答by Khan
You can read properties file in js file using messageResource.jslibrary created by me.
您可以使用我创建的messageResource.js库读取 js 文件中的属性文件。
1) Include messageResource.js in your html.
1) 在你的 html 中包含 messageResource.js。
<script src="messageResource.min.js"></script>
2) You can access key-value pair of properties file from js as follows.
2) 您可以从 js 访问键值对属性文件,如下所示。
// initialize messageResource.js with settings
messageResource.init({
// path to directory containing message resource files(.properties files),
// give empty string or discard this configuration if files are in the
// same directory as that of html file.
filePath : 'path/messageresource/'
});
// will load the file 'path/messageresource/moduleName_en_US.properties'
// and callbackFunction will be executed when loading is complete.
messageResource.load('moduleName', callbackFunction, 'en_US');
// use messageResource.get function to get values from loaded file.
var value = messageResource.get('key', 'moduleName', 'en_US');
回答by user1524017
You can write a JSON file with the native English language as key and L10N message as the value, and use AJAX to load the related JSON depending on user's browser language configuration, and alert the message with alert(tanslatedTable[USER_LANG][ENGLISH_STRING])
您可以编写一个以英语母语为key,L10N消息为值的JSON文件,根据用户浏览器语言配置使用AJAX加载相关JSON,并使用alert(tanslatedTable[USER_LANG][ENGLISH_STRING])
回答by Spunog
I do this in my javascript within script tags on the jsp pages :
我在jsp页面上的脚本标签内的javascript中执行此操作:
<code>
alert("<s:text name="amountMustBeNumeric"/>");
</code>
Works fine for me.
对我来说很好用。