Ejemplo n.º 1
0
 public static function getTranslationsAsEntries($domain = null, $entries = false)
 {
     if (!$entries) {
         $entries = new Entries();
     }
     if (empty($domain)) {
         $domain = self::$domain;
     }
     //try to get translations from dictionary
     if (!empty($entries)) {
         foreach ($entries as $translator) {
             if (isset(self::$dictionary[$domain][$translator->original][1])) {
                 $translator->setTranslation(self::$dictionary[$domain][$translator->original][1]);
             } elseif (isset(self::$dictionary[$domain][$translator->context . self::$context_glue . $translator->original][1])) {
                 $translator->setTranslation(self::$dictionary[$domain][$translator->context . self::$context_glue . $translator->original][1]);
             }
             if (isset(self::$dictionary[$domain][$translator->original][2])) {
                 $translator->setPluralTranslation(self::$dictionary[$domain][$translator->original][2], 0);
             }
         }
     } elseif (!empty(self::$dictionary[$domain])) {
         foreach (self::$dictionary[$domain] as $original => $array_trans) {
             $context = '';
             $entries->insert($context, $original, $array_trans[0]);
             if ($translation = $entries->find($context, $original)) {
                 $translation->setTranslation($array_trans[1]);
                 if (!empty($array_trans[2])) {
                     $translation->setTranslation($array_trans[2]);
                 }
             }
         }
     }
     return $entries;
 }
Ejemplo n.º 2
0
 public static function generate(Entries $entries, $string = false)
 {
     $array = array();
     $context_glue = Translator::$context_glue;
     foreach ($entries as $translation) {
         $key = ($translation->hasContext() ? $translation->getContext() . $context_glue : '') . $translation->getOriginal();
         $entry = array($translation->getPlural(), $translation->getTranslation());
         if ($translation->hasPluralTranslation()) {
             $entry = array_merge($entry, $translation->getPluralTranslation());
         }
         $array[$key] = $entry;
     }
     $domain = $entries->getDomain() ?: 'messages';
     $translations = array($domain => array('' => array('domain' => $domain, 'lang' => Translator::$lang, 'plural-forms' => 'nplurals=2; plural=(n != 1);')));
     $translations[$domain] = array_merge($translations[$domain], $array);
     if ($string) {
         return '<?php return ' . var_export($translations, true) . '; ?>';
     }
     return $translations;
 }
Ejemplo n.º 3
0
 public static function parse($file, Entries $entries)
 {
     $content = (include $file);
     $content = $content['messages'];
     $entries_info = isset($content['']) ? $content[''] : null;
     unset($content['']);
     if (isset($entries_info['domain'])) {
         $entries->setDomain($entries_info['domain']);
     }
     foreach ($content as $key => $message) {
         $key = explode(Translator::$context_glue, $key);
         $context = isset($key[1]) ? array_shift($key) : '';
         $original = array_shift($key);
         $plural = array_shift($message);
         $translation = array_shift($message);
         $plural_translation = array_shift($message);
         $entry = $entries->find($context, $original, $plural) ?: $entries->insert($context, $original, $plural);
         $entry->setTranslation($translation);
         $entry->setPluralTranslation($plural_translation);
     }
 }
