Support » Plugins » Hacks » Internationalization for local variables in foreach loop

  • Resolved mirioki02


    Hi all,

    Currently I’m internationalization plugins and themes to ensure all text displayed is translated according to the correct language.

    I’ve come across a function that prints out descriptions in a foreach loop. From the codex and online material, for dynamic text or variables, I’m supposed to wrap the __() in a printf() or sprintf().

    However, if applied to a foreach loop, the msgid string in .po file will be the same for all description which is “This is my text %s”.

    How do I go about achieving internationalization in a foreach loop?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi


    Forum Moderator

    Where are you pulling the descriptions from?

    It’s from the checkout page of the Woocommerce plugin. The foreach loop is used to list all the available payment gateways and the description for each. The function echo the description but I’m using ob_start() to capture it to a local variable.

    The concern now is how to make sure at each iteration, the text to refer in the .mo file is unique.



    Forum Moderator

    Can you place all of the descriptions into an array and then loop through that as part of your foreach loop? That way you could assign a localized version of the text as a string in your printf().

    I think you just need to localize “This is my text %s” and each value separately. If values in loop are “AAA”, “BBB” and “CCC” you must localize:
    – This is my text %s
    – AAA
    – BBB
    – CCC

    In spanish you can translate:
    – Este es mi texto %s
    – AAA-es
    – BBB-es
    – CCC-es

    Output will be “Este es mi texto AAA-es”, “Este es mi texto BBB-es”, etc…

    I don’t know if this is a solution for you. If it’s not, could you be more specific?

    As I’m already storing domain a local variable from the output buffer, it will the same as doing it from an array. Since the text is not dynamic, as in there’s no change in value, do I need to put it printf()?

    I’ve tried _e($local, ‘domain’) before this, but I’ll retry that again when I get back to my workplace. Thanks!

    To give a clearer picture about my problem, here’s the excerpt of my code:

    <?php foreach ($available_gateways as $gateway ) : ?>
    <input type="radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php if ($gateway->chosen) echo 'checked="checked"'; ?> />
    <label for="payment_method_<?php echo $gateway->id; ?>"><?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?></label>
    		if ( $gateway->has_fields() || $gateway->get_description() ) :
    			echo '<div class="payment_box payment_method_'.$gateway->id.'" style="display:none;">';
    			//capture echo output from $gateway->payment_fields() to local variable
    			//pass variable to internationalization
    			//My description includes {"Paypal", "Credit Card", "Bank Transfer", "Cash on Delivery"}
    			//This printf returns the same translation for all
    			printf(__('Pay via %s', 'domain'), $field_desc);
    			//This doesn't return translated string
    			_e($field_desc, 'domain');
    			echo '</div>';
    <?php endforeach; ?>

    Is there a way for the _e($field_desc, ‘domain’) function to interpret the string in $field_desc variable and send the string value “Cash on Delivery” for translation instead of the text “$field_desc”? Something like

    _e(echo $field_desc, 'domain');

    I tried this but obviously it’s erroneous.

    Hi all,

    Turned out the issue wasn’t due to the foreach loop at all.

    The text failed to translate because my text contains apostrophe which was changed to its HTML entity. That’s why the translated text was not found.

    I’m going to mark this as resolved but I would like to know how I can escape/encode the apostrophe. I tried esc_attr_e() but the apostrophe is still changed to '.

    Thank you esmi and iuttu for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Internationalization for local variables in foreach loop’ is closed to new replies.