How can a script translate text?

Any issue about Cyclos 4 scripting and Webservices

Moderators: rmvanarkel, hugo, alexandre

Post Reply
admin_de2
Posts: 81
Joined: Wed Dec 31, 1969 9:00 pm

How can a script translate text?

Post by admin_de2 »

Dear Cyclosians,

is there a function to access the translation of a text, usable by a script?

I want to access the current locale and use existing or own
translations, e.g. to translate the currency name or currency
abbreviation)?

Like in Wordpress, where the "__"-function always translates the text given by the translation-key.

Thank you, Thomas (& Jakob)
luis
Posts: 201
Joined: Fri Feb 17, 2006 11:01 am

Re: How can a script translate text?

Post by luis »

Cyclos has 2 kinds of translations: application translation and data translation.
Application translation can translate "static" text used by the application, but is based on a set of predefined keys. It is not possible to define custom keys to translate.
Data translation can translate data created in Cyclos, such as currencies, groups, etc. You mentioned to translation of a currency name or symbol. You can use the https://documentation.cyclos.org/4.13/s ... ndler.html interface, accessible via dataTranslationHandler in scripts. You need to pass in the data you want to translate and the property. Example:

Code: Select all

import org.cyclos.entities.system.Language

def currency = entityManagerHandler.find(Currency, 'unit')
def translations = dataTranslationHandler.getValues(currency, true)
println(translations.name)
println(translations.symbol)

// Translate in another language
def portuguese = entityManagerHandler.find(Language, 'pt_BR')
translations = dataTranslationHandler.getValues(portuguese, currency, true)
println(translations.name)
println(translations.symbol)
Luis Fernando Planella Gonzalez
Cyclos development team
admin_de2
Posts: 81
Joined: Wed Dec 31, 1969 9:00 pm

Re: How can a script translate text?

Post by admin_de2 »

Hello Luis,

thank you very much, this is very useful for us! Super.

In our case we are also in the situation to use already existing application- and/or data-translation-keys.
E.g., the data-translation-key for the current currency-name, as you mention in your code-snippet. Or an existing application-translation-key.

Do you have a little hint for handling of application-translation-keys, please?

Thank you, Thomas
luis
Posts: 201
Joined: Fri Feb 17, 2006 11:01 am

Re: How can a script translate text?

Post by luis »

You will need the https://documentation.cyclos.org/4.13/s ... ndler.html available under translationHandler in scripts.
All message keys are organized per 'submodule', and are static classes generated into a container class per module:
Example:

Code: Select all

import org.cyclos.model.users.UsersKeys

println translationHandler.message(UsersKeys.Users.EMAIL)
Luis Fernando Planella Gonzalez
Cyclos development team
admin_de2
Posts: 81
Joined: Wed Dec 31, 1969 9:00 pm

Re: How can a script translate text?

Post by admin_de2 »

like a charme... :D
jakob.schumann
Posts: 20
Joined: Thu Apr 23, 2020 5:37 pm

Re: How can a script translate text?

Post by jakob.schumann »

Thanks, both variants are working!

Just to be sure: So there is no way to translate e.g. a custom error message for a custom field validator via the translation files?
Aren't there any multi-language cyclos installations that use scripts that generate messages? The only way would be to include all translated messages in the script and manually detect the current locale and return the corresponding message?
Post Reply