首先抱歉我的英语不好:)我对这个剧本有疑问。我发现这个脚本进入了一个旧框架,我想将它用于我的朋友,但我不能让这个脚本在没有整个框架的情况下工作。
string.php
<?php
class StringResource{
private $string = array();
public function StringResource($path){
global $lang;
if($lang == null){
$file = $path . 'strings.xml';
if(file_exists($file))
$xml = simplexml_load_file($file);
}else{
$file = $path . 'strings_'.$lang.'.xml';
if(file_exists($file)){
$xml = simplexml_load_file($file);
}else{
$file = $path . 'strings.xml';
if(file_exists($file))
$xml = simplexml_load_file($file);
}
}
if($xml){
foreach($xml->children() as $child) {
if($child->getName() == 'string'){
$attribute = $child->attributes();
$key = (String) $attribute['name'];
$this->string[$key] = (String) $child;
}
}
}
}
public function Get($key){
return $this->string[$key];
}
}
我知道我可以使用gettext等,但我的朋友知识非常低,所以编辑一个文件对他来说是最好的。
strings.xml这里是我的xml示例
<?xml version="1.0" encoding="utf-8"?>
<strings>
<string name="menu1">Menu 1 name</string>
<string name="menu2">menu 2 name</string>
....
当我想使用它们时我使用{{ST:menu1}}所以我不需要处理回声。那么这个脚本缺少什么才能正常工作? get key函数就像替换?
使用以下命令创建对象:
$sr = new StringResource("/path/to/directory/");
XML数据在/path/to/directory/strings.xml
中的位置。
然后你可以得到一个字符串:
$menu1 = $sr->Get("menu1");
顺便说一句,使用类的名称作为方法名是在PHP中编写构造函数的过时方法。现代的方式是命名为__construct
。