i18n message in specific language

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

i18n message in specific language

Postby gk1441607435 » Tue Nov 07, 2017 12:14 pm

I need to get an i18n message in an other language than the language of the logged-in user.

I was looking for a function like this

Code: Select all
i18n.getI18NMessage(i18nKey, langCode);
var textInFrech = i18n.getI18NMessage('my.18n.text', 'fr');

but there was nothing like this.

Of course I could set the user language and switch back after I got my message

Code: Select all
i18n.setLocale('fr', 'FR');
var textInFrech = i18n.getI18NMessage('my.18n.text', 'fr');
i18n.setLocale('en', 'US');

but I don't want to do this. If something went wrong the user is lost in a french programm :shock:

Has anyone an other idea, how to get the message for a specific language?

Thanks Gregory
Posts: 14
Joined: Mon Sep 07, 2015 8:30 am

Re: i18n message in specific language

Postby mboegem » Tue Nov 07, 2017 4:35 pm

Hi Gregory,

it's exactly the way you describe, but I could understand your concern.

Since all the messages are in a table (once deployed) you could just query the value.
Something like this:
Code: Select all
SELECT COALESCE(l.message_value, d.message_value)
FROM my_message_table d
LEFT JOIN my_message_table l ON d.message_key = l.message_key AND l.message_language = ?
WHERE d.message_key = ? AND d.message_language IS NULL

Just create a global/scope function that takes the parameters you want (key + language) and let it return the value.

In developer however, this is more an issue, because the i18n entries are stored in a workspace file.
You could get around this by syncing the workspace file to the table (right click the i18n files node in the solution explorer and choose 'write to db'

Hope this helps
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

User avatar
Posts: 1566
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: i18n message in specific language

Postby patrick » Tue Nov 07, 2017 6:08 pm

Having a language parameter to the getMessage() method would mean that all language values would have to be downloaded (at least in the smart client). So querying the table directly is the best solution indeed.
Patrick Ruhsert
Servoy DACH
Posts: 3692
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: i18n message in specific language

Postby gk1441607435 » Wed Nov 08, 2017 9:11 am

Hi all

Thank you Patrick for explaining why there is no function to retrieve a specific language. I aggree.

I think in future I will use the example of Marc to get my language text. At the moment we have that problem only in a few parts of our software. It could be done hardcoded. But if it happens more often, we should write a reusable function to get specific languages direcly from the table.

Thank you Marc
Thank you Patrick

You helped me a lot

Posts: 14
Joined: Mon Sep 07, 2015 8:30 am

Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 4 guests