Localization

A separate library is available for localizing messages. It can be used to output messages in the current user’s language setting.

Source: shareScriptDir/cm/serverCmds/lib/localizer.tcl

Usage

After calling

::localizer::new

the current namespace provides the commands localizer, localize, as well as localizeWithLanguage.

localizer serves to associate a message with a key. Placeholders such as %version% in the example below can be used.

Example

localizer addBulk de {
  "word" "Wort"
  "This is version %version%." "Das ist Version %version%."
  "Permitted types: %types%" "Erlaubte Vorlagen: %types%"
}

For localizing messages, use a call according to the following scheme:

localize <key> ?substitutes?

substitutes stands for an optional list of name-value pairs with which the placeholders in the message to be localized will be replaced.

A placeholder is a word enclosed in percent characters. Optionally, conversion functions may precede the word. Conversion functions must be enclosed in curly braces. They are defined in the ::localizer::converter namespace.

If no localization for a key exists, the key itself is returned as the localized string. It is therefore recommended to use the localization for the standard language as the key.

Examples

localize word
# => "Wort"

localize "This is version %version%." [list "%version%" "1.8"]
# => "Das ist Version 1.8."

localize "Permitted types: %types%" [list "%{and}{displayTitles objClass}types%" \
    [list "weather" "news"]]
# => Erlaubte Vorlagen: Wetter und Nachrichten
Conversion Functions

Conversion functions convert the substitution values passed to them for localization into a suitable text representation. The functions may accept parameters. In the call to the localiziation functions, the parameters are separated with a space character from each other and from the function name (see the parameter objClass for the displayTitles function in the example above).

For your convenience, the following conversion functions are supplied:

  • and: converts the provided list into text by adding the localized word and as element separators.
  • or: like and, however using or.
  • join: converts the values into comma-separated text.
  • displayTitles class: converts the values passed into their descriptions. class, for example, becomes attribute.
  • displayTitle class: like displayTitles, however only one value is converted.

Next to the placeholder itself you can access the original values in the localization using #names, provided that the conversion function supports this.

localizer add de "Permitted types: %types%" "Erlaubte Vorlagen: %types%#names"
localize "Permitted types: %types%" [list "%{and}{displayTitles objClass}types%" \
    [list "weather" "news"]]
# => Erlaubte Vorlagen: weather und news