Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Please login or create an account
Change language to: English - Français - 日本語 - Русский
Ajuda do Scilab >> Localização > gettext


indexes or/and translates some indexed english messages


Alias of gettext().


gettext("The literal reference message")
gettext(["item #1" ; "item#2"])
translated = gettext("The literal reference message")
translated = gettext(["item #1" ; "item#2"])
translated = gettext(msgid)
.. gettext(domain, ..)


"The literal reference message"

Single case-sensitive literal text of an english message to be indexed or/and translated. A column of literal texts explicitly separated with semi-colons may also be specified. Messages can include some C-like placeholders starting with "%".

Only standard ASCII characters can be used. Any other extended ASCII or UTF-8 characters would make gettext() failing.


single or array of case-sensitive messages identifiers (in english) to be translated, in a variable.

Only standard ASCII characters can be used.


Input messages translated in the current language of the Scilab session. If no translated version is available for an input message, the input version in english is returned. The input and output arrays have the same sizes.

These messages are labelled msgstr and defined in the ./locales/*.po files. They can include extended ASCII or UTF-8 characters.


word of text: the name of a domain. When localizing an external module, domain may be usually set to the technical name of the module.

domain can indifferently be a literal or a variable. It is case-sensitive.

domain is required by tbx_generate_pofile() to make the literal msgid string indexed (harvesting stage. See below).

When gettext(domain, msgid) is used to retrieve the translated version, domain is used to get the path to the directory where translations are stored, as beforehand registered with addlocalizationdomain(domain, path).


Harvesting messages and overall processing

gettext is a free and open external application shipped within Scilab to support multilinguism for messages. This support consists in 4 main stages:

  1. Harvesting messages among the code and indexing them to be translated. For an external module, this is explicitly performed with the xgettext external binary, that is part of the gettext external application, and that is called by the tbx_generate_pofile Scilab function. In Scilab, it is also possible to use tbx_build_localization(..) or tbx_make . localization, that both call tbx_generate_pofile(..).

    Each collected message is considered as an identifier (a message id, = msgid) for all its forthcoming translations. In Scilab, the reference language for identifiers is english. Therefore, the msgids to be indexed must be written in english.

    Only single literal and in one piece messages being as gettext input argument are collected. Hence, the following syntaxes and cases won't index the considered message, and no further translated versions will be available:

    #SyntaxStatus Harvesting results
    1 gettext("To be translated") OK standard syntax
    msg = "To be translated";
    NOK The text is in a variable. It is not a literal. It won't be indexed. Nevertheless, this syntax will work to retrieve the translated version, provided that the message has been collected elsewhere in a right way.
    3 gettext("To be" + " translated") NOK "To be" and " translated" are indexed as 2 separate msgid. Then, since the concatenated msgid "To be translated" does not exist, retrieving its translation will fail.
    gettext("To be" + ..
            " translated")
    NOK Same as #3
    5 gettext(["item#1", "item#2"]) NOK Only "item#1" is collected. "item#2" is ignored.
    6 gettext(["item#1" "item#2"]) NOK "item#1item#2" is indexed. Then, since "item#1" and "item#2" are unknown separate msgid, retrieving their respective translation will fail.
    NOK Same as #6
    8 gettext(["item#1" ; "item#2"]) OK "item#1" and "item#2" are indexed as separate msgids.
    gettext(["item#1" ;
    OK Same as #8. Please take care of the semi-colon. Without it, the case #7 fails.

    For an external module, tbx_generate_pofile() harvests only gettext occurrences that specify a domain. It ignores any call to gettext with only one input argument.

    Harvesting does not need to beforehand declare the domain with addlocalizationdomain()

  2. All indexed texts are then translated by the author of the code or by some contributors. Anyway, by some humans. Good translation tools are also available online.

  3. All translated versions are bundled in a way that gettext(..) becomes efficient to retrieve the version in the current session's language. This is done by running tbx_generate_pofile() another time.

  4. Finally, some calls like gettext(message) or gettext("The message content") are used to retrieve and return the translated version.

    When the message is not a native Scilab one (for instance it is specific to an external module), the domain where the message and its translations can be found must be specified, as in gettext(domain, message) or gettext(domain, "The message content"). In this case, addlocalizationdomain(domain, path) must be run before, in order to indicate to Scilab the directory where the translated messages of the given domain are stored.

Most often, a message is declared to be harvested and is used to retrieve its translated version through the same gettext("The literal message") call. However, it is not mandatory. Hence, a piece of code like if %F, gettext("The literal message"), end will never execute the gettext call, but is nevertherless meaningfull: It is here only to make the message visible to the xgettext scanner/harvester. Then, somewhere else in the code, it will be possible to use msg = "The literal message"; gettext(msg) to return the translated version.

As well, using several times the same literal gettext("The literal message") call won't be rare, for example to retrieve the translation in several files. In this case, the xgettext harvester will collect the message only once, and the same translation will be returned for all calls.

Limitations: There is no way to translate a message
  • in a language other than the current session's one. If this is really required, the session's language will have to be temporarily switched to the desired language, then gettext() called, and at last the initial session's language restored.

  • from a translated version rather than from the message id in english.

msgid messages

To be correctly processed, english messages must comply with a few rules:

  • Literal harvestable messages may be indifferently delimited with single or double quotes.

    When gettext() or _() is used in a XML or XSL file such as in the preferences files of an external module, the literal domain string and the literal message id must no longer be delimited.

    For instance, _(my_domain, My message) will be used instead of _("my_domain", "My message").

    In addition, spaces before and after the messageID are then never taken into account and never belong to the message. For example, the actual message ID corresponding to _(my_domain, My message: ) is "My message:", not " My message: ".

  • Double quotes must be avoided in the body of messages. Please use single quotes.

  • Inner escaped sequences like "\t" "\n" etc are not interpreted, neither by the xgettext scanner nor by the gettext function. They are collected and stored as is in the messages.

  • msgid messages often include some "%" C-like placeholders aiming to be replaced with some custom input data through msprintf() at run time. For instance, msprintf("Hi %s, could you come at %s today?", "John", "12:30") will return "Hi John, could you come at 12:30 today?".

msgstr translated messages

This section will be more useful for translaters than for actual users.

When an english msgid message includes some C-like placeholders, this always means that in the code the translated message is processed with msprintf() to replace placeholders with actual values. Hence, the order and type of placeholders indicate the order and types of actual input values when calling msprintf.

This is of first importance for translaters, when writing the corresponding translated message:

  • If placeholders are not numbered: they must be in the same order and types in the translated msgstr message than in the msgid message. Example (english to french):
    msgid  "%s is %d year old.\n"
    msgstr "%s a %d ans.\n"
    // first %s (a string), second %d (a decimal number), in both strings.
    Then, from a Scilab session set to french, we will get
    gettext("%s is %d year old.\n") // => "%s a %d ans.\n"
    // and                                     first v      v second
    t = msprintf(gettext("%s is %d year old.\n"), "Peter", 18)
    // => "Peter a 18 ans."
  • Otherwise, All placeholders must be numbered in the translated message. Each numbered placeholder can be used only once in the format (Scilab limitation). Example (english to japanese):

    msgid "%s: Wrong number of input argument: %d to %d expected.\n"
    msgstr "%1$s: 入力引数で不正な数: %3$d への %2$d を想定します。\n"
    msprintf(gettext("%s: Wrong number of input argument: %d to %d expected.\n"), "myFun", 2, 5)
    will return in a Scilab session set to english
    "myFun: Wrong number of input argument: 2 to 5 expected."
    while, in a Scilab session set to japanese
    "myFun: 入力引数で不正な数: 5 への 2 を想定します。"



// Messages ids are case-sensitive:
// "monday" is not an indexed native msgid, while "Monday" is:
gettext(["monday" ; "Monday"])

//_() is an alias of gettext():

// In Scilab, messages ids are in english:
_("Dimanche")       // The french version can't be used as msgid
--> gettext(["monday" ; "Monday"])
 ans  =
!monday  !
!Lundi   !

--> _("Sunday")
 ans  =

--> setlanguage("ru");
--> _("Dimanche")
 ans  =

--> _("Sunday")
 ans  =

Using a domain: Here, "tbx" is a predefined domain used to test translation features:

msg = "%s: something not in Scilab.\n";  // unknown in the main native Scilab domain:
gettext(msg)          // So, it is returned as is.

//  If we use the domain without having declared it before,
//  Scilab does not know where to find the translation:
gettext("tbx", msg)   // The input message is still returned as is.

// So, let's declare the domain:
addlocalizationdomain("tbx", "SCI/modules/localization/tests/unit_tests/locale");
gettext("tbx", msg)   // Now it works

// The answer is a joke: Here it is still in english (while french is expected).
// The point is that it has been set as the french translation of the input msgid.

// The same msgid can be used as a native Scilab one with its native translations,
// and in one or several domains, with other translations:
msg = "%s: No more memory.\n";
[_(msg) ; _("tbx", msg)]
--> msg = "%s: something not in Scilab.\n"; // unknown in the main native Scilab domain:
--> gettext(msg)
 ans  =
 %s: something not in Scilab.\n

--> //  Scilab does not know yet where to find the translation:
--> gettext("tbx", msg)
 ans  =
 %s: something not in Scilab.\n

--> // We declare the domain:
--> addlocalizationdomain("tbx", "SCI/modules/localization/tests/unit_tests/locale");
--> gettext("tbx", msg)   // Now it works
 ans  =
 %s : it is true, that is not in Scilab.\n

--> msg = "%s: No more memory.\n";
--> [_(msg) ; _("tbx", msg)]
 ans  =
!%s : Plus de mémoire disponible.\n    !
!%s : Overwrite Scilab translation.\n  !

Messages with numbered C-like placeholders:

in = getlanguage();
msg = "%s: Unknown value %s for %s option"; // has already some translations
t = gettext(msg)
msprintf(t, "setdiff", "''f''", "''direction''")

t = gettext(msg)
msprintf(t, "setdiff", "''f''", "''direction''")

--> setlanguage("en");
--> t = gettext(msg)
 t  =
  "%s: Unknown value %s for %s option"

--> msprintf(t, "setdiff", "''f''", "''direction''")
 ans  =
  "setdiff: Unknown value 'f' for 'direction' option"

--> setlanguage("ja");
--> t = gettext(msg)
 t  =
  "%1$s: %3$s オプション の未知の値 %2$s"

--> msprintf(t, "setdiff", "''f''", "''direction''")
 ans  =
  "setdiff: 'direction' オプション の未知の値 'f'"

See also


5.5.0 Domain management added.
6.1.0 C-like placeholders in messages can now be numbered.
Scilab Enterprises
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jan 03 14:35:43 CET 2022