2

我正在将我的网站翻译成不同的语言,并且我有超过 130 个页面,所以我想通过一个将替换关键字
IE 的函数传递我的 .php 文件:附件 = อุปกรณ์<br /> 这是英语到泰语。

但是我可以使用我的方法让它工作......我在这些页面中有php(显然),输出只显示html而不执行php

是否有标题方法或我必须在我的 php 页面开头传递的东西..

这是我用来查找文本结果然后从我的 php 文件中替换它们的函数。

    <?php

    // lang.php
    function get_lang($file)
    {

    // Include a language file
        include 'lang_thai.php';

    // Get the data from the HTML
        $html = file_get_contents($file);

    // Create an empty array for the language variables

        $vars = array();

    // Scroll through each variable

        foreach($lang as $key => $value)

        {

            // Finds the array results in my lang_thai.php file (listed below)

            $vars[$key] = $value;
        }


    // Finally convert the strings

        $html = strtr($html, $vars);

    // Return the data


        echo $html;

    }

    ?>

//这是lang_thai.php文件

    <?php

    $lang = array(
    'Hot Items' => 'รายการสินค้า', 
    'Accessories' => 'อุปกรณ์'

    );


    ?>
4

1 回答 1

1

许多框架使用一个函数来进行翻译,而不是使用 .pot 文件进行替换。该函数如下所示:

<h1><?php echo _('Hello, World') ?>!</h1>

因此,如果它是英语且未翻译,则该函数只会返回未翻译的字符串。如果要翻译它,那么它将返回翻译后的字符串。

如果您想继续执行绝对更快的路线,请尝试以下操作:

<?php
    function translate($buffer) {
        $translation = include ('lang_tai.php');

        $keys = array_keys($translation);
        $vals = array_values($translation);

        return str_replace($keys, $vals, $buffer);
    }

    ob_start('translate');

    // ... all of your html stuff

您的语言文件是:

<?php
    return array(
        'Hot Items' => 'รายการสินค้า', 
        'Accessories' => 'อุปกรณ์'
    );

一件很酷的事情是include可以返回值!所以这是从文件传递值的好方法。ob_start 也是一个带有回调的输出缓冲区。因此,在您将所有 html 回显到屏幕之后,就在它实际显示到屏幕之前,它会将所有数据传递给translate函数,然后我们翻译所有数据!

于 2013-06-06T21:34:41.337 回答