Page 1 of 1

Problems with translation in notif logs with arguments

Posted: 02 January 2018, 16:57
by Woodruff
Hello,

I have troubles with translation inside the log, when the message is passed through a notification (notif_msg parameter). Enclosed arguments are not translated, even if all litteral strings are enclosed with clienttranslate().
Here's the simplest example of a string with argument that won't translate:

Code: Select all

self::notifyPlayer($player_id, 'log', clienttranslate('${You} have ${ressource_count} ${icon}.'), array(
    'You' => self::getColoredText(clienttranslate('You'), $player_id), /* 'You' word won't translate */
    'ressource_count' => $ressource_count,  /* just a number */
    'icon' => $icon /* just a span with a background-image */
));
The method getColoredText is defined like this:

Code: Select all

function getColoredText($text, $player_id) {
    $color = self::getPlayerColorFromId($player_id);
    return "<span style='font-weight: bold; color:#".$color.";'>".$text."</span>";
}
Here, all the string will appear translated in play, except the You that will remain You. This You is referenced in the translation string so the community could propose a translation for it.
Here the part of the string is passed to a function to be colored but there are other situations where these won't translate as well (a function to convert a code to the corresponding text, another to convert a number to its spelling, etc.).

Do you have any idea what I do wrong?

Thanks!

Tcheby

Re: Problems with translation in notif logs with arguments

Posted: 02 January 2018, 21:33
by DrKarotte
Usually notifications with arguments to be translated need a "i18n" array handed over, in your example the line

'i18n' => array ('You'),

could do the trick?

Re: Problems with translation in notif logs with arguments

Posted: 03 January 2018, 01:30
by Victoria_La
It won't get translated even with i18n because its bunch of non-static html. Just send "You have ${ressource_count} ${icon}"
without color. Or if you want fancy you can inject html on the client side. That is what I do with all my logs leaving php side html free.

Not posting here because its extensive example, but I added this to the wiki
http://en.doc.boardgamearena.com/BGA_St ... in_the_log

Re: Problems with translation in notif logs with arguments

Posted: 03 January 2018, 22:20
by Woodruff
Thanks Victoria_La.
Nice tuto, that's will solve my issue.
One question: why do you use __("lang_mainsite", text) instead of _(text) for translatable strings in JS?

Re: Problems with translation in notif logs with arguments

Posted: 03 January 2018, 22:54
by Woodruff
How would you do to do the same in action bar?
I have to inject card names like in Seasons.

Re: Problems with translation in notif logs with arguments

Posted: 03 January 2018, 23:27
by Victoria_La
Tchebychev wrote: One question: why do you use __("lang_mainsite", text) instead of _(text) for translatable strings in JS?
That just mean it will take translation from mainsite since"you" is already translated there,
you can do _("You") - that mean it will be added to your game strings,
and have to translated again for 30 languages

Re: Problems with translation in notif logs with arguments

Posted: 03 January 2018, 23:32
by Victoria_La
Tchebychev wrote:How would you do to do the same in action bar?
I have to inject card names like in Seasons.
You can use same technique, but if you just want text you can use just use args and i18n
I.e.
In states.php

Code: Select all

	"descriptionmyturn" => clienttranslate('${you} must do something with ${card_name}'),
        'args' => 'arg_playerTurn',
In ggg.game.php

Code: Select all

function  arg_playerTurn() {
    return ['card_name'=>clienttranslate("My Card"), 'i18n'=>['card_name'] ];
}
I did not test it but I think it should work

Re: Problems with translation in notif logs with arguments

Posted: 04 January 2018, 10:58
by Woodruff
Right, I thought that i18n was for notifs only.
I will test that and report if that works.

Re: Problems with translation in notif logs with arguments

Posted: 06 January 2018, 00:07
by Victoria_La
Tchebychev wrote:Right, I thought that i18n was for notifs only.
I will test that and report if that works.
The description and its arguments are go though exact same function format_string_recursive, so it will use i18n and
if you override this function it will use your hacks (but it does add ${you} argument on its own case of state description)

Re: Problems with translation in notif logs with arguments

Posted: 08 January 2018, 01:50
by quietmint
Yes, this works automatically for the state description top white bar:
ss.png
ss.png (20.15 KiB) Viewed 3490 times
You can do same for action buttons by manually calling format_string_recursive() instead of dojo.substitute() when you add the button:

Code: Select all

var str = this.format_string_recursive(_('I block with my ${card_name}'), {
    card_name: name
});
this.addActionButton('button_block' + blocker, str, 'onBlock');
button.png
button.png (14.48 KiB) Viewed 3490 times