【问题标题】:PHP language files in XMLXML 中的 PHP 语言文件
【发布时间】:2013-02-20 23:04:03
【问题描述】:

所以我正在制作这个必须有多种语言的网站。我四处寻找并得出结论,我应该使用 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.")";
}

【问题讨论】:

  • simplexml_load_file 是更好的选择。 blog.teamtreehouse.com/how-to-parse-xml-with-php5
  • 如果您对使 XMLReader、DOMDocument 或 SimpleXML 工作的了解不够,那么您真的确定决定使用 XML 是个好主意
  • 马克贝克,是的,我是。我宁愿花几天时间学习这些东西,然后使用某种后门。 Sibiraj thx 的链接,它真的很有帮助

标签: php xml domdocument xmlreader


【解决方案1】:

如果您在文件中也对语言进行编码,您可以(但不能)将多种语言放入同一个文件中。 XML 确实支持两者。只是说,因为您选择了 XML,这将是一个好处。

在你的程序内部你只需要一个映射

page + string + language:= translation

在你的操作中,这是一种语言,你甚至可以忽略它,因此你可以只取一个数组:

$translations[$string]

每种语言和页面。然后你需要做的就是将你的文件转换成一个数组:

$getTranslations = function($file, $page) {
   $translations = [];
   foreach(simplexml_load_file($file)->$page->translation as $translation)
   {
       $translations[$translation['string']] = $translation['value'];
   }
   return $translations;        
}

$translations = $getTranslations($file, $page); 

if (isset($translations[$string]) {
   // found
} else {
   // not found (e.g. fallback to $string)
}

这确实为优化留下了很大的空间,例如您可以将每个文件/页面的翻译保存在内存中,因此您只需要加载一次。或者您可以使用xpath() 来获取值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多