translate prompt (and alert) message triggered in client script

undefined1
Tera Contributor

hey, I'm new to scripting and wanted to ask about this as I haven't found any advice. and while writing my question I found the solution so maybe this will help somebody in future.

My original post:

I have a prompt in client script in the widget. this should be asked in respective language as my users are speaking German and English.

I tried different setups with getMessage function and prompt with variable instead of the text but nothing works.

I ended up with this temp solution which is ugly but working. would like to change it to something proper...

var lang = g_lang;
var reason;
if(lang == 'de'){
reason = prompt('German question');
} else {
reason = prompt('English question');
}

Now the solution:

add English and German message in sys_ui_message with key of your choice eg 'My Msg'

in client script in widget after c is defined like:

var c = this;

define your message like:

c.customMsg = "${My Msg}";

then you can trigger prompt or alert like:

var answer = prompt(c.customMsg);
if (answer) {
$scope.data.your_variable = answer;
etc...

or simply :

alert(c.customMsg);

 

not sure if this is the right way but it worked for me. hopefully this will help somebody in future. 

if you have some better solution or if this is not good a practice please comment.

 

 

 

 

5 REPLIES 5

Weird
Mega Sage

If you have a user with specific language, then getMessage should work.

var message = getMessage('messagetext');
var answer = prompt(message);

 

undefined1
Tera Contributor

@Weird, thank you for your answer, I've tried that and maybe it should work, but it doesn't work for me. in console I get:

undefined1_0-1685351341281.png

 

Hmm, that's weird. And you're working on a widget client script?
Can you try using c.getMessage and see if that works? I usually do translation on server side, so if client side doesn't work you could just use data.prompt = gs.getMessage("messagekey"); since that should be available on the client side then as "c.data.prompt".

undefined1
Tera Contributor

I added this to server script:

 

data.prompt = gs.getMessage("my Message");

 

and in client script:

 

var reason = prompt(c.data.prompt);
if (reason) {...etc...

 

and this works correctly. the question now is if there are cons or pros to these two solutions.

1. translate on server side and bring message to client side?

or

2. if it is okay to use $ identifier to pull message on client side?