April 27, 201511 yr // the 'common' translatable ways: echo sprintf( __('I love %1$s and %3$s','translate_domain'), 'Apple', 'Banana', 'Coconut'); or echo vsprintf( __('I love %1$s and %3$s','translate_domain'), array('Apple','Banana','Coconut') ); // what I'd like to do $array = array( '%alpha%' => 'Apple', '%bravo%' => 'Banana', '%charlie%' => 'Coconut', // ... '%zulu%' => 'Zucchini', ); $translated_text = __('I love %alpha% and %bravo%','translate_domain'); // And then send to function to replace the tokens Hopefully the comments in the code are useful. The issue is the token replacement, I'm under the impression that text variables such as %1$s and %3$s are common (used by the sprinf function) in translatable text but I'd like to pass some more meaningful variables. Am I allowed to put meaningful tokens (%... ...%) into the text to be translated and the translaters out there be okay with it? Or to put it another way: How to translate when the sequenial order of tokens being passed changes ie Coconut isn't going to always be %3$s?
May 22, 201511 yr I would do something like this <?php $fruits= array('Apples', 'Bananas', 'Coconuts'); printf( __( 'I love %1$s, and %2$s.', 'my-text-domain' ), $fruits[0], $fruits[1] ); ?> If you need to change the position of the elements then do so in the array rather than in the output.
Create an account or sign in to comment