You can now take advantage of Localized Strings in your Android projects to better localize your text. Localized Strings are added to using the Constant Editor using these steps:
- Add a Constant, give it a name, default value and set its type to String.
- A Localized property will now appear in the Inspector. Turn it on.
- This constant will now appear in a new “Localized Strings” section in the Navigator for the project item.
- Use the “+” button to add a localized string, then set its Language and Value.
- Use this localized string in your project.
Below is a localized string that contains some translations of “thanks”.
The most common way to use this is to specify the localized string name as the text property for a control, such as a Label. You prefix the name with “#” to indicate it should be looked up at runtime.
Below you can see “#ThanksString” is set as the value for the Text property of Label1.
Note that “Default” is shown in the IDE because it is using the value for the “Default” language in the localized string. If there was no “Default” language, then the value specified in the Inspector as the “Default Value” is shown in the IDE.
If we then run this project on a device set to US locale, it displays “Thanks”:
If I change to a German locale and run again, it shows “Danke”:
To change the locale, open Settings on the Android device. Navigate to System and then Languages & input. There you will see the Languages option.
Choose that to see the list of languages supported by the device. Click the “+” to add a language. Here I’ve added German (Deutsch):
To make Deutsch the default, drag it so that it is first in the list. You’ll immediately see the UI change its language.
You can also specifically request a language in code by providing the locale as a parameter to the localized string. For example to get the French value:
Var french As String
french = ThanksString("FR")
// Variable french now contains "Merci".
Read the Introduction to App Localization documentation topic to learn more.
Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at Goto 10 and on Mastodon @lefebvre@hachyderm.io.