Home Contents | # tr() | Tr | Core4 Lua Commands

## SYNOPSIS

1. text = tr(id)
2. text = tr(message[, disambiguation|nil[, context|nil[, numerus]]])

## DESCRIPTION

This function supports translations of user visible text for multiple languages. The first form is uses Numeric Mode (see there), while the second form uses Contextual Mode.

In contextual mode, a few optional parameters help resolving disambiguities that occur when the same phrase in the source language needs to translate differently depending on its usage context. (For example, None in english might translate into Kein or Nichts in german.) To disambiguate between several uses of one phrase, pass a disambiguation comment as the disambiguation parameter.

The optional parameter context helps grouping translation phrases. When not given, the system uses the filename of the Lua source file as the name of the context, with the path and the file extension removed. The context name shows up in left hand panel of the Qt Linguist translation tool.

Some languages are quite complex when it comes to distinctions between singular, plural (and more, for some languages.) In order to support this, a number must be passed as the numerus parameter. Inside the message string, put %n at the place where the number needs to be inserted. The translation subsystem will do all the necessary magic for you.

The function asks all translators installed into the table tr.translators for a translation until a match is found. Translators are installed into the table by calling tr:installTranslator().

## RETURN VALUE

If the lookup into the translation tables was successful, returns the translated text in UTF8 encoding, if not, returns the original string.

In both cases the replacement of %n by the numerus value is done.

## NOTES

The values for message, disambiguation and context must be literal strings and may not be variables.
To pass a value for numerus, you might need to pass nil for disambiguation and context.
The number in "Found 42 matches." is a numerus, while the number in "Please pay $12.34" is not.

## EXAMPLE

Numeric mode:

> >
tr[1] = { [1000] = "Please wait" } print(tr(1000))
Please wait

Contextual mode (note the singular form):

> > > > > >
tr.translators = nil tr:installTranslator("/some/where/hello_de.qm") print(tr("Hello World")) for nr = 0,2 do print(tr("%n file(s) found."), nil, nil, nr) end
Hallo Welt 0 Dateien gefunden. 1 Datei gefunden. 2 Dateien gefunden.

## SEE ALSO

tr.context, tr:installTranslator, tr.load, tr.loadTranslator, tr.translators