gettext() equivalent in Intl library?

后端 未结 3 735
梦如初夏
梦如初夏 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)

    0 讨论(0)
  • 2020-12-31 09:51

    You'd use gettext. Intl (like ICU underneath it) is for l10n, not loading translations.

    0 讨论(0)
  • 2020-12-31 10:01

    That advise wasn't very truthful. The intl functions can be used in conjunction to gettext, not as replacement.

    MessageFormatter is what people have in mind when they associate INTL with text translations. The examples suggest so. But in reality it's just a sprintf on steroids. It injects numbers into existing strings. (I'm not even sure how the locale support is of any use there, as it just serves as internal switch.)

    0 讨论(0)
提交回复
热议问题