Language and translation
Handling languages from api’s and translation helper functions
Handling languages from api’s and translation helper functions
Current and default languages are stored inside globally available .Session
object. There is 2
possible language codes available at the moment, - 2-letter and 3-letter code. 2-letter code is
an iso code, but 3-letter is an internal erply api language code, please
check erply api for more information.
{{ .Session.Language.Code }}
- 2-letter iso code{{ .Session.Language.LegacyCode }}
- 3-letter erply legacy code{{ .Session.DefaultLanguage.Code }}
- 2-letter iso code of default language{{ .Session.DefaultLanguage.LegacyCode }}
- 3-letter erply legacy code of default languageDynamic sample:
<input type="hidden" name="PIMApi.Api.Get.products" value="v1/product" data-preset-val="v1/product">
<ul>
{{ range $row := .Data.PIMApi.Api.Requests.products.Response.Array }}
<li>{{ $row.Get "id" }} | {{ $row.Get (printf "name.%s" $.Session.Language.Code) }}</li>
{{ end }}
</ul>
Models sample:
{{ range $row := .Data.PIMApi.ProductList }}
<!-- Print name value in the selected/default language -->
<!-- Will print empty string if value under language code not found -->
<p>Name in current lang: {{ index $row.Name $.Session.Language.Code }}</p>
<p>Name in default lang: {{ index $row.Name $.Session.DefaultLanguage.Code }}</p>
{{ end }}
Dynamic sample:
{{ range $row := .Data.PIMApi.Api.Requests.products.Response.Array }}
<tr>
<td>{{ $row.Get "id" }}</td>
<td>{{ $row.Get (printf "name.%s" $.Session.Language.Code) }}</td>
</tr>
{{ end }}
Models sample:
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{{ range $row := .Data.PIMApi.ProductList }}
<tr>
<td>{{ $row.ID }}</td>
<td>{{ index $row.Name $.Session.Language.Code }}</td>
</tr>
{{ end }}
</tbody>
</table>
Dynamic sample:
<select>
{{ range $row := .Data.PIMApi.Api.Requests.products.Response.Array }}
<!-- Print name value in the selected/default language -->
<option value='{{ $row.Get "id" }}'>{{ $row.Get (printf "name.%s" $.Session.Language.Code) }}</option>
{{ end }}
</select>
Models sample:
<select>
{{ range $row := .Data.PIMApi.ProductList }}
<!-- Print name value in the selected/default language -->
<option value="{{$row.ID}}">{{ index $row.Name $.Session.Language.Code }}</option>
{{ end }}
</select>
Dynamic sample:
<form method="post">
<input type="hidden" name="PIMApi.Api.Post.saveProd" value="v1/product">
{{ range .Data.ErplyApi.LanguageList }}
<div class="form-field">
<label for="formInputName{{ .IsoCode }}">Name ({{ .IsoCode }}):</label>
<input type="text" id="formInputName{{ .IsoCode }}"
name='PIMApi.Api.Json.saveProd.string.name.{{ .IsoCode }}'
value='{{ $.Data.PIMApi.Api.Requests.saveProd.Json.Get (printf "name.%s" .IsoCode) }}'>
</div>
{{ end }}
<button type="submit">Send</button>
</form>
Models sample:
{{ range.Data.ErplyApi.LanguageList }}
<div class="form-field">
<label for="formInputName{{ .IsoCode }}">Name ({{ .IsoCode }}):</label>
<input type="text" id="formInputName{{ .IsoCode }}"
name="AccountAdminApi.WarehouseInput.Name.{{ .IsoCode }}"
value="{{ index $.Data.AccountAdminApi.WarehouseInput.Name .IsoCode }}">
</div>
{{ end }}
This feature can be used to define translations for the pages or applications that can be used to work automatically together with the language parameter from the url.
The translation files are in json format and the language keys are expected to be in the iso 2 character codes.
{
"en": {
"key": "value"
},
"es": {
"key": "value"
}
}
Translations can be added on 2 different levels
These are stored as separate files. New ones can be created from CREATE NEW -> Create new page and selecting the type translation.
These translations are only used by applications and the contents are shared between all the pages of the application once connected to one.
An application can also have multiple translation files. They are loaded in alphabetical order.
If 2 files contain the same key for the same language then the last loaded item will determine the used value.
These are stored on the templates (pages) and can only be used on the same template.
Translations can be added using the earth icon in the editor.
Template based translations will override application translations if both define the same key for a language.
This function uses the current navigation language code from url /en/ and used this to locate the language needed.
/000000/en/break-temp-6-page
{
"en": {
"key": "value"
}
}
{{ .Translation.Get “key“ }}
This would be loaded as “value” when the page is opened.
This function takes in a second parameter for the language code we want to get the result for, this one ignores the url path language.
If the input language cannot be found then it default to the path parameter instead.
/000000/en/break-temp-6-page
{
"en": {
"key": "value"
}
}
{{ .Translation.GetIn “key“ “en“ }}
This would be loaded as “value” when the page is opened.
We can also use an external source for the translations, as long as it follows the same json structure. Values entered with this will overload the translations from both application and the template if they define the same key.
{{ .Translation.GetWithSource “key“ {"en": {"key": "value_1"}} }}
{{ .Translation.GetWithSourceIn “key“ “en“ {"en": {"key": "value"}} }}
Load order determines what value is returned if there are multiple sources for translations
If a key is not found then the key itself is printed.
You can override this behaviour (where the key is returned when translation is not found) by settings a special value in the translations file. Using this the methods ‘Get’ and ‘GetWithSource’ will attempt to return the translation in the set default language when the translation for the initial language code is not found.
{
"default": {
"to": "en"
}
}
There is also option to optimize huge translation files to one single file that will contain only translations that are actually used in the application. That feature would be useful for translation files larger than 1MB.
To generate translation file and enable/disable it, just go to the application configuration, select templates section and click on the “Translations optimization” button.
Then need to generate the file by clicking on the “Generate optimal translations” button. This
action will generate a file in application with name {application UUID}-translation
Please
note: Optimization should be re-generated manually every time when at least one of the original
translation files were modified. Check the Enable optimized translations
option and confirm
the action to basically enable the feature.
Translation file generated in scope of main application and does not include translations from modules. If you have translations in modules, then they should contain only module specific translations, leaving huge translation files in main application. Translations from main application can be used inside modules.
When feature is enabled, GoErp will always use generated file instead of all application translation files.