Child pages
  • Customization via I18N

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To use i18n in your code, you can use the function:

Code Block
i18n.getMessage("i18n-key")

The i18n key has to be provided as a string.
For example:

Code Block
var message = i18n.getMessage("servoy.general.clickOk");

This will return the value of your language, if your language has no entry it will return the default value/reference text : "Click OK to continue".
It is also possible to use dynamic values. You can provide an array with the values you want to replace. To use dynamic values the i18n value should contain tags like {0}, {1}, ... , {n}. The tags will be replaced by the values you provide in the same order.

Code Block

{color:#7f0055}{*}var{*}{color} company_name {color:#ff0000}={color} {color:#2a00ff}"Servoy";

...

{color}
{color:#7f0055}{*}var{*}{color} amount {color:#ff0000}={color} {color:#800000}15{color}
{color:#7f0055}{*}var{*}{color} type {color:#ff0000}={color} {color:#2a00ff}"developers";{color}
{color:#7f0055}{*}var{*}{color} message {color:#ff0000}={color} i18n.getMessage({color:#2a00ff}"servoy.license.registered"

...

{color},{color:#0000ff}*\[*

...

{color}company_name,amount, type{color:#0000ff}*\]*{color});

For example if the key servoy.license.registered has the value 'Registerd to {0} with {1} {2}' the outcome will be 'Registered to Servoy with 15 developers'
You might be wondering how to know when using i18n in a dialog with button the user has clicked because depending on the language of the user the Yes button can be Si, Ja , Oui, Hai, ect.
To solve this you can get the translation in a variable and use that to check wich button is clicked.
For example:

Code Block

{color:#7f0055}{*}function{*}{color} question()

...

 \{
{color:#7f0055}{*}var{*}{color} yes {color:#ff0000}={color} i18n.getI18NMessage({color:#2a00ff}"servoy.lbl.yes"{color});

...


{color:#7f0055}{*}var{*}{color} no {color:#ff0000}={color} i18n.getI18NMessage({color:#2a00ff}"servoy.lbl.no"{color});

...


{color:#7f0055}{*}var{*}{color} cancel {color:#ff0000}={color} i18n.getI18NMessage({color:#2a00ff}"servoy.lbl.cancel"{color});

...


{color:#7f0055}{*}var{*}{color} answer {color:#ff0000}={color} {color:#ff0000}plugins{color}.dialogs.showQuestionDialog({color:#2a00ff}"i18n:servoy.lbl.title"{color}, {color:#2a00ff}"i18n:servoy.lbl.message"{color}, yes, no, cancel);

...


{color:#7f0055}{*}if{*}{color}(answer {color:#0000ff}*==*{color} yes) \{
{color:#00c800}application{color}.output({color:#2a00ff}"yes{color} {color:#2a00ff}is{color} {color:#2a00ff}pressed"{color});
{color:#3f7f5f}//execute your code.{color}
\}
\\
\}

Note that in dialogs you don't need to use the function i18n. getI18Nmessage() but you can just use "i18n:key".

...

If you want, you can overwrite a i18n message with the function i18n.seti18nMessage(). For example if you use i18n keys for you date formats you can let the user enter in your system how he prefers his date separator. Then when the user starts the system you can use the i18n.seti18nMessage() to replace the default separator with the user defined separator. For example in the i18n message you have used a '/' as date separator and the user wants a '-' to separate the date. In the onOpen method you can now use the code:

Code Block

i18n.seti18nMessage({color:#2a00ff}'servoy.lbl.date'{color}, {color:#00c800}utils{color}.stringReplace(i18n.getI18NMessage({color:#2a00ff}'servoy.lbl.date'{color}),{color:#2a00ff}'/'{color},{color:#2a00ff}'-'{color}))


Seperators

Formatting defaults need to be specified in the English(US) format the dot (".") as decimal separator (and comma as 1000 separator). Decimal values withing code need to be specified in English format as well. This will automatically translated to the end users format when he runs the code.
For example in Dutch the separator for decimals is a ','. In the format on a field you will have to enter the English format for a number for example '#.00'. When a user with the locale 'Nederlands(Nederland)' will use the solution and enters '23' into the field. The field will display '23,00'