translate()
When a template has been prepared to use the translation feature (see Translating templates), this feature is also accessible via a template script.
The translate()
function gets the translation for a message, provided that a translation entry can be found for it (with the same context, if given).
If the text to be translated does not already exist in the template, you can create a translation entry for it by using the Add Empty String button on the Translations pane.
Note that calling the translate()
function may be unnecessary if a script adds text to an element that has the data-translate
attribute; see Tagging text that is inserted by a script. In that case, any existing translation will be applied automatically.
translate(message, context)
The translate()
function gets the translation of a source text with an (optional) context. It returns the message as-is if no translation is found.
message
String containing the message (the source text).
context
The message's context (optional). A context may be specified when creating a translation entry. Specifying a context makes it possible to translate a certain text in different ways.
Example
The following line of code gets the translation of the string "Submit" that was registered with the context "button".
var str = translate( "Submit", "button" );