Question

I'm testing a way of running our translations through .ini files for each language. Test site can be found here, forgive the URL: www.exodus-squad.com

However, in the right-hand box I want to display the following text:

With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In 2015, Engine Expo will once again host the Electric & Hybrid Pavilion!

But the phrases 2015 and Engine Expo are both variables in a PHP configuration file. Currently, the section in my .ini file looks like this:

[pavilion]
texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In "
textb  = ", "
textc  = " will once again host the Electric & Hybrid Pavilion!"

and my on-page code looks like this:

<p>
    <?=$i['pavilion']['texta'];?>
    <?=$year?>
    <?=$i['pavilion']['textb'];?>
    <?=$show?>
    <?=$i['pavilion']['textc'];?>
</p>

but breaking out and back in of the paragraph of text is a problem, however, especially when it comes to having other languages with commas in different places, or words rearranged, etc. Ideally, I would like to be able to do something like this:

texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In " . $year . ", " . $show . " will once again host the Electric &amp; Hybrid Pavilion!"

Or:

texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In {$year}, {$show} will once again host the Electric &amp; Hybrid Pavilion!"

But neither are valid syntaxes. Does anyone know if this is possible?


EDIT

After receiving a couple of answers, my .ini file now looks like so:

texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In %d, %s will once again host the Electric &amp; Hybrid Pavilion!"

and my code looks like so:

<?=sprintf($i['pavilion']['texta'], $year, $show); ?>

but this just prints the variables after?

With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In %d, %s will once again host the Electric & Hybrid Pavilion!2015Engine Expo

EDIT #2

This is my parse_ini_file() code:

$find_lang = $_SERVER['REQUEST_URI'];
if (strpos($find_lang, '/fr/') !== false) {
    $lang = 'fr';
}
else if (strpos($find_lang, '/de/') !== false) {
    $lang = 'de';
}
else if (strpos($find_lang, '/it/') !== false) {
    $lang = 'it';
}
else {
    $lang = 'en';
}

$i = parse_ini_file($lang . ".ini", true);

EDIT #3

I've even tried breaking the code up slightly, so a $texta variable is made first, then echoed on to the page through sprintf(), but the output remains exactly the same:

<?php
$texta = $i['pavilion']['texta'];
echo sprintf($texta, $year, $show);
?>

Works! Thank you all.

Was it helpful?

Solution

You can use sprintf, it lets you put placeholders into your text, which you can fill up later. And it can do a lot more, it is really useful... You can even use numbered placeholders, so their order can be changed - very handy for translations.

For example:

texta = "Blah Blah Blah %d Blah Blah %s"

And then in your code:

<p>
    <?=sprintf($i['pavilion']['texta'], $year, $show)?>
</p>

OTHER TIPS

You can use the string formater: http://www.php.net/sprintf

text="First part %s second part %s last part";

Then in your echo file:

<?php echo sprintf($i['pavilion']['text'], $year, $show); ?>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top