示例#1
0
 /**
  * Test non-joiner functionality.
  *
  * @return void
  */
 public function testNonJoinerOptions()
 {
     $reader = new ExtendedIniReader();
     $input = ['foo="bar"', 'baz=""'];
     $output = ['foo' => 'bar', 'baz' => ''];
     $nonJoiner = html_entity_decode('‌', ENT_NOQUOTES, 'UTF-8');
     $nonJoinerOutput = ['foo' => 'bar', 'baz' => $nonJoiner];
     // Test behavior with and without the $convertBlanks switch:
     $this->assertEquals($output, (array) $reader->getTextDomain($input, false));
     $this->assertEquals($nonJoinerOutput, (array) $reader->getTextDomain($input));
 }
示例#2
0
 /**
  * Search the path stack for language files and merge them together.
  *
  * @param string $filename    Name of file to search path stack for.
  * @param bool   $failOnError If true, throw an exception when file not found.
  *
  * @return TextDomain
  */
 protected function loadLanguageFile($filename, $failOnError = true)
 {
     // Don't load a file that has already been loaded:
     if ($this->checkAndMarkLoadedFile($filename)) {
         return new TextDomain();
     }
     $data = false;
     foreach ($this->pathStack as $path) {
         if (file_exists($path . '/' . $filename)) {
             // Load current file with parent data, if necessary:
             $current = $this->loadParentData($this->reader->getTextDomain($path . '/' . $filename));
             if ($data === false) {
                 $data = $current;
             } else {
                 $data->merge($current);
             }
         }
     }
     if ($data === false) {
         // Should we throw an exception? If not, return an empty result:
         if ($failOnError) {
             throw new InvalidArgumentException("Ini file '{$filename}' not found");
         }
         return new TextDomain();
     }
     return $data;
 }
 /**
  * Copy one language string to another
  *
  * @return \Zend\Console\Response
  */
 public function copystringAction()
 {
     // Display help message if parameters missing:
     $argv = $this->consoleOpts->getRemainingArgs();
     if (!isset($argv[1])) {
         Console::writeLine("Usage: {$_SERVER['argv'][0]} [source] [target]");
         Console::writeLine("\tsource - the source key to read");
         Console::writeLine("\ttarget - the target key to write");
         Console::writeLine("(source and target may include 'textdomain::' prefix)");
         return $this->getFailureResponse();
     }
     $reader = new ExtendedIniReader();
     $normalizer = new ExtendedIniNormalizer();
     list($sourceDomain, $sourceKey) = $this->extractTextDomain($argv[0]);
     list($targetDomain, $targetKey) = $this->extractTextDomain($argv[1]);
     if (!($sourceDir = $this->getLangDir($sourceDomain)) || !($targetDir = $this->getLangDir($targetDomain, true))) {
         return $this->getFailureResponse();
     }
     // First, collect the source values from the source text domain:
     $sources = [];
     $sourceCallback = function ($full) use($sourceKey, $reader, &$sources) {
         $strings = $reader->getTextDomain($full, false);
         if (!isset($strings[$sourceKey])) {
             Console::writeLine("Source key not found.");
         } else {
             $sources[basename($full)] = $strings[$sourceKey];
         }
     };
     $this->processDirectory($sourceDir, $sourceCallback);
     // Make sure that all target files exist:
     $this->createMissingFiles($targetDir->path, array_keys($sources));
     // Now copy the values to their destination:
     $targetCallback = function ($full) use($targetKey, $normalizer, $sources) {
         if (isset($sources[basename($full)])) {
             $fHandle = fopen($full, "a");
             fputs($fHandle, "\n{$targetKey} = \"" . $sources[basename($full)] . "\"\n");
             fclose($fHandle);
             $normalizer->normalizeFile($full);
         }
     };
     $this->processDirectory($targetDir, $targetCallback);
     return $this->getSuccessResponse();
 }
示例#4
0
 /**
  * Normalize a file from disk and returns the result as a string.
  *
  * @param string $file Filename.
  *
  * @return string
  */
 public function normalizeFileToString($file)
 {
     $reader = new Translator\Loader\ExtendedIniReader();
     // Reading and rewriting the file by itself will eliminate all comments;
     // we should extract comments separately and then recombine the parts.
     $fileArray = file($file);
     // Strip off UTF-8 BOM if necessary.
     $bom = html_entity_decode('', ENT_NOQUOTES, 'UTF-8');
     $fileArray[0] = str_replace($bom, '', $fileArray[0]);
     $comments = $this->extractComments($fileArray);
     $strings = $this->formatAsString($reader->getTextDomain($fileArray, false));
     return $comments . $strings;
 }
