views:

132

answers:

3

Hello: I'm developing a web application with JSF, so I use a Java Properties File (i.e. resources_es_CO.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.

A: 

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.

Pointy
A: 

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

yilmazhuseyin
A: 

Just in case anyone else is interested in a solution for this question, I've found a jQuery plugin which matches exactly my needs.

Its name is jQuery.i18n.properties and the URL where I found it is http://codingwithcoffee.com/?p=272

Now I'll just to try it, but what it offers is exactly what I need. If anyone has experience with this plugin (whether good or bad) please share it with us. Thank you.

nihcap