Localization refers to creating or modifying extension program code and YML markup so that it can be translated into multiple languages and also display localized strings, such as numbers, dates and times appropriately.
For example, for English you may display a string with a dynamic date as "Robot purcahsed on May 4, 2021.", where as in Japanese you'd like it to say "ロボットは2021年05月04日 (火曜日)で購入しました。" and in German "Roboter, der am 4. Mai, 2021 gekauft wurde.".
There are two places where you may wish to localize strings:
MessageFormat
and related classes.Text.text
property.
Text.text
properties.As a simple example, you might create one property file for English, named LanguageBundle_en.properties
that contains all the English strings, including:
purchased_on_date = Robot Purchased on {0,date,full}.
and another file LanguageBundle_ja.properties
for all the Japanese strings, including:
purchased_on_date = ロボットは{0,date,yyyy年MM月dd日 (EEEE)}で購入しました。
The translated strings can contain parameters to be substitued with arguments you supply, using {n}
. In this case, the first argument given (0) will be interpreted as a date with the specified date-specific format.
Then, after registing the files with the API, in your YML where you'd like to display the date you might write:
property string purchase_date: '2021-05-04'
Text {
text: tr('purchased_on_date', purchase_date)
}
and it will display in the appropriately localized format depending on the pendant language setting.
Notice that for English, we opted for the default 'full' date format, but for Japanese we opted to customize the format using explicit specifiers for the year (yyyy), month (MM), day (dd) and weekday (EEEE).
If you wish to update strings using the API, you can use the ResourceBundle
Java class. The ResourceBundle
class simplifies matters by allowing you to use the same property files you used for YML Text
. When instantiating the ResourceBundle
class, you must retrieve the locale setting from the API through the currentLocale()
method. This can be done as follows:
Locale locale = Locale.forLanguageTag(pendant.currentLocale())
Next, you can instantiate the ResourceBundle class using the locale and your property file collection as follows:
ResourceBundle resourceBundle = ResourceBundle.getBundle("LanguageBundle", locale);
To simplify matters, you can instantiate the Locale
and ResourceBundle
classes at the start of your extension so that the appropriate language and localization files are retrieved when your extension boots up.
When editing YML Text.text
properties through the API, you use the ResourceBundle
class object to retrieve the desired text. This is done using the getString()
method from the ResourceBundle
class. Simply pass the string parameter from your property to the getString()
method and it will retrieve the corresponding string based on your current locale. Using the purchased_on_date
key from before, you might invoke the API as follows:
pendant.setProperty("<YML_text_ID>", "text", resourceBundle.getString("purchased_on_date"));
If your locale was English, the text that would get passed would be "Robot Purchased on {0,date,full}". If the locale was Japanese, the passed text would be "ロボットは{0,date,yyyy年MM月dd日 (EEEE)}で購入しました".