1

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.

kapa
  • 77,694
  • 21
  • 158
  • 175
mpdc
  • 3,550
  • 5
  • 25
  • 48

2 Answers2

1

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>
Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
  • Please see my edit. Placeholders are shown as they are, with `$show` and `$year` being concatenated to the end. – mpdc Feb 12 '14 at 10:45
  • @MatthewPeckham That's very strange :). Are you sure you do not have some parentheses in the wrong position? – kapa Feb 12 '14 at 10:50
  • As certain as I can be! What I've pasted is the exact code from the files. – mpdc Feb 12 '14 at 10:52
  • @MatthewPeckham Here is a [PHPFiddle](http://phpfiddle.org/main/code/exm-gcu). It works fine there. What do you use to get the string from the ini file? The strange thing is why it adds the variables to the end... `sprintf` would normally simply ignore them if it does not detect the placeholders... That is why I suspected some misbehaving parentheses. – kapa Feb 12 '14 at 10:56
  • My code to parse the ini file has been added to my initial question. – mpdc Feb 12 '14 at 11:01
  • @MatthewPeckham Then all you can do is strip down the code until you find the part that is causing the problem. If you have the minimal code that still causes it, you can share it on PHPfiddle or in your question, and I will happily help. It should work, so there is some problem in your code which I do not see in the parts you have posted. Btw, have you tried running the code I have posted in my fiddle? – kapa Feb 12 '14 at 11:08
  • The code was working with multiple keys/values in my ini file, it seems to be the `sprintf()` function that is for some reason not working. – mpdc Feb 12 '14 at 11:10
  • @MatthewPeckham I assure you the `sprintf` function works quite fine :). Please do what I suggested, otherwise we cannot really solve this problem. I do not sit at your computer, so I cannot debug the exact code that you see. The solution is what I suggested: strip down everything around the `sprintf` and you will see what is causing the problem, or at least you can share it with us and we will find the problem. – kapa Feb 12 '14 at 11:31
1

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); ?>

Bang Dao
  • 5,091
  • 1
  • 24
  • 33