This demo project shows the handling of multiple languages within the UI. See following detailed informations:
- How to localize normal UI elements like XdevButtons, XdevTextFields, XdevLabels
- How to localize XdevTableHeader or how to use localization in entities
- How to localize dynamic messages and notifications
- How to keep localization after click on browser refresh
- The most common naming strategies and patterns
- The most common resource file structure
- Clone the GIT Reporsitory or use the import function of RapiClipse
- File -> Import -> RapidClipse -> Demo Projects -> "rapidclipse-demo-localizedUI"
- Maybe do some Maven updates to remove project errors
- Start the UI at "QuickLaunch"
At the MainView you will see serveral UI Elements like Buttons, Textfields and also a MenuBar. Just select one of these elements and you will see that some attributs like 'Caption' or 'Text' is filles with content that looks like the following
{$btnLanguageGerman} or {$menuData.text}
or in the Code, there are some lines like the following
this.btnLanguageGerman.setCaption(StringResourceUtils.optLocalizeString("{$btnLanguageGerman.caption}", this));
This pattern is the basic indicator for the RapidClipse framework to get the real value from a resource file.
Some important informations about the ressource files:
- The resource files have to has the same name like the supported UI File e.g. MainView -> MainView_de.properties
- The second part of the resource file name is the language indicator '_de' for German or '_en' -> English.
- The resource file has to be in the same folder / package like the supported UI File
The contend of the resource files is quite simple:
btnSave.caption = Speichern
txtName.caption = Vorname
lbldescription.value = Beschreibung
Left side = name of the variable, right side = value of the variable
Naming strategies for resource files:
Example Name | Range |
---|---|
MainView_de.properies | A resource file exclusive for the specified file in this case the 'MainView'. |
CustomerUI_de.properies | If 'CustomerUI' is a JavaPackage then the resource file is related to the whole Package and all files in it. In this case the resourcefile should be part of the parent folder of the related one. |
rapidclipse-demo-localizedUI_de.properies | If 'rapidclipse-demo-localizedUI' is the projectname the resourcefile is related to the whole project. In this case the resource file should be in the resource folder. |
see 'MainView'
StringResourceUtils.optLocalizeString("{$btnMessage.message}", this)
see 'MainView'
//Changes the language for the current session
UI.getCurrent().setLocale(Locale.ENGLISH);
//Save the current language to the current session
UI.getCurrent().getSession().setAttribute(Locale.class, Locale.ENGLISH);
//Navigates to the itself. Best way to refresh the UI
Navigation.to("").navigate();
//Changes the language for the whole application
Locale.setDefault(Locale.ENGLISH);