示例#5
0
 /**
  * Copy one language string to another
  *
  * @return \Zend\Console\Response
  */
 public function copystringAction()
 {
     // Display help message if parameters missing:
     $argv = $this->consoleOpts->getRemainingArgs();
     if (!isset($argv[1])) {
         Console::writeLine("Usage: {$_SERVER['argv'][0]} [source] [target]");
         Console::writeLine("\tsource - the source key to read");
         Console::writeLine("\ttarget - the target key to write");
         return $this->getFailureResponse();
     }
     $reader = new ExtendedIniReader();
     $normalizer = new ExtendedIniNormalizer();
     $source = $argv[0];
     $target = $argv[1];
     $langDir = realpath(__DIR__ . '/../../../../../languages');
     $handle = opendir($langDir);
     if (!$handle) {
         Console::writeLine("Could not open directory {$langDir}");
         return $this->getFailureResponse();
     }
     while ($file = readdir($handle)) {
         // Only process .ini files, and ignore native.ini special case file:
         if (substr($file, -4) == '.ini' && $file !== 'native.ini') {
             Console::writeLine("Processing {$file}...");
             $full = $langDir . '/' . $file;
             $strings = $reader->getTextDomain($full, false);
             if (!isset($strings[$source])) {
                 Console::writeLine("Source key not found.");
             } else {
                 $fHandle = fopen($full, "a");
                 fputs($fHandle, "\n{$target} = \"" . $strings[$source] . "\"\n");
                 fclose($fHandle);
                 $normalizer->normalizeFile($full);
             }
         }
     }
     return $this->getSuccessResponse();
 }
 /**
  * Assemble a new language string by combining existing ones using a
  * template.
  *
  * @return \Zend\Console\Response
  */
 public function addusingtemplateAction()
 {
     // Display help message if parameters missing:
     $argv = $this->consoleOpts->getRemainingArgs();
     if (!isset($argv[1])) {
         Console::writeLine("Usage: {$_SERVER['argv'][0]} [target] [template]");
         Console::writeLine("\ttarget - the target key to add " . "(may include 'textdomain::' prefix)\n" . "\ttemplate - the template to build the string, using ||string||" . " to import existing strings");
         return $this->getFailureResponse();
     }
     // Make sure a valid target has been specified:
     list($targetDomain, $targetKey) = $this->extractTextDomain($argv[0]);
     if (!($targetDir = $this->getLangDir($targetDomain, true))) {
         return $this->getFailureResponse();
     }
     // Extract required source values from template:
     $template = $argv[1];
     preg_match_all('/\\|\\|[^|]+\\|\\|/', $template, $matches);
     $lookups = [];
     foreach ($matches[0] as $current) {
         $key = trim($current, '|');
         list($sourceDomain, $sourceKey) = $this->extractTextDomain($key);
         $lookups[$sourceDomain][$current] = ['key' => $sourceKey, 'translations' => []];
     }
     // Look up translations of all references in template:
     $reader = new ExtendedIniReader();
     foreach ($lookups as $domain => &$tokens) {
         $sourceDir = $this->getLangDir($domain, false);
         if (!$sourceDir) {
             return $this->getFailureResponse();
         }
         $sourceCallback = function ($full) use($domain, &$tokens, $reader) {
             $strings = $reader->getTextDomain($full, false);
             foreach ($tokens as &$current) {
                 $sourceKey = $current['key'];
                 if (isset($strings[$sourceKey])) {
                     $current['translations'][basename($full)] = $strings[$sourceKey];
                 }
             }
         };
         $this->processDirectory($sourceDir, $sourceCallback, false);
     }
     // Fill in template, write results:
     $normalizer = new ExtendedIniNormalizer();
     $targetCallback = function ($full) use($template, $targetKey, $normalizer, $lookups) {
         $lang = basename($full);
         $in = $out = [];
         foreach ($lookups as $domain => $tokens) {
             foreach ($tokens as $token => $details) {
                 if (isset($details['translations'][$lang])) {
                     $in[] = $token;
                     $out[] = $details['translations'][$lang];
                 } else {
                     Console::writeLine('Skipping; no match for token: ' . $token);
                     return;
                 }
             }
         }
         $fHandle = fopen($full, "a");
         fputs($fHandle, "\n{$targetKey} = \"" . str_replace($in, $out, $template) . "\"\n");
         fclose($fHandle);
         $normalizer->normalizeFile($full);
     };
     $this->processDirectory($targetDir, $targetCallback);
 }