0

I would like to translate all items from button style dropdown list on the settings page in my plugin.

Normally, when I want to translate something, I use this pattern:

__( 'Theme style', 'qty-increment-buttons-for-woocommerce' )

However, this is not a plain text, but a setting that I use for example in defaults array:

$defaults = array ( 'qib_auto_table' => false, 'qib_merge_buttons' => true, 'qib_button_height' => 35,
                            'qib_button_width' => 35, 'qib_button_style' => 'Silver', 'qib_quantity_field_width' => 52 );

Then in my plugin I do checks like this:

switch ( $args['button_style'] ) {
            case 'Theme style':

Can I translate these options and refer to them using my normal translation pattern (in $items array as well)? Or it can break if, for instance, someone saves settings in one language and then changes the language of the website?

public function qib_button_style_callback() {

    $items = array ('Theme style', 'Silver', 'Black', 'Orange');        

    echo '<select id="qib_button_style" name="qib_settingz[qib_button_style]">';        
    foreach($items as &$item) {         

        $selected = (isset( $this->options['qib_button_style'])) && ( $item == $this->options['qib_button_style']) ? 'selected="selected"' : '';            
        $item = __( $item, 'qty-increment-buttons-for-woocommerce' );
        printf(
            '<option value="%1$s" %2$s>%1$s</option>',
            esc_attr($item),
            $selected
        );
    }       
    printf(
        '</select>
        <p class="description">%1$s</p>',
        __( 'To use button colors from your theme, choose "Theme style". Otherwise plugin will use its own style to color increment buttons. It includes background, font, hover background and focus outline. Also quanity input field\'s border is colored. If for example custom background color is needed, you can override rule with child theme CSS.', 'qty-increment-buttons-for-woocommerce' )
    );
}
1

My advice would be to not use the label as the value in your dropdown. The value is stored in the database and used programatically in your template, so it doesn't need to be localised. Then you can translate the label without any side effects.

public function qib_button_style_callback() {
    $items = array(
        'theme-style' => __( 'Theme style', 'qty-increment-buttons-for-woocommerce' ),
        'silver'      => __( 'Silver', 'qty-increment-buttons-for-woocommerce' ),
        'black'       => __( 'Black' 'qty-increment-buttons-for-woocommerce' ),
        'orange'      => __( 'Orange' 'qty-increment-buttons-for-woocommerce' ),
    );

    echo '<select id="qib_button_style" name="qib_settingz[qib_button_style]">';

    foreach( $items as $value => $option ) {         
        printf(
            '<option value="%1$s" %2$s>%3$s</option>',
            esc_attr( $value ),
            selected( $value, $this->options['qib_button_style'], false ),
            esc_html( $option )
        );
    }       
    printf(
        '</select>
        <p class="description">%1$s</p>',
        __( 'To use button colors from your theme, choose "Theme style". Otherwise plugin will use its own style to color increment buttons. It includes background, font, hover background and focus outline. Also quanity input field\'s border is colored. If for example custom background color is needed, you can override rule with child theme CSS.', 'qty-increment-buttons-for-woocommerce' )
    );
}

Then in your template you can check the value without worrying about translation:

switch ( $args['button_style'] ) {
    case 'theme-style':

And set the appropriate default:

'qib_button_style' => 'silver'

Also note that there is a WordPress function that I've used, selected(), that makes it easier to set the current value of a dropdown.

Another thing to remember is that you cannot pass variables to translation functions like __(). You must only pass a string. So this is incorrect:

__( $item, 'qty-increment-buttons-for-woocommerce' );

Mark Jaquith describes the reasons in his post Translating WordPress Plugins and Themes: Don’t Get Clever:

See, PHP isn’t the only thing that needs to parse out your translatable strings. GNU gettext also needs to parse out the strings in order to provide your blank translation file to your translators. GNU gettext is not a PHP parser. It can’t read variables or constants. It only reads strings. So your text domain strings needs to stay hardcoded as actual quoted strings. 'my-plugin-name'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.