示例#1
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);
     }
 }
示例#2
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;
 }
示例#3
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;
                }
            }
        }
    }
示例#4
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);
     }
 }
示例#5
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;
         }
     }
 }
示例#6
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;
            }
        }
    }