wood burning stoves 2.0*
The moose likes Struts and the fly likes Internationalization: variable language and validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Internationalization: variable language and validation" Watch "Internationalization: variable language and validation" New topic
Author

Internationalization: variable language and validation

Marc Schipperheyn
Greenhorn

Joined: Mar 15, 2005
Posts: 7
Hi,

I have a country specific validation to ensure proper country specific zipcode checks. So, to get his, the locale has to be set to _US. I want the language to be variable per country site. So one country site could have english, french and italian. But that same english, french and italian language file is valid for other country sites as well. So in order to have this flexibility and not have duplicate files for every country, the language files should be _en, _fr and _it.

Problem is, when I set the Locale to _US, the country specific language file doesn't get loaded. When I set the Locale to _en, the country specific validation file isn't loaded. When I set the Locale to en_US, the country validation file is loaded, but the language file isn't. And I *really* don't want to have tens of identical language files.

Is there a way around this?

Kind regards,

Marc
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
marc,
here's how i would approach internationalization together with country-specific zip code validation. i need to start from the basic and work the solution so see if it's feasible and compare/contrast what you have you may be able to work out something.

1. 3 props for containing a zip.format key (i don't know what the other 2 are)
. ApplicationResources.properties - zip.format = "12345";
. ApplicationResources_it.properties - zip.format = "1214 134";
. ApplicationResources_fr.properties - zip.format = "aff 143";

each file should have something like
errors.zipformat = Your zip must be in the format {0};
w/corresponding translations.

2. <html:html locale="true">

so far, this is only to display language per the browser's language setting. i am sure you know this.

3. add custom validation (assuming you're using the struts validation framework and know how to set this up, if not, i can walk you through it)
. inside:
- get locale by request.getLocale()
- determine the zip mask based on the locale and return true/false. you can have a properties file containing the keys of the locale
en_US = ^((\d{5}))$
fr = blah
it = blah
and use a expression util to evaluate the validity based on the mask (or however you validate that now).

- upon failed validation, add a new ActionMessage containing the key to the message "errors.zipformat" (errors.add(Globals.ERROR_KEY,new ActionMessage("errors.zipformat") the and the "zip.format" must be added as your arg0 parameter in your validation.xml).

short of doing this myself, i am 99% positive that this approach will work unless you have constraints i don't know about.


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
Marc Schipperheyn
Greenhorn

Joined: Mar 15, 2005
Posts: 7
Hi,

Well, the only thing in your approach is that I can't use en_US for the validation, since the validation is country, not language specific.

With Struts I set the locale in a custom request processor:
protected void processLocale(HttpServletRequest req, HttpServletResponse res) {
req.getSession().setAttribute(Globals.LOCALE_KEY, Config.COUNTRY);
}

Config.Country contains a Locale object with ("","US") as configuration to load the proper zipcode validation or ("en","") to use the proper language file. If I set it to ("en","US"), I get those language key faults where it tries to access a key by ???en_US.somekey???

So, it would seem I would need to be able to access message resources based on a ("en","") Locale and validation files based on a ("","US") Locale.

Kind regards,

Marc
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
i realized exactly the problem you're having now. short of doing codes myself to figure it out, if all else fails, you can always externalize your masks and formats to avoid duplicate language files. see if this makes sense.

this approach puts the mask for each locale into a separate resource bundle, not based on the language resource. this gets your around the dependency on the ApplicationResource for the zip.mask is to do

CountryZipMappings.properties

en_US = usRegExMask | 12345
fr = frRegExMask | 00000
fr_BE = fr_beRegExMask | 000 00

RegExMask would be the regex for each. when added the error to the message, you'll have to tokenize the properties value to add validation error. something like this?

errors.add(Globals.ERROR_KEY,new ActionMessage("errors.zipformat",Utils.getZipMask(locale));

1. errors.zipformat = Your postal code must be in the following format: {0}

2. Utils.getZipMask() is where you would tokenize and get the corresponding displayable zip mask.

please do let me know if you finally get it to work and how. i am very interested. thanks.
Marc Schipperheyn
Greenhorn

Joined: Mar 15, 2005
Posts: 7
Apparently, the kind developers of Struts already thought of this scenario: this is their reply:

PropertyMessageResources first searches the specific locale (e.g.
_nl_US.properties) then less specific (i.e. _nl.properties) and then the default locale. This would seem OK in you case for language resources. For the validation-constants you could create an alternative MessageResources implementation that after checking the specific locale (e.g. _nl_US.properties) then checks for the country (i.e. _US.properties). Then all you need to do is have two separate bundles - one for language resources and the other for validation-constants, and configure your custom MessageResources implementation for the validation-constants bundle.

You would need to create MessageResources and MessageResourcesFactory implementations to do this. Then in the struts-config.xml...

<message-resource parameter="MyMessageResources" />
<message-resource parameter="MyValidationResources" key="validation"
factory="yourPackage.CountryMessageResourcesFactory" />

Marc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Internationalization: variable language and validation
 
Similar Threads
change client's locale
java platform independent?
Question on i18n
internationalization: a rock and a hard place
internationalization & Properties file order (gone south)