Ejemplo n.º 4
0
 public static function parse($file, Entries $entries)
 {
     $tokens = token_get_all(file_get_contents($file));
     $functions = array();
     $currentFunction = null;
     foreach ($tokens as $k => $value) {
         if (is_string($value)) {
             if ($value === ')' && $currentFunction) {
                 $functions[] = $currentFunction;
                 $currentFunction = null;
             }
             continue;
         }
         if ($currentFunction && $value[0] === T_CONSTANT_ENCAPSED_STRING) {
             $val = $value[1];
             if ($val[0] === '"') {
                 $val = str_replace('\\"', '"', $val);
             } else {
                 $val = str_replace("\\'", "'", $val);
             }
             $currentFunction[] = substr($val, 1, -1);
             continue;
         }
         if ($value[0] === T_STRING && is_string($tokens[$k + 1]) && $tokens[$k + 1] === '(') {
             if (in_array($value[1], self::$functions)) {
                 $currentFunction = array($value[1], $value[2]);
             }
             continue;
         }
     }
     foreach ($functions as $args) {
         $function = array_shift($args);
         if (!isset(self::$functions[$function])) {
             continue;
         }
         $line = array_shift($args);
         switch (self::$functions[$function]) {
             case '__':
                 $original = $args[0];
                 $translation = $entries->find('', $original) ?: $entries->insert('', $original);
                 break;
             case 'n__':
                 $original = $args[0];
                 $plural = $args[1];
                 $translation = $entries->find('', $original, $plural) ?: $entries->insert('', $original, $plural);
                 break;
             case 'p__':
                 $context = $args[0];
                 $original = $args[1];
                 $translation = $entries->find($context, $original) ?: $entries->insert($context, $original);
                 break;
         }
         $translation->addReference($file, $line);
     }
 }
Ejemplo n.º 5
0
    public static function parse($file, Entries $entries)
    {
        $content = file_get_contents($file);
        $keywords = implode("|", array_keys(self::$functions));
        $ps_trans_pattern = '(.*[^\\\\])';
        $regex = '/\\{(' . $keywords . ')
			(
				\\s*(original)\\=[\'\\"]' . $ps_trans_pattern . '[\'\\"]?|
				\\s*(plural)\\=[\'\\"]' . $ps_trans_pattern . '[\'\\"]?|
				\\s*(context)\\=[\'\\"]' . $ps_trans_pattern . '[\'\\"]?|
				\\s*[a-z0-9]+\\=[\'\\"]*' . $ps_trans_pattern . '[\'\\"]*?
			)+
			\\s*\\}/ixU';
        preg_match_all($regex, $content, $matches, PREG_SET_ORDER);
        if (!empty($matches)) {
            foreach ($matches as $match) {
                if (!isset(self::$functions[$match[1]])) {
                    continue;
                }
                switch (self::$functions[$match[1]]) {
                    case '__':
                        $original = $match[4];
                        $translation = $entries->find('', $original) ?: $entries->insert('', $original);
                        break;
                    case 'n__':
                        $original = $match[4];
                        $plural = $match[6];
                        $translation = $entries->find('', $original, $plural) ?: $entries->insert('', $original, $plural);
                        break;
                    case 'p__':
                        $original = $match[4];
                        $context = $match[8];
                        $translation = $entries->find($context, $original) ?: $entries->insert($context, $original);
                        break;
                    case 'np__':
                        $original = $match[4];
                        $plural = $match[6];
                        $context = $match[8];
                        $translation = $entries->find($context, $original, $plural) ?: $entries->insert($context, $original, $plural);
                        break;
                }
            }
        }
    }
Ejemplo n.º 6
0
 public static function parse($file, Entries $entries)
 {
     $stream = new CachedFileReader($file);
     if (!$stream || isset($stream->error)) {
         return false;
     }
     $magic = self::readInt($stream, 'V');
     if ($magic === self::MAGIC1 || $magic === self::MAGIC3) {
         //to make sure it works for 64-bit platforms
         $byteOrder = 'V';
         //low endian
     } elseif ($magic === (self::MAGIC2 & 0xffffffff)) {
         $byteOrder = 'N';
         //big endian
     } else {
         $this->error = 1;
         //not MO file
         return false;
     }
     self::readInt($stream, $byteOrder);
     $total = self::readInt($stream, $byteOrder);
     //total string count
     $originals = self::readInt($stream, $byteOrder);
     //offset of original table
     $translations = self::readInt($stream, $byteOrder);
     //offset of translation table
     $stream->seekto($originals);
     $table_originals = self::readIntArray($stream, $byteOrder, $total * 2);
     $stream->seekto($translations);
     $table_translations = self::readIntArray($stream, $byteOrder, $total * 2);
     for ($i = 0; $i < $total; $i++) {
         $stream->seekto($table_originals[$i * 2 + 2]);
         $original = $stream->read($table_originals[$i * 2 + 1]);
         if ($original) {
             $stream->seekto($table_translations[$i * 2 + 2]);
             $entries->insert(null, $original)->setTranslation($stream->read($table_translations[$i * 2 + 1]));
         }
     }
 }
Ejemplo n.º 7
0
 public static function generate(Entries $entries)
 {
     $lines = array('msgid ""', 'msgstr ""');
     $headers = array_replace(array('Project-Id-Version' => '', 'Report-Msgid-Bugs-To' => '', 'Last-Translator' => '', 'Language-Team' => '', 'MIME-Version' => '1.0', 'Content-Type' => 'text/plain; charset=UTF-8', 'Content-Transfer-Encoding' => '8bit'), $entries->getHeaders());
     $headers['POT-Creation-Date'] = $headers['PO-Revision-Date'] = date('c');
     foreach ($headers as $name => $value) {
         $lines[] = '"' . $name . ': ' . $value . '\\n"';
     }
     $lines[] = '';
     //Entries
     foreach ($entries as $translation) {
         if ($translation->hasComments()) {
             foreach ($translation->getComments() as $comment) {
                 $lines[] = '# ' . $comment;
             }
         }
         if ($translation->hasReferences()) {
             foreach ($translation->getReferences() as $reference) {
                 $lines[] = '#: ' . $reference[0] . ':' . $reference[1];
             }
         }
         if ($translation->hasContext()) {
             $lines[] = 'msgctxt ' . self::quote($translation->getContext());
         }
         if ($translation->hasPlural()) {
             $lines[] = 'msgid ' . self::quote($translation->getOriginal());
             $lines[] = 'msgid_plural ' . self::quote($translation->getPlural());
             $lines[] = 'msgstr[0] ' . self::quote($translation->getTranslation());
             foreach ($translation->getPluralTranslation() as $k => $v) {
                 $lines[] = 'msgstr[' . ($k + 1) . '] ' . self::quote($v);
             }
         } else {
             $lines[] = 'msgid ' . self::quote($translation->getOriginal());
             $lines[] = 'msgstr ' . self::quote($translation->getTranslation());
         }
         $lines[] = '';
     }
     return implode("\n", $lines);
 }
Ejemplo n.º 8
0
 public static function parse($file, Entries $entries)
 {
     $lines = file($file, FILE_IGNORE_NEW_LINES);
     $i = 2;
     while (($line = trim($lines[$i++])) !== '') {
         $line = self::clean($line);
         if (strpos($line, ':')) {
             $header = explode(':', $line, 2);
             $entries->setHeader($header[0], $header[1]);
         }
     }
     $translation = new Translation();
     for ($n = count($lines); $i < $n; $i++) {
         $line = trim($lines[$i]);
         $line = self::fixMultiLines($line, $lines, $i);
         if ($line === '' && $translation->hasOriginal()) {
             $entries[] = $translation;
             $translation = new Translation();
             continue;
         }
         list($key, $data) = preg_split('/\\s/', $line, 2);
         $append = null;
         switch ($key) {
             case '#,':
             case '#':
             case '#.':
                 $translation->addComment($data);
                 $append = null;
                 break;
             case '#:':
                 if (strpos($data, ':')) {
                     $data = explode(':', $data);
                     $translation->addReference($data[0], $data[1]);
                 }
                 $append = null;
                 break;
             case 'msgctxt':
                 $translation->setContext(self::clean($data));
                 $append = 'Context';
                 break;
             case 'msgid':
                 $translation->setOriginal(self::clean($data));
                 $append = 'Original';
                 break;
             case 'msgid_plural':
                 $translation->setPlural(self::clean($data));
                 $append = 'Plural';
                 break;
             case 'msgstr':
             case 'msgstr[0]':
                 $translation->setTranslation(self::clean($data));
                 $append = 'Translation';
                 break;
             case 'msgstr[1]':
                 $translation->setPluralTranslation(self::clean($data));
                 $append = 'PluralTranslation';
                 break;
             default:
                 if (strpos($key, 'msgstr[') === 0) {
                     $translation->addPluralTranslation(self::clean($data));
                     $append = 'PluralTranslation';
                     break;
                 }
                 if (isset($append)) {
                     if ($append === 'PluralTranslation') {
                         $key = count($this->getPluralTranslation()) - 1;
                         $this->setPluralTranslation($this->getPluralTranslation($key) . self::clean("\n" . $data), $key);
                         break;
                     }
                     $getMethod = 'get' . $append;
                     $setMethod = 'set' . $append;
                     $translation->{$setMethod}($translation->{$getMethod}() . self::clean("\n" . $data));
                 }
                 break;
         }
     }
     if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($entries))) {
         $entries[] = $translation;
     }
     return $entries;
 }
Ejemplo n.º 9
0
 public static function parse($file, Entries $entries)
 {
     $content = file_get_contents($file);
     $encoding = mb_detect_encoding($content, array('UTF-8', 'ISO-8859-1', 'WINDOWS-1252'), true);
     if ($encoding && ($encoding !== 'UTF-8' || mb_check_encoding($content, 'UTF-8') === false)) {
         $content = utf8_encode($content);
     }
     $functions = implode('|', array_keys(self::$functions));
     $content = htmlspecialchars($content, ENT_NOQUOTES);
     $length = strlen($content);
     $index = 0;
     while ($index < $length) {
         if (($index = strpos($content, '(', $index)) === false) {
             break;
         }
         if (preg_match('/(^|[^\\w-])(' . $functions . ')$/', substr($content, 0, $index), $matches) !== 1) {
             $index++;
             continue;
         }
         $function = $matches[2];
         $start = $index - strlen($function);
         $quote = null;
         $buffer = '';
         $args = array();
         $l = $p = null;
         $index++;
         for ($in = 0; $index < $length; $index++) {
             $p = $l;
             $l = $content[$index];
             switch ($l) {
                 case '"':
                     if ($quote === '"' && $p !== '\\') {
                         $quote = null;
                     } else {
                         if ($quote === null) {
                             $quote = '"';
                         } else {
                             $buffer .= $l;
                         }
                     }
                     break;
                 case "'":
                     if ($quote === "'" && $p !== '\\') {
                         $quote = null;
                     } else {
                         if ($quote === null) {
                             $quote = "'";
                         } else {
                             $buffer .= $l;
                         }
                     }
                     break;
                 case ',':
                     if ($quote === null) {
                         $args[] = $buffer;
                         $buffer = '';
                     }
                     break;
                 case ')':
                     if ($quote === null) {
                         $args[] = $buffer;
                         break 2;
                     }
                     $buffer .= $l;
                     break;
                 case ' ':
                     if ($quote !== null) {
                         $buffer .= $l;
                     }
                     break;
                 default:
                     $buffer .= $l;
                     break;
             }
         }
         foreach ($args as &$arg) {
             $arg = str_replace('\\', '', $arg);
         }
         switch (self::$functions[$function]) {
             case '__':
                 $original = $args[0];
                 $translation = $entries->find('', $original) ?: $entries->insert('', $original);
                 break;
             case 'n__':
                 $original = $args[0];
                 $plural = isset($args[1]) ? $args[1] : '';
                 $translation = $entries->find('', $original, $plural) ?: $entries->insert('', $original, $plural);
                 break;
             case 'p__':
                 $context = $args[0];
                 $original = $args[1];
                 $translation = $entries->find($context, $original) ?: $entries->insert($context, $original);
                 break;
         }
     }
 }
Ejemplo n.º 10
0
    public static function parse($file, Entries $entries)
    {
        $strings = $regs = array();
        $content = file_get_contents($file);
        $encoding = mb_detect_encoding($content, array('UTF-8', 'ISO-8859-1', 'WINDOWS-1252'), true);
        if ($encoding && ($encoding !== 'UTF-8' || mb_check_encoding($content, 'UTF-8') === false)) {
            $content = utf8_encode($content);
        }
        $content = htmlspecialchars($content, ENT_NOQUOTES);
        $content = preg_replace_callback('# ( / (?: (?>[^/\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\/ )+ (?<!\\\\)/ ) [a-z]* \\b #ix', function ($match) use(&$regs) {
            $counter = count($regs);
            $regs[$counter] = $match[1];
            return "<<reg{$counter}>>";
        }, $content);
        $content = preg_replace_callback(array('# " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" #ix', "# ' ( (?: (?>[^'\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\' )* ) (?<!\\\\)' #ix"), function ($match) use(&$regs, &$strings) {
            $counter = count($strings);
            $strings[$counter] = preg_replace_callback("#<<reg(\\d+)>>#", function ($match) use($regs) {
                return $regs[$match[1]];
            }, $match[0]);
            return "<<s{$counter}>>";
        }, $content);
        //delete line comments
        $content = preg_replace("#(//.*?)\$#m", '', $content);
        //delete multiline comments
        $content = preg_replace('#/\\*(.*?)\\*/#is', '', $content);
        $content = preg_replace_callback("#<<s(\\d+)>>#", function ($match) use($strings) {
            return $strings[$match[1]];
        }, $content);
        var_dump($content);
        $keywords = implode('|', array_keys(self::$functions));
        //TODO: to exclude function definitions
        //		preg_match_all("#(?!.*function\s*)($keywords)\s*\((.*?)\)#ix", $content, $matches, PREG_SET_ORDER);
        //TODO: try to exclude function definitions
        //		preg_match_all("#(?!( function\s*\(.*\) )) ($keywords)\s*\(( .* ) \)#sixU", $content, $matches, PREG_SET_ORDER);
        //
        #
        /**/
        //prestashop
        //		preg_match_all('/\{l\s*s=[\'\"] (.*[^\\\\]) [\'\"](\s*sprintf=.*)?(\s*js=1)?\s*\}/U', $content, $matches, PREG_SET_ORDER);
        //		$regex = '/('.$keywords.')\s*\(\s*s=[\'\"] (.*[^\\\\]) [\'\"]\s*\)/ixU';
        //		$regex = '/('.$keywords.')\s*\(\s*[\'\"] (.*[^\\\\]) [\'\"]\s*\)/ixU';
        //
        //		$regex = "#
        //			(?<!function\s{1})
        //			($keywords)
        //			\s*\(\s*
        //
        //					(?:
        //						((?:
        //							\"(?: \\\\\"| [^\"] )+\"
        //						),*)*
        //						|
        //						((?:
        //							'((?: \\\' | [^'] | )+)'
        //						),*)*
        //					)+
        //
        //			\)
        //			#ixU";
        $regex = '#
			(?<!function\\s{1})
			(' . $keywords . ')				
			\\s*\\(\\s*
				(				
					(
					
						[\'\\"]
							(
								(?!\\)).*
							)
						[\'\\"]
						
					)
					[,]*
					[0-9]*\\w*
				)+

			\\s*\\)
			#ixU';
        var_dump($regex);
        preg_match_all($regex, $content, $matches, PREG_SET_ORDER);
        var_dump($matches);
        foreach ($matches as $match) {
            if (!isset(self::$functions[$match[1]])) {
                continue;
            }
            switch (self::$functions[$match[1]]) {
                case '__':
                    $original = $match[2];
                    $translation = $entries->find('', $original) ?: $entries->insert('', $original);
                    break;
                case 'n__':
                    $original = $match[2];
                    $plural = $match[3];
                    $translation = $entries->find('', $original, $plural) ?: $entries->insert('', $original, $plural);
                    break;
                case 'p__':
                    $context = $match[2];
                    $original = $match[3];
                    $translation = $entries->find($context, $original) ?: $entries->insert($context, $original);
                    break;
            }
        }
    }