Text Translation

OFBiz externalizes text presented to the user by keeping it in UI label files. Three file formats are supported:

  1. *.properties file that contains the text in key=value format, one file per locale
  2. *.xml file using the Java property XML file format, one file per locale
  3. *xml file using the OFBiz property XML file format, one file for all locales

All new OFBiz development uses the OFBiz XML file format. This format is preferred because it is easy to edit using Unicode languages, and it also makes it easier to notice missing translations.

Translating Text In The Screen Widgets And Freemarker Templates

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:

<actions>
    <property-map resource="CommonUiLabels" map-name="uiLabelMap" global="true"/>
</actions>

where resource is the name of the UI label file (extension optional) and map-name is the name of the Map that will contain the contents of the UI label file. The keys in the file become the Map keys.

Here is an example of translated text in CommonUiLabels.xml:

<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:

<!-- Screen widget -->
<container style="h1"><label text="${uiLabelMap.CommonAddNew}"/></container>
<!-- 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 :

<!-- 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 :

<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 :

    <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 :

<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:

<property-to-field resource="CommonUiLabels" property="CommonAddNew" field="addNew"/>
<log level="info" message="${addNew}"/>

where resource is the name of the UI label file (extension optional) and field is the name of the variable that will contain the translated text.

Translating Text In Java

Translating text in Java is accomplished by using the org.ofbiz.base.util.UtilProperties class. Here is an example:

String myLabel = UtilProperties.getMessage(CommonUiLabels, "CommonAddNew", locale)