Best way to internationalize simple PHP website

后端 未结 4 1676
不思量自难忘°
不思量自难忘° 2020-12-01 06:55

I have to develop a pretty simple php website so I don\'t need framework. But it\'s must support multi language (EN/FR/CHINESE). I have looked for php built in system and I

4条回答
  •  醉话见心
    2020-12-01 07:24

    Although ext/gettext and ext/intl are both related to i18 (internationalization), gettext deals with translation while intl deals with internationalizing things like number and date display, sorting orders and transliteration. So you'd actually need both for a complete i18-solution. Depending on your needs you may come up with an home-brew solution relying on the extensions mentioned above or your use components provided by some framework:

    • Translation
      • Symfony 2 Translation component: https://github.com/symfony/Translation
      • Zend Framework Zend_Translate
    • Internationalization
      • Zend Framework Zend_Locale

    If you only need translation and the site is simple enough, perhaps your simple solution (reading a translation configuration file into an PHP array, using a simple function to retrieve a token) might be the easiest.

    The most simple solution I can think of is:

    $translation = array(
        'Hello world!' => array(
            'fr' => 'Bonjour tout le monde!',
            'de' => 'Hallo Welt!'
        )
    );
    
    if (!function_exists('gettext')) {
        function _($token, $lang = null) {
            global $translation;
            if (   empty($lang)
                || !array_key_exists($token, $translation)
                || !array_key_exists($lang, $translation[$token])
            ) {
                return $token;
            } else {
                return $translation[$token][$lang];
            }
        }
    }
    
    echo _('Hello World!');
    

提交回复
热议问题