0

我正在使用此功能来翻译内容:

function t($string)
{
    global $_ACTIVE_LANGUAGE;
    if(is_array($_ACTIVE_LANGUAGE) && array_key_exists($string,$_ACTIVE_LANGUAGE) )
    {
        return (!empty($_ACTIVE_LANGUAGE[$string])) ? $_ACTIVE_LANGUAGE[$string] : $string; 
    } else {
        return $string;
    }
}

它工作得很好,我放了 t('hola') 并且如果有带有数组 'hola' => 'hello' 的英文文件,它会翻译它。

但是,现在我希望能够翻译可以包含比字符串更多的文本的字符串,例如这个例子:

$string1 = 'download-file-justin-bieber-awesome-voice.html';
$string2 = 'view-file-rihanna-very-sexy.html';
$string3 = 'mostseen12345.html';
$string4 = 'incredible:stuff-and:real-things.html';

$array = array
(
    'download-file' => 'descargar-archivo',
    'view-file' => 'ver-archivo',
    'mostseen' => 'masvistos',
    'incredible:stuff' = 'cosas:increibles'
}

我希望脚本能够翻译给定字符串中数组键中的部分。这可能吗?

4

1 回答 1

1

您可以查看php -manualstr_replace()

$from = array('download-file','view-file','mostseen','incredible:stuff');
$to   = array('descargar-archivo','ver-archivo','masvistos','cosas:increibles');

$translated_string = str_replace($from,$to,$original_string);
于 2012-04-29T01:21:52.343 回答