artejera
artejera

Reputation: 1486

Java JRE: How to add localized resources to stantard JRE resources

I need the JRE to use translated versions of a JRE resource that is available only in English.

As per the ResourceBundle.java doc, it's easy: add localized resources with the right locale suffix. For example, the standard

would become a translated version:

And so on.

EDIT: this particular file lives in : com\sun\org\apache\xerces\internal\impl\msg\XMLSchemaMessages.properties

My question is: how do I make those extra resources visible to the JRE ?

Thanks in advance, for any help. -- cheers

Upvotes: 3

Views: 2830

Answers (3)

Baca
Baca

Reputation: 31

For next generation, my solution - add dependency to your pom "xercesImpl" (check the newest on https://mvnrepository.com/artifact/xerces/xercesImpl):

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.12.2</version>
</dependency>

This will cause search for XMLSchemaMessages_<lower_case_language_code>.properties files in the project's classpath. If your project uses an external source of this library, e.g. it is run on wildfly, then without adding the library to the project, files will be searched outside your project in the wildfly namespace. Place the file

XMLSchemaMessages_<lower_case_language_code>.properties

in the directory

src/main/resources/org/apache/xerces/impl/msg

and that's all. Should work.

Upvotes: 0

DAB
DAB

Reputation: 1873

The above answers took me a little while to work out.

Just to make it easier for others, here's my summary of how to get Locale specific error messages to appear if you try parsing an XML document using XML schema with Java's internal Xerces parser:

Find an appropriate properties file in the format

XMLSchemaMessages_<lower_case_language_code>.properties

For Italian I found XMLSchemaMessages_it.properties on the following site (which might be an old version, but it worked for me)

http://grepcode.com/file/repo1.maven.org/maven2/com.sun.xml.parsers/jaxp-ri/1.4.5/com/sun/org/apache/xerces/internal/impl/msg/XMLSchemaMessages_it.properties

I then created a directory structure in my temp directory to hold the new file

com\sun\org\apache\xerces\internal\impl\msg

Since jar files and zip files share the same format (and I'm lazy and today I was using Windows), I then zipped the above com directory, creating a file called com.zip. I then changed the name of the file

rename com.zip to XMLSchemaMessages_Locale.jar

and then moved the new jar file to

C:\Program Files\Java\jdk1.7.0_04\jre\lib\ext

Of course the above path depends on your platform and specific version of Java (I was using Windows 7).

Instead of zipping, if you have JDK you could easily build the jar file using the command-line jar command, from Unix, Linux or Windows.

Upvotes: 2

Assuming that Xerces uses ResourceBundle to get the messages, you should put a new file in

com\sun\org\apache\xerces\internal\impl\msg\XMLSchemaMessages<locale>.properties 

where locale is a correct identifier for the locale you need.

Then pinpoint the exact location where the XMLSchemaMessages resource bundle is loaded, and set a breakpoint so you single step through the ResourceBundle loading procedure in the JRE (a JDK is recommended here, so you have source for the runtime) and you can see what is being searched for.

Note: You are dealing with a vendor specific XML Parser here meaning this will be Oracle specific and may even only work on some Java versions. Considered bringing in your own validating XML Parser and localize it instead?

Upvotes: 2

Related Questions