Translatable text in action buttons

Game development with Board Game Arena Studio
Post Reply
User avatar
Woodruff
Posts: 412
Joined: 08 March 2014, 00:53

Translatable text in action buttons

Post by Woodruff »

Hi,
What is the most straightforward way to provide buttons with translatable text. This text being brought by the PHP side:

Code: Select all

// In my_game.game.php
function argMyState() {
    ...
    
    // Define each available option:  1 option => 1 button on JS side
    $options = array(array('value' => 1, 'text' => clienttranslate("Yes")), array('value' => 0, 'text' => clienttranslate("No")));
    
    ...
    
    return array(
        'options' => $options,
        ... // Any other parameters useful for this state
    );
}

// In my_game.js
onUpdateActionButtons: function(stateName, args) {
    if(this.isCurrentPlayerActive()) {
        switch(stateName) {
        case 'myState':
            // Add a button for each available option
            for(var i=0; i<args.options.length; i++) {
                var option = args.options[i];
                this.addActionButton("choice_" + option.value, _(option.text), "action_clicForChoose")
            }
            break;
            ...
        default:
            break; 
        }
    }
},
I think 'i18n' can't work, since I do not bring these values as part of a log.
1. Should I pass these buttons in the log as parameters like ${button_opt_1}, ${button_opt_2}... then ovveride format_string_recursive to wrap these texts into buttons instead of doing that in onUpdateActionButtons?
2. Is there anything smarter and/or more concise?

Thanks for your help!

Tcheby
User avatar
Victoria_La
Posts: 620
Joined: 28 December 2015, 20:55

Re: Translatable text in action buttons

Post by Victoria_La »

In this example you explicitly translating it by using _(...) function so no need for i18n unless your have recursive strings.
For simple strings you should not do anything else. It pretty straight forward already.

Just FYI string like Yes and No is actually translated in main-site so you can handle it differently but maybe its easier not to make exceptions

I personally never pass client only prompts, buttons names, etc from the server, its all handled by client. The only strings
server knows about are original game strings such as card name, tooltips etc.

And btw 'clic' is 'click' in english

For translating tooltips use this function, which supports recursive structures

Code: Select all

        getTr : function(name) {
            if (typeof name == 'undefined') return null;
            if (typeof name.log != 'undefined') {
                name = this.format_string_recursive(name.log, name.args);
            } else {
                name = this.clienttranslate_string(name);
            }
            return name;
        },
Using your example it would be

Code: Select all

 this.addActionButton("choice_" + option.value, this.getTr(option.text), "action_clickToChoose")
However in this specific example its redundant since addActionButton will apply formatted for the label. I use it mostly for tooltips
that comes from server from material file.
If you do use recursive strings you need i18n for deeper levels.
Post Reply

Return to “Developers”