开发者

i18n in javascript using .properties file

I'm developing a web application with JSF, so I use a Java Properties File (i.e. resources_es_CO.开发者_C百科properties) to localize strings for the application and this is working OK.

Mi question is: how can I call localized strings in this kind of files from my javascript validations so alerts show those localized strings to user?

Thanks in advance.


What I do is to send the messages out as part of the page, dropped into hidden <span> tags with "id" values made from the property names.

Alternatively, you could write an Ajax-called action and fetch the properties dynamically.

To do an ajax callback, you'd have to implement a server-side action that would understand something like the property key. The server would just apply the localization (ie look up the property in the locale associated with the session) and then return the string. Alternatively, you could implement a service that'd return a whole set of properties, maybe on a per-form basis, or grouped according to some convention of property names (like, "return all properties that start with 'validation.addressForm'")

The simplest case would look something like this with jQuery:

$.get('/fetchProperty', { property: 'firstNameMissing' }, function(propValue) {
  $('#errMsg').text(propValue);
}, "text/plain");

Other frameworks provide similar ajax tools, or you could do the XMLHttpRequest yourself.


you could go to server with an ajax call and send alert texts from server to client and show it. or you could put messages to your page when your jsp's being rendered. both is ok. if you can change language without refreshing the page you probably want to make ajax call. if you can not , putting messages in javascript variables will be easier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