そのため、複数の言語で作業する必要があるこのWebサイトを作成しています。私はざっと見て、言語ごとに1つずつXMLファイルを使用する必要があるという結論に達しました。魔女は私には理にかなっていますが、ここに私の問題があります:
次のようなXMLファイルを作成しました。
<?xml version="1.0" encoding="utf-8"?>
<translations>
<frontpage>
<!--Main translation-->
<translation string="email" value="E-mail" />
<translation string="password" value="Password" />
<translation string="createacc" value="Create account" />
<translation string="login" value="Login" />
<!--Errors-->
<translation string="erroremail1" value="E-mail not valid" />
<translation string="erroremail2" value="There's no account with that e-mail" />
<translation string="errorpass" value="Incorrect password" />
</frontpage>
</translations>
しかし、PHPライブラリからのXMLReaderとDOMDocumentがどのように機能するかを理解できません。これは私がこれまでに持っているコードです:
public function Translate($page,$string,$variables = array()) {
$reader = new XMLReader();
$reader->open(www::findFile("lang/".$this->short.".xml"));
//Here i want to find the <translation> with the attribute string that is === $string
//With the parent of $page (fx. Translate("frontpage","erroremail1"))
$reader->close();
$find = array();
for ($x = 0; count($find) != count($variables); $x++) $find[] = "{".$x."}";
return (isset($value)) ? str_replace($find,$variables,$value) : "NOT TRANSLATED (".$string.")";
}
解決:
public function Translate($page,$string,$variables = array()) {
//Read from XML
$reader = simplexml_load_file(www::findFile("lang/".$this->short.".xml"));
foreach ($reader->$page->translation as $t) if ($t['string'] == $string) { $value = $t['value']; break; }
$find = array();
for ($x = 0; count($find) != count($variables); $x++) $find[] = "{".$x."}";
return (isset($value)) ? str_replace($find,$variables,$value) : "NOT TRANSLATED (".$string.")";
}