Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Custom Tokens in Translatable Text

Featured Replies

// 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?

  • 4 weeks later...

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.