This section describes the built-in function: ng:translation(resource, key, locale,fallback_value)
The function can be used, if you e.g. want to create a multi-language template. The function refers to one or more translate files. Another way to use translate files is in the translation element in the designer. Details of translations are covered in the description of the translation element. The rules for identifying the value to return is explained in the section, Which value is returned.
The advantages with this built-in function compared to the translate element in the designer are:
1.You can use the ng:translate built-in function in the workflow components, where the translation element cannot be used.
2.You can use the ng:translate function in xpath expressions and even assign the result of the ng:translate function to variables and workflow variables.
The function has these parameters:
Resource
This is the path to the main translate file from the translation directory - without the extension. If you e.g. have these translate files:
Then you need to refer to the translate.properties files as: /Demo/translate as the resource - as the translation files are placed in the Demo directory.
Key
This is the key for the lookup in the translation files.
Locale
The language/locale, that you want to use.
If this parameter is null or an empty string and the next parameter, Fallback_value is not defined, then the locale setting from the template properties is used. That can be helpful if you are using this function a lot in your template as you can then simply change the local once and it will have effect on all ng:translation functions used in the template. This simulates the functionality of the translation element and the translation option on the text element. The alternative is to use a common Xpath function for all these functions - you can e.g. define a variable with the locale and refer to this variable.
Fallback_value
This parameter is optional
If the parameter is used, then the function will return the fallback value, if either the translation file has not been found, or if the specified key was not found while no *OTHERWISE* key was specified in the root translation file.
Please notice, that the expression might not be executed in the result view in the designer. You need to preview to see the final result.
The ng:translate function will fail with an error, if the key is not found in the translation file. You can however insert the special key named *OTHERWISE* in the root/default translation file to specify what value to use, if not match is found.
Examples:
This function refers to the translation file, /Demo/translate_da_DK.properties and looks up with the key, AMOUNT.
ng:translation('/Demo/translate','AMOUNT','da_DK')
This function refers to the translation file, /Demo/translate.properties and looks up with the key, AMOUNT.
ng:translation('/Demo/translate','AMOUNT','')
This function returns null as the translation file (in this situation) is not found (the fourth parameter specifies what value to return):
ng:translation('/Demo/NotFound','AMOUNT','','')
Example for the *OTHERWISE* option
You can use a translation file as a user friendly way to configure the way that InterFormNG2 works e.g. to configure the preferred output per customer or per customer groups. For setups like that you might also need to be able to handle the situation, where no matching key is found.
If you e.g. have setup the root/default translate file like so:
Then you will get the value back, **Translation key not found**, if there was no matching key.
So you could setup a select..when like below to branch out depending on if the key was found or not:
Alternatively you can build it all into a single text element with a condition like so:
if (ng:translation('/Demo/translate',$key,'')='**Translation key not found**') then 'No matching key could be found' else ng:translation('/Demo/translate',$key,'')
Or of course the other way around:
if (ng:translation('/Demo/translate',$key,'')!='**Translation key not found**') then ng:translation('/Demo/translate',$key,'') else 'No matching key could be found'
In the example above the text 'No matching key could be found' will be printed out, if the variable, key does not contain a key, that is found in the translation file. If there is a match then the value is printed out.