使用翻译字符串作为多语言的数组键,如Gettext msgid?

时间:2018-02-08 07:46:52

标签: php localization multilingual

我做了一些测试,将翻译字符串用作数组键,如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?"

优势和优势是什么?缺点还是可以是任何问题?使用它是否安全?

0 个答案:

没有答案