国际化简单PHP网站的最佳方法 [英] Best way to internationalize simple PHP website

查看:302
本文介绍了国际化简单PHP网站的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须开发一个非常简单的php网站,因此不需要框架. 但是它必须支持多语言(EN/FR/CHINESE). 我寻找了内置于系统中的php,发现了两种方法:

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 found two ways :

  • intl module from php5.3 (http://php.net/manual/fr/book.intl.php)
  • gettext (http://php.net/manual/fr/book.gettext.php)

我在没有框架的情况下没有i18n的经验,因此关于支持多语言的最简单方法的任何建议是什么?

I have no experience in i18n without framework, so any advices about what's the simplest way to support multi language ?

最后,我只需要一个将翻译搜索成文件(按语言一个文件)的功能. 情商: trans('hello');

At end I just need a function that search translation into file (one file by language). EQ : trans('hello');

=> en.yaml(无论是否为Yaml,这都是示例)

=> en.yaml (yaml or not, it's an example)

hello: "Hello world!"

=> fr.yaml

=> fr.yaml

hello: "Bonjour tout le monde !"

如果可能的话,我更喜欢纯PHP实现

And if possible I prefer Pure PHP implementations

推荐答案

尽管ext/gettextext/intl都与i18(国际化)有关,但gettext处理翻译,而intl处理数字之类的国际化和日期显示,排序顺序和音译.因此,您实际上需要两个完整的i18解决方案.根据您的需求,您可以根据上述扩展或某个框架提供的使用组件提出一个自制的解决方案:

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

      如果您只需要翻译并且站点足够简单,那么也许您最简单的解决方案(将翻译配置文件读入PHP数组,使用简单的函数来检索令牌)是最简单的.

      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!');
      

      这篇关于国际化简单PHP网站的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