Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Antechinus on September 24, 2019, 01:32:58 AM

Title: Calling username variable in the middle of custom strings.
Post by: Antechinus on September 24, 2019, 01:32:58 AM
The story is I'm recoding a bit of Display.template.php, and had the very silly idea that while I'm doing it I might as well set the language strings up so they'll be good to go in any language.

The relevant part of the template code is this:

<img src="', $settings['images_url'], '/', ($message['member']['online']['is_online']) ? 'user_on' : 'user_off', '.png" alt="', ($message['member']['online']['is_online']) ? $message['member']['name']. ' '. $txt['pm_online'] : $message['member']['name'].' '. $txt['pm_offline'], '" />', $txt['pm_menu_send'], '


First stab at it for the language strings has this:

$txt['pm_online'] = 'is online.';
$txt['pm_offline'] = 'is offline.';


Which works in English. It just echoes "Antechinus is online." or offline, as the case may be. The same format (name string, followed by separate status string) is also fine in some other languages. OTOH it screws the pooch in Tagalog, according to one of our resident experts. It probably does rude things to canines in a range of other languages too.

So what it really needs to make it bulletproof is a way of calling the username variable as part of one string. Something like:

$txt['pm_online'] = '%1$d is online.';

for English and:

$txt['pm_online'] = 'Si %1$d ay aktibo ngayon.'

for Tagalog. And whatever else for any other language on the planet.

So, how do I get ThemeStrings.english.php to call $message['member']['name'] as %1$d so I can use it in language strings?
Title: Re: Calling username variable in the middle of custom strings.
Post by: Arantor on September 24, 2019, 02:38:25 AM
You use sprintf to fix it, e.g. sprintf($txt['pm_online'], $message['member']['name']) inside the template.
Title: Re: Calling username variable in the middle of custom strings.
Post by: Antechinus on September 24, 2019, 02:53:16 AM
Ok. Will give that a go.
Title: Re: Calling username variable in the middle of custom strings.
Post by: Antechinus on September 24, 2019, 04:26:15 AM
Gave it a go. Close, but no cigar. It echoes "0 is online." (IOW, substituting zero for the username).

What I'd forgotten is the %1$d is for integers. For strings it needs to be %1$s. So this works in the language file:


$txt['pm_online'] = '%1$s is online.';
$txt['pm_offline'] = '%1$s is offline.';


With this in the template:

alt="', ($message['member']['online']['is_online']) ?  sprintf($txt['pm_online'], $message['member']['name']) : sprintf($txt['pm_offline'], $message['member']['name']), '"

SMF should do more strings like this.
Title: Re: Calling username variable in the middle of custom strings.
Post by: Aleksi "Lex" Kilpinen on September 24, 2019, 04:45:07 AM
Definitely, for i18n purposes - stuff should be done like this a lot more :)
Title: Re: Calling username variable in the middle of custom strings.
Post by: Antechinus on September 24, 2019, 05:01:56 AM
I'm gonna do any custom strings this way. It just makes more sense.