Esempio n. 1
0
 public function testExtend()
 {
     $l1 = new Locale();
     $l1->set('Hello, World!', 'Hej, Verden!');
     $l1->set('Hello, %1!', 'Hej, %1!');
     $l1->set('There are %1 users', array('Der er %1 bruger', 'Der er %1 brugere'));
     $l2 = new Locale();
     $l2->set('Create user', 'Opret bruger');
     $l2->set('Hello, %1!', 'Halløj, %1!');
     $l2->extend($l1);
     $this->assertEquals('Hej, Verden!', $l2->get('Hello, World!'));
     $this->assertEquals('Hej, World!', $l2->get('Hello, %1!', 'World'));
     $this->assertEquals('Der er 0 brugere', $l2->nget('There are %1 users', 'There is %1 user', 0));
     $this->assertEquals('Der er 1 bruger', $l2->nget('There are %1 users', 'There is %1 user', 1));
     $this->assertEquals('Der er 2 brugere', $l2->nget('There are %1 users', 'There is %1 user', 2));
 }
Esempio n. 2
0
 /**
  * Read a gettext MO-file.
  *
  * @param string $file
  *            MO-file.
  * @return Locale Localization object.
  */
 public static function readMo($file)
 {
     $f = file_get_contents($file);
     if (!is_string($f)) {
         throw new LocaleException('Could not open file: ' . $file);
     }
     $magic = bin2hex(substr($f, 0, 4));
     if ($magic === '950412de') {
         // Big endian
         $header = 'Nrev/NN/NO/NT/NS/NH';
         $format = 'N';
     } elseif ($magic === 'de120495') {
         // Little endian
         $header = 'Vrev/VN/VO/VT/VS/VH';
         $format = 'V';
     } else {
         throw new LocaleException('Not a valid MO file: incorrect magic number: ' . $magic);
     }
     $o = 4;
     $data = unpack($header, substr($f, $o, 24));
     $num = $data['N'];
     $oOffset = $data['O'];
     $tOffset = $data['T'];
     if ($num == 0) {
         return new Locale();
     }
     $format = $format . $num * 2;
     $o = $oOffset;
     $oTable = unpack($format, substr($f, $o, 8 * $num));
     $o = $tOffset;
     $tTable = unpack($format, substr($f, $o, 8 * $num));
     $offsets = array();
     $n = $num * 2;
     $o = $oTable[2];
     $messages = array();
     for ($i = 1; $i <= $n; $i += 2) {
         $length = $oTable[$i];
         if ($length == 0) {
             $message = '';
             $o += 1;
         } else {
             $message = substr($f, $o, $length);
             $o += $length + 1;
             $hasNul = strpos($message, "");
             if ($hasNul !== false) {
                 $message = substr($message, $hasNul + 1);
                 // gets plural
             }
         }
         $messages[$i] = $message;
     }
     $o = $tTable[2];
     $l = new Locale();
     for ($i = 1; $i <= $n; $i += 2) {
         $length = $tTable[$i];
         if ($length > 0) {
             $translation = substr($f, $o, $length);
             $o += $length + 1;
             if ($messages[$i] == '') {
                 $properties = explode("\n", $translation);
                 foreach ($properties as $property) {
                     list($property, $value) = explode(':', $property, 2);
                     if (trim(strtolower($property)) == 'plural-forms') {
                         $l->pluralForms = $value;
                         break;
                     } elseif (trim(strtolower($property)) == 'language') {
                         $value = LanguageTag::parseTag($value);
                         $l->name = $value[0];
                         $l->region = $value[1];
                     }
                 }
             }
             if (strpos($translation, "") !== false) {
                 $translation = explode("", $translation);
             }
             $l->set($messages[$i], $translation);
         } else {
             $o += 1;
         }
     }
     return $l;
 }