最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - accessing resource property file in *.js file - Stack Overflow

programmeradmin1浏览0评论

Hi my project is in struts 2. We have written mon 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.

Hi my project is in struts 2. We have written mon 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.

Share Improve this question edited Jul 13, 2012 at 16:38 kiranvj 34.2k8 gold badges75 silver badges79 bronze badges asked Jul 13, 2012 at 15:36 VipulVipul 8364 gold badges14 silver badges26 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Store the alert messages in js files with file names like

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.

<script type="text/javascript" src="YOUR-FILE.JSP"></script>

In this JSP file you output JavaScript after reading from resource bundle.

Check this also.

You can read properties file in js file using messageResource.js library created by me.

1) Include messageResource.js in your html.

<script src="messageResource.min.js"></script>

2) You can access key-value pair of properties file from js as follows.

// 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 plete.
messageResource.load('moduleName', callbackFunction, 'en_US'); 

// use messageResource.get function to get values from loaded file. 
var value = messageResource.get('key', 'moduleName', 'en_US');

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])

I do this in my javascript within script tags on the jsp pages :

<code>
    alert("<s:text name="amountMustBeNumeric"/>");
</code>

Works fine for me.

发布评论

评论列表(0)

  1. 暂无评论