API for translations

Introduction

The APIs described below are used to read/write strings translated inside the LocalizationDictionary objects. They are supplied by the InformationModel static class.

The table below indicates the APIs to use for various purposes:

Purpose

API

Read a translation based on the session locale.

InformationModel.LookupTranslation(localizedText)

Read the first translation available from multiple locales of interest.

InformationModel.LookupTranslation(localizedText, localeIds)

Adds a key, and optionally a translation, in a LocalizationDictionary.

InformationModel.AddTranslation(localizedText, localizationDictionary)

Update a translation associated with an existing key.

InformationModel.SetTranslation(localizedText)

LocalizedText class

Every API has an instance of the LocalizedText C# class among its own arguments, which identifies a key/line or specific string translated in a LocalizationDictionary. It contains the following properties:

  • NamespaceIndex int: identifier of the namespace of the project to which the LocalizationDictionary belongs.

    Note

    if the LocalizationDictionary is in the same NetLogic project, it is not necessary to specify the NamespaceIndex in the construction of a LocalizedText.

  • TextId string: key with which the translations of a string can be associated.

  • LocaleId string: locale ID (e.g.: en-US).

  • Text string: translated string.

InformationModel.LookupTranslation(localizedText)

Reads the translations associated with a key based on the NamespaceIndex and TextId properties of the argument, and returns a LocalizedText object whose LocaleId and Text properties are assigned a value based on the session locale.

LocalizedText LookupTranslation(LocalizedText localizedText);

Arguments

localizedText LocalizedText

C# object whose TextId and NamespaceIndex properties identify the key of interest.

Returns

LocalizedText

C# object whose Text and LocaleId properties are assigned a value based on the key provided and based on the session locale.

Example

Below is an example in which the API returns a translation object of the LocalizedText type that contains the requested translated string, associated with the Key1 key. The API argument is a LocalizedText object created using a constructor whose argument defines its TextId property.

The translation object is used to set the text of a label1 label based on the session locale.

var myLocalizedText = new LocalizedText("Key1");
var translation = InformationModel.LookupTranslation(myLocalizedText);
var label1 = Owner.Get<Label>("Label1");
label1.Text = "Translation: " + translation.Text;

InformationModel.LookupTranslation(localizedText, localeIds)

Reads the translations associated with a key based on the NamespaceIndex and TextId properties of the first argument, and returns a LocalizedText object whose LocaleId and Text properties are assigned a value based on the first translation available between the locales supplied in the second argument.

LocalizedText LookupTranslation(LocalizedText localizedText, List<string> localeIds);

Arguments

localizedText LocalizedText

C# object whose TextId and NamespaceIndex properties identify the key of interest.

localeIds List<string>

Locales of interest expressed with locale IDs. Any IDs after the first indicate the fallback locale.

Returns

LocalizedText

C# object whose LocaleId and Text properties are assigned values based on the first locale available among those indicated in the localeIds argument.

Note

if a string for the indicated locale is not available, the LocaleId and Text properties remain empty.

Example

Below is an example in which the API returns a translation object of the LocalizedText type, which represents the Key2 key of a LocalizationDictionary. The first API argument is a LocalizedText object created using a constructor whose argument defines its TextId property. The second argument is a list that contains the IDs for the locales of interest (it-IT and es-ES).

The translation object is used to set the text of a label based on the first available translation between it-IT and es-ES.

var myLocalizedText = new LocalizedText("Key2");
var translation = InformationModel.LookupTranslation(myLocalizedText, new List<string>() { "it-IT", "es-ES" });
var label2 = Owner.Get<Label>("Label2");
label2.Text = "Translation: " + translation.Text;

InformationModel.AddTranslation(localizedText, localizationDictionary)

Adds a new key in the project LocalizationDictionary, whose name corresponds to the value of the TextId property of the first argument. If the first argument also contains the LocaleId and Text properties, also adds the translated string. The second argument, optional, indicates the LocalizationDictionary to which the key is added.

Important

it is necessary to specify the second argument if the project contains more than one LocalizationDictionary.

void AddTranslation(LocalizedText localizedText, IUAVariable localizationDictionary);

Arguments

localizedText LocalizedText

C# object whose properties identify the key and any translated string to add.

localizationDictionary IUAVariable

C# object that represents the desired LocalizationDictionary.

Example

Below is an example in which the API adds a NewKey new key and a New translation string for the en-US locale.

var myLocalizedText = new LocalizedText("NewKey", "New translation", "en-US");
InformationModel.AddTranslation(myLocalizedText);

InformationModel.SetTranslation(localizedText)

Update a translation associated with the desired existing key, based on the argument properties.

void SetTranslation(LocalizedText localizedText);

Arguments

localizedText LocalizedText

C# object whose TextId, Text, and LocaleId properties identify the key and related translation to be updated.

Example

Below is an example in which the API writes the New translation string for the en-US locale associated with the Key1 key.

var localizedText = new LocalizedText("Key1", "New translation", "en-US");
InformationModel.SetTranslation(localizedText);

See also

Related concepts

Sessions and locale

Related procedures

Manage the translations

Application examples

Import translations from another project

References

Translation editor