首先,我为我糟糕的英语感到抱歉:)我有一个关于这个脚本的问题。我在一个旧的框架中发现了这个脚本,我想把它用在我的朋友身上,但是如果没有整个框架我就不能让这个脚本工作。
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}},所以我不需要处理echo。那么,这个脚本需要什么才能正常工作呢?get key函数的作用类似于替换吗?
发布于 2017-12-23 09:48:09
您可以使用以下命令创建对象:
$sr = new StringResource("/path/to/directory/");XML数据在/path/to/directory/strings.xml中的位置。
然后,您可以使用以下命令获得一个字符串:
$menu1 = $sr->Get("menu1");顺便说一下,在PHP中使用类名作为方法名是一种过时的编写构造函数的方式。现代的方式是将其命名为__construct。
https://stackoverflow.com/questions/47949079
复制相似问题