Soluling home   Document home

Java Localization and Internationalization

Java Localization and Internationalization

Java (Wikipedia) is a popular platform independent programming language. Soluling localization tool and service support Java. Most Java applications use .properties file to store string resources. Mobile Java application can also use .res file to store strings resources. In addition to properties resources, most Java applications also use image file and can use any other file type. In most cases you package compiled java code (.class), resources (.properties and images) into a single JAR file. Soluling lets you choose if you want to localize a properties file or a JAR file. It is easier to localize a JAR file because the one single file contains all the items that you need to localize.

The end of the document contains links to get the full source code of the samples. After reading this document, we recommend reading a tutorial about how to use Soluling.

Localization process

Java applications are written in Java and compiled as Java byte code files (.class). When deploying compiled files and other files needed by the application are stored into single Java archive file (.jar). When you write an Java application you place resource files into your project. The files are either string resource files that contain string values, or media files such as images, or data files such as XML or JSON. Soluling can localize them all. Soluling lets you localize Java application in two ways. First is to localize resource files. This is called resource file localization. Second is to localize the JAR file. This is called JAR file localization.

Resource file localization

This localization method localizes the string resource files(s). String resource file is the most important resource type. It contains strings used by the application. The resource file extension is .properties. When Soluling localizes a resource file it read the original resource file (e.g. messages.properties) and creates localized versions of that file. For example if you localize into German Soluling will create messages_de.properties. Finally when you rebuild your Java application the localized resource files get included along the original resulting a multilingual JAR file.

Resource project If you want to use resource localization add a string resource file into Soluling project. Once Soluling has created the localized files recompile your application.

This method lets you to localize only string resources that exist on the properties files. In addition this method produces only multilingual application. If you want to localize some other resources such as images, you have to use either JAR file localization. If you want to create localized application that support only one language (for example to make application files smaller) you have to use the JAR file localization.

JAR file localization

When Soluling localizes a Java application it reads the original application package file (JAR file) and creates localized versions of that file. Application package file contains all the resources that need to be localized. There are several resource types. Most important for localization are the string and layout resources. Layout resources contains the user interface design of the application. String resource contains strings are are used by the application. In addition to these two resource types, application files contain also image resource and may also contain animation, XML or any custom resources. Soluling can localize them all.

JAR project If you want to use application localization add an Java application file into Soluling project. Once Soluling has created the localized application files they are ready to be deployed.

Comparing methods

The following table shows some pros and cons of each localization methods:

Method Pros Cons
Resource
  • Simple to use
  • Only strings resources can be localized
  • You need to recompile and sign your application each time you build localized files
  • Cannot (easily) create localized single language applications
JAR
  • Can localize all resources
  • Can create localized single language applications
  • No recompilation need but you can deploy the localized .jar right away
 

Resource types

Java applications can use several resource types.

String resources

The string resource (.properties) is the most important Java resource type. It contains one or more string values. If all strings of the application (from code and from layout files) are added into string resources all you need to do to localize is to translate the string resources.

The string resource source files are flat text files that contains one or more items. The following resource file, message.properties, contains one string resource. The name of the resource is "hello" and value is "Hello World".

name=Hello World

Other resources

In addition to the string resource types, an application file contains also image resource, XML, JSON or any custom resources. Soluling can localize them all.

Localized application vs. Multilingual application

When you write an Java application you can include resources in several languages. If you do that the compiled Java application file contains resources in several languages. Such an application is multilingual. When the application starts Java runtime checks if the application contains resource data for the default locale of the device. If the application contains resource in that locale they will be used instead of the neutral resources. The advantage of the multilingual application is that you only have one file to be deploy. All customers install the same file. The disadvantage of the multilingual application is larger file size compared to a monolingual application. If only strings and layout resources are localized then the size difference is minimal but if you start localizing image or other media files the multilingual files gets a lot bigger than the monolingual one. In such case it is better to create many monolingual applications each support one language (e.g. English version, German version, etc).

Most applications are monolingual by default and in almost every case the language of the strings and UI is English. If you select a monolingual JAR file into a Soluling project you have two choices about the output files. Soluling can either create localized application files (one for each language) or one multilingual application file that contains resources in all languages you have added into the Soluling project. You can even make Soluling to create both output types. The following picture contains simplified structure of an original Java application file. It contains compiled Java code and resources. Some resources are language neutral and there is not need to localize them. Such resources may be the image and layout resources. Some resources contain English text and must be localized. Such resources are the string and layout resources. Some resources are language neutral and do not require localization. Such resources are for example most images.

Original application files

Let's suppose that you have added the above file into Soluling project and have added German and French as target languages. Now Soluling has two output options. One is to make multiple monolingual but localized application files. The following picture contains German and French application files created by Soluling. They both are similar to the original file except the English resource has been replaced with German or French.

German application file

French application files

The above solution works fine but requires to have multiple application files. The user has to choose the right version. An easier solution would be one multilingual file. This also is possible. The second output options is to create a multilingual application file. It contains the same code and resources as the original file but it also contains German and French resources. Each resource data can have a language id. This makes it possible to include the same source more than once if each resource has different language id. The following picture contains such a file. Original neutral resource and English resource have no language id. German resource is the same as English resource but instead of having no language id it has German language id. French resource has French language id. Multilingual application file is easy to deploy because you only install one file and all customer get the same file. When the application is run on Java runtime having English as active language, the application will start in English. On German Java runtime it will start in German and so on.

Multilingual application files

If your original application file is multilingual the process is the same except Soluling automatically imports the existing translations from the original application file. The output file is either localized files that contains resources in one language or one multilingual file that contains resources of the original languages plus those languages you have added into the Soluling project. If you start a new Java project from scratch we recommend that your original application would be monolingual. If you want to make it multilingual let Soluling to create the multilingual application file.

Properties file

Properties file (Wikipedia) is used to store string resources of a Java application. Soluling reads original properties file (.properties) to extracts strings. Finally when building Soluling creates localized properties files. There is one output file (.properties) for each language. For example we have Resources.properties. It contains the original English strings of our Java application. If we localized our application into German we need to create Resources_de.properties file that contains the same items as the original file but value are in German.

JAR files

JAR file (Wikipedia) is a deployment package of a Java application. The file itself is a compressed zip file that contains all the files that the application need. You start localization by creating a new Soluling project that contains the jar file. Soluling read the file extracting the resource items. Finally when building Soluling creates localized JAR file one for each language, or one multilingual JAR file that contains resources in many languages.

Plurals in Java

Standard Java code uses String.format function. It is not plural enabled but fortunately Soluling contains a plural enabled Format function for Java.

If you use standard Format function you might have something like this

str = String.Format("%d files",  count);

Replace "%d files" with "one;%d file;other;%d files" and add that into the properties file and give it MessagePlural name. The modified code is

str = Soluling.Plural.format(res.GetString("MessagePlural"), count, count); 

Soluling.Plural.format function is found from <data-dir>\Library\Java\Plural.java

See <data-dir>\Samples\Java\Plural sample.

Samples

GitHub and <data-dir>\Samples\Java contains following source code samples:

Directory Description Notes
Driving A sample that shows how to localize a Java application. Try this first!
Image A sample that shows how to localize images.  
Plural A sample that shows how to use plural messages.  

Configuring Java Localization

You can configure how to localize your Java application by selecting the item in the project tree, right-clicking, and choosing the Options menu. A source dialog appears that lets you edit the options. This source uses the following option sheets.