Calling username variable in the middle of custom strings.

Started by Antechinus, September 24, 2019, 01:32:58 AM

Previous topic - Next topic

Antechinus

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?

Arantor

You use sprintf to fix it, e.g. sprintf($txt['pm_online'], $message['member']['name']) inside the template.


Antechinus

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.

Aleksi "Lex" Kilpinen

Definitely, for i18n purposes - stuff should be done like this a lot more :)
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Antechinus

I'm gonna do any custom strings this way. It just makes more sense.

Advertisement: