我做了一些测试,将翻译字符串用作数组键,如gettext msgid
简单示例:
function translate( $str, $locale = 'en')
{
// $languages = load_from_file(); will use fallowing var just for sample
$languages =[
'en' => [
'Hello World' => 'Hello World',
'What time is it?' => 'What time is it?',
'What time is it now?' => 'What time is it now?',
'Supported symbols [. ? # $ , / * + - _ ]' => 'Simple supported symbols . ? # $ , / * + - _',
],
'de' => [
'Hello World' => 'Hallo Welt',
'What time is it?' => 'Wie spät ist es?',
'What time is it now?' => 'Wie viel Uhr ist es jetzt?',
'Supported symbols [. ? # $ , / * + - _ ]' => 'Unterstützte Symbole [. ? # $, / * + - _]',
]
];
$lang = $languages[ $locale ];
return $lang[ $str ] ?? 'Unknown String';
}
echo translate( 'Supported symbols [. ? # $ , / * + - _ ]' );
// returns "Unterstützte Symbole [. ? # $, / * + - _]"
echo translate( 'What time is it?' );
// returns "Wie spät ist es?"
优势和优势是什么?缺点还是可以是任何问题?使用它是否安全?