...
- *.properties file that contains the text in key=value format, one file per locale
- *.xml file using the Java property XML file format, one file per locale
- *xml file using the OFBiz property XML file format, one file for all locales
...
Before the translated text can be used in a web page, the appropriate UI label file must be loaded into memory using the screen widget actions element:
Code Block |
---|
|
<actions>
<property-map resource="CommonUiLabels" map-name="uiLabelMap" global="true"/>
</actions>
|
...
Here is an example of translated text in CommonUiLabels.xml:
Code Block |
---|
|
<property key="CommonAddNew">
<value xml:lang="ar">إضافة الجديد</value>
<value xml:lang="de">Neu hinzufügen</value>
<value xml:lang="en">Add New</value>
<value xml:lang="es">Crear nuevo</value>
<value xml:lang="fr">Ajouter un nouveau</value>
<value xml:lang="it">Aggiungi nuovo</value>
<value xml:lang="nl">Voeg een nieuwe toe</value>
<value xml:lang="pt">Adicionar Novo</value>
<value xml:lang="ro">Adauga Nou</value>
<value xml:lang="ru">Добавить новый</value>
<value xml:lang="th">เพิ่มใหม่</value>
<value xml:lang="zh">新建</value>
<value xml:lang="zh_CN">添加新的</value>
</property>
|
If we want to display this text, we simply reference it using String expansion:
Code Block |
---|
|
<!-- Screen widget -->
<container style="h1"><label text="${uiLabelMap.CommonAddNew}"/></container>
|
Code Block |
---|
|
<!-- Freemarker template -->
<h1>${uiLabelMap.CommonAddNew}</h1>
|
The text will be translated into the user's language.
Translating Text In Form Widgets
Labels used in forms have to be loaded in memory from parent screens. The map uiLabelMap loaded in screens can be accessed within included forms like :
Code Block |
---|
|
<!-- Form widget -->
<field name="firstName" title="${uiLabelMap.PartyFirstName}" tooltip="${uiLabelMap.CommonRequired}" widget-style="required"><text/></field>
<field name="description" title="${uiLabelMap.CommonDescription}"><text/></field> |
Specific Rules :
It is not mandatory for a field title to be specified like in the previous example (${uiLabelMap.PartyFirstName}). If title property is not given, the form renderer will look for a specific property within uiLabelMap using the rule : FormFieldTitle_${field-name}.
In our case for firstName field :
Code Block |
---|
|
<property key="FormFieldTitle_firstName">
<value xml:lang="de">Vorname</value>
<value xml:lang="en">First Name</value>
<value xml:lang="es">Primer nombre</value>
<value xml:lang="fr">Prénom</value>
<value xml:lang="hi_IN">प्रथम नाम</value>
<value xml:lang="it">Nome</value>
<value xml:lang="ja">姓(FN)</value>
<value xml:lang="nl">Voornaam</value>
<value xml:lang="pt_BR">Nome</value>
<value xml:lang="ro">PreNume</value>
<value xml:lang="ru">Имя</value>
<value xml:lang="th">ชื่อ</value>
<value xml:lang="vi">Tên</value>
<value xml:lang="zh">名字</value>
<value xml:lang="zh_TW">名字</value>
</property> |
Display-entity field can be translated within each language using another specific property file. Using the following example as illustration :
Code Block |
---|
|
<field name="statusId"><display-entity entity-name="StatusItem" key-field-name="statusId" description="${description}"/></field> |
If nothing is configured, such display entity will display the content of the description attribute from the StatutItem entity record, having in primary key the value of statusId. But only one description can be stored in the database.
In *EntityLabels.xml property files (which have to be loaded like other property files) are defined the translations of such case :
Code Block |
---|
|
<property key="StatusItem.description.CAL_CANCELLED">
<value xml:lang="de">Annulliert</value>
<value xml:lang="en">Cancelled</value>
<value xml:lang="es">Cancelado</value>
<value xml:lang="fr">Annulé</value>
<value xml:lang="it">Cancellato</value>
<value xml:lang="ja">取消</value>
<value xml:lang="nl">Gannuleerd</value>
<value xml:lang="pt">Cancelado</value>
<value xml:lang="ro">Sters</value>
<value xml:lang="ru">Отменен</value>
<value xml:lang="th">ยกเลิก</value>
<value xml:lang="vi">Hủy bỏ</value>
<value xml:lang="zh">已取消</value>
<value xml:lang="zh_TW">已取消</value>
</property>
<StatusItem description="Cancelled" sequenceId="99" statusCode="CANCELLED" statusId="CAL_CANCELLED" statusTypeId="CALENDAR_STATUS"/> |
Then the display-entity field will display the translation of "Cancelled" for the given record of status item using the identifiing rule : EntityName.FieldToDisplay.ValueOfPK
Translating Text In Mini-Language
The process is similar in Mini-Language, but the syntax is slightly different:
Code Block |
---|
|
<property-to-field resource="CommonUiLabels" property="CommonAddNew" field="addNew"/>
<log level="info" message="${addNew}"/>
|
...
Translating text in Java is accomplished by using the org.ofbiz.base.util.UtilProperties
class. Here is an example:
Code Block |
---|
|
public static Map<String, Object> myJavaService(DispatchContext ctx, Map<String, ?> context) {
Locale locale = (Locale) context.get("locale");
try {
// Do something risky
...
} catch (Exception e) {
return ServiceUtil.returnError(String myLabel = UtilProperties.getMessage(MyUiLabelsCommonUiLabels, "RiskyErrorMessageCommonAddNew", locale));
}
...
}
|