gettext() equivalent in Intl library?

后端 未结 3 738
梦如初夏
梦如初夏 2020-12-31 09:35

I\'m looking for a way to to do i18n and l10n.

I\'ve used gettext before and it was good: I would simply create .mo files in different languages and ever

3条回答
  •  执念已碎
    2020-12-31 09:40

    Here's how I used intl for translations (tested on php v. 5.3.10 and 5.4.7):

    intl.php

    namespace Example;
    class Intl {
    
      private $resource;
    
      public function __construct() {
        $bundle_location = "./locales";
        $user_locale = \Locale::acceptFromHttp( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
        $this->resource = new \ResourceBundle( $user_locale, $bundle_location );
      }
    
     ...
    

    display.php

     use Example\Intl;
    
     $intl = new Intl();
     $r = $intl->resource;
    
     echo $r['string_to_be_translated'];
    
     ...
    

    resource bundles

    In the locales directory, I have my resource files:

    root.res - root language (English)

    root { 
      string_to_be_translated {String to be translated } 
    }
    

    ja.res - Japanese

    ja { 
      string_to_be_translated {\u5909\u63DB\u3055\u308C\u308B\u6587\u5B57\u5217 }
    }
    

    sp.res - Spanish

    sp { 
      string_to_be_translated {Cadena a ser traducido }
    }
    

    (etc)

提交回复
热议问题