访问* .js文件中的资源属性文件

嗨我的项目是struts 2.我们已经编写了常见的js文件进行客户端validation。 现在的问题是,要实现内部化,我们必须根据语言更改警报消息。 所以我的问题是有没有办法访问js文件中的资源属性。 或任何人建议一些替代或相同的例子。

将警报消息存储在具有文件名的js文件中

alert_en.js alert_fr.js alert_jp.js

在每个文件中存储这样的警报

var ALERT_EMAIL_INCORRECT = "Incorrect email"; var ALERT_USERNAME_INCORRECT = "Incorrect username"; 

根据用户语言包含文件。

要么

您可以使用JSP文件从资源包加载消息,并在页面中链接,如下所示。

  

在此JSP文件中,您在从资源包读取后输出JavaScript。

检查一下 。

您可以使用我创建的messageResource.js库在js文件中读取属性文件。

1)在你的html中包含messageResource.js。

  

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'); 

您可以使用本机英语作为键和L10N消息作为值编写JSON文件,并使用AJAX根据用户的浏览器语言配置加载相关的JSON,并使用alert警告消息(tanslatedTable [USER_LANG] [ENGLISH_STRING])

我在jsp页面上的脚本标签中的javascript中执行此操作:

  alert("");  

对我来说很好。