PHP页面将文本更改为常量[关闭]

PHP页面将文本更改为常量[关闭]

问题描述:

I have a php site and Now i want to make it multilingual. Is there any way to parse the php pages and find text and replace it with similar php constants? I want to make an automatic script which first find all php pages inside it then find all the nodes which having text inside it and then replace it with php constatnt and at the same time add a new entry of this constant into database.

我有一个php网站,现在我想让它多语言化。 有没有办法解析php页面并找到文本并用类似的php常量替换它? 我想创建一个自动脚本,首先找到其中的所有php页面然后找到其中包含文本的所有节点,然后用php constatnt替换它,同时将此常量的新条目添加到数据库中。 p> div>

There are a lot ways. I suggest you do do it like

before: <title>Hello World</title>

after: <title><?=ts("Hello World");?></title>

Then write a function ts($source)

which cares about output the right string. This is very easy and comfortable because you can continue coding the page in English or your language and ts() is caring about everything

You should add a function like loc("Home") to everywhere, instead of typing plain text.

<li><?php echo loc("About");?></li>

If in English, loc() should return what it was given. Else it should return the translation.

$lang = "en";
$dict = Array(
    "About" => Array(
        "fr" => "Sur"
    )
);

function loc($input) {
    if ($lang == "en") {
        return $input;
    }
    return $dict[$input][$lang];
}