Multi Language Support and Localization

These days, most applications are expected to be available in multiple languages. The JBStrap framework comes with a strong multi-language support and solution. You can use localized properties files. Each component, message, and any text in the application are stored in a properties file, so the developer can easily change and translate application texts.

The default language of the framework is English. If you want to use more languages, all you have to do is translate the appropriate properties file, rename it accordingly, and place it in the application’s resource folder.

All displayed texts are stored in the messages.properties file. This file is a regular properties file, in which the key values store the text, (as in key = value ). If you want to translate the application, all you have to do is make a copy of the properties file, rename it indicating the new language (e.g messages_de.properties for a German translation: this will take effect if the browser language is set to German). Do not change the keys, because the application won’t be able to find the texts. This new properties file will be loaded by the framework automatically, and it will be ready for use.

If the connected client has a language setting that does not have a corresponding properties file, the framework will resort to using the default messages.properties. This means that the application will be displayed in the default language.

If the application you developed displays static texts, we recommend that you create a new .properties file (as described above) to store these texts. These files will be loaded automatically when the framework launches, regardless of its name, and can also be localized. Text constants in the file can be easily queried and displayed (in the corresponding language) by using the MessageSourceAPI . To learn more about the MessageSourceAPI , please refer to the Utilities page.

Creating a file that contains custom text constants. The file will be named example.properties
Creating a file with the German translation of the above example. The file will be named example_de.properties:
Querying the above examples in the default language:
Querying the above examples in German:
Querying the above examples, according to the client’s language settings:

MessageSourceAPI

The JBStrap framework enables you to display the interface in multiple languages. To do this, you need to put static into a *.properties file and not directly into the source code. Static texts are stored in *.properties files in key-value pairs. If you want to display the application in multiple languages, you should create a *.properties file for each language.

You can freely choose any name for such files. Upon application start-up, the framework automatically reads all available files with the extension *.properties and stores their content. A prefix inserted after the file name indicates the specific language. If a file name contains no prefix, the framework processes the file as a generic file, meaning that if there is no corresponding static text for a specific key, the generic value is taken as default.

Using a *.properties file not only enables you to store language-specific static texts, but also parameters and API settings.

When starting the application, the framework automatically reads only already compiled *.properties files. You can also read single or multiple *.properties files. The framework handles these files the same way it handles automatically read files: static texts and parameters will be available using the API.

Example of a generic properties file. The example file will be named MESSAGE.PROPERTIES:
An example of a properties file that contains data to display in English. File name: MESSAGE_EN.PROPERTIES:
Example of a properties file that contains data to be displayed for German. File name: MESSAGE_DE.PROPERTIES:

If the above files are placed into the resource folder, they will be included in the WAR file created when compiling the application. Upon application start-up, the framework automatically reads the above files. You can access the files' contents via the Message Source API. If you specify a language code when reading the keys in the file, the value is determined using the language code. If no key is specified or the key is not found, the API returns a value based on the framework's default language. To set the default language of the framework, use the parameter DEFAULT_LANGUAGE .

An example of reading a key's value without specifying a language code. In the example, a comment contains the expected return value:

In method calls like this, values are returned only from default files.

Example of reading a value by entering a language code. As with the previous one, the return values will be included here in the comment:

These examples show that if a key cannot be found in the *.properties file for a specific language, the framework will attempt to search it in the default *.properties file. If it finds any, then the value in the file will be returned. On the other hand, if the key can be found in the language-specific *.properties file, the corresponding value will be returned.

In *.properties files, you can specify text constants that require parameters. This is how you can display text values passed as a parameter in run-time. This function may be very useful for error messages.

An example of specifying a key-value pair that expects two parameters:

As you can see in the example, the values to be replaced must be referenced by an ID between curly braces {}. When the key is read out, the specified parameters are replaced based on the ID.

Reading the above key using parameters:

You can also use such keys in localized files. The first parameter to be read must be the language code.

Example of reading the above key in the language specified:

Related pages