【发布时间】:2014-02-12 10:29:37
【问题描述】:
我正在测试一种通过 .ini 文件运行每种语言的翻译的方法。测试站点可以在这里找到,原谅网址:www.exodus-squad.com
但是,我想在右侧框中显示以下文本:
随着减排的步伐不断加快, 电动和混合动力技术正在接近中心舞台。在 2015年,Engine Expo将再次举办Electric & Hybrid 亭子!
但短语2015 和Engine Expo 都是PHP 配置文件中的变量。目前,我的 .ini 文件中的部分如下所示:
[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!"
我的页面代码如下所示:
<p>
<?=$i['pavilion']['texta'];?>
<?=$year?>
<?=$i['pavilion']['textb'];?>
<?=$show?>
<?=$i['pavilion']['textc'];?>
</p>
但是,文本段落的中断和返回是一个问题,特别是当涉及到其他语言在不同位置使用逗号或重新排列单词等时。理想情况下,我希望能够做到像这样:
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 & Hybrid Pavilion!"
或者:
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 & Hybrid Pavilion!"
但两者都不是有效的语法。有谁知道这是否可能?
编辑
收到几个答案后,我的 .ini 文件现在如下所示:
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 & Hybrid Pavilion!"
我的代码如下所示:
<?=sprintf($i['pavilion']['texta'], $year, $show); ?>
但这只是在之后打印变量?
随着减少排放的努力继续加快步伐,电动和混合动力技术越来越接近中心舞台。在 %d,%s 将再次举办 Electric & Hybrid Pavilion!2015Engine Expo
编辑#2
这是我的parse_ini_file() 代码:
$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);
编辑#3
我什至尝试过稍微分解代码,所以首先创建了一个$texta 变量,然后通过sprintf() 回显到页面上,但输出保持完全相同:
<?php
$texta = $i['pavilion']['texta'];
echo sprintf($texta, $year, $show);
?>
有效!谢谢大家。
【问题讨论】: