PHP Gettext problems (like non-thread-safe?)

后端 未结 3 860
时光说笑
时光说笑 2020-12-30 07:49

I want to start using gettext to handle my translations on web projects (PHP 5). Since it is a widely used standard with a good reputation it seems to be the best choice.

3条回答
  •  感情败类
    2020-12-30 08:03

    I had same problem with PHP 5.6.30 VC11 Theard Safe on Windows 10. Workaround found and fix this issue here by sirio3mil.

    Apparently PHP with TS can access only Locale language folder. So when setlocale and putenv function is call with another language than system's one, folder with .mo and .po cannot be read.

    Workaround is to have only one language folder with system language and multiple pairs of .mo/.po files for each translated languages. Domain will be set with wanted language.

    Example with Swiss French, German and Italian:

    Folder structure

    \Locale\fr_CH\LC_MESSAGES

    • fr_CH.mo + fr_CH.po // system language
    • de_CH.mo + de_CH.po
    • it_CH.mo + it_CH.po

    Code

    $lang = 'fr_CH' or 'de_CH' or 'it_CH'
    
    bindtextdomain($lang, '.\Locale');
    textdomain($lang);
    bind_textdomain_codeset($lang, 'UTF-8');
    setlocale (LC_ALL, $lang);
    putenv('LC_ALL=' . $lang);
    

提交回复
热议问题