public function testGetTranslations_shouldReturnEmptyArray() { $root = vfsStream::setup('root'); $root->addChild(new vfsStreamFile('language.php')); $obj = new Language_File(vfsStream::url('root/language.php')); $this->assertEquals(array(), $obj->getTranslations()); }
public function merge() { $sourceStrings = $this->sourceFile->parse(); $targetStrings = $this->targetFile->parse(); $toUpdate = array(); foreach ($targetStrings as $key => $string) { if (isset($sourceStrings[$key]) && $sourceStrings[$key]['translated'] === true && (!isset($string['translation']) || $sourceStrings[$key]['translation'] != $string['translation'])) { $toUpdate[$key] = $sourceStrings[$key]['translation']; } } // move the code that actually writes to the language.php to another class // that would handle every ways to write to a language.php file. $tmpFilePath = $this->targetFile->filePath . '.tmp'; $handle = fopen($tmpFilePath, 'w'); $lines = file($this->targetFile->filePath); if ($handle) { // foreach each line in the target file check decide whether to keep the // current translation or use the translation from the source file if one exists foreach ($lines as $line) { $matches = array(); if (preg_match('|^/?/?\\s*\\"(.*)\\"\\s*\\=\\>\\s*\\"(.*)\\"\\s*\\,\\s*$|', $line, $matches) && isset($toUpdate[$matches[1]])) { fwrite($handle, "\"{$matches[1]}\" => \"{$toUpdate[$matches[1]]}\",\n"); } else { fwrite($handle, $line); } } rename($tmpFilePath, $this->targetFile->filePath); } }
/** * Update language.php file with new strings. * * @param array $strings English strings collected from source files * @param bool $outputFiles whether file paths were string was found should be included or not in the output * @return null */ public function writeStringsToFile(array $strings, $outputFiles = false) { if (empty($strings)) { return false; } // backup original language file copy($this->filePath, $this->filePath . '.old'); $this->translations = $this->parseFile->getTranslations(); $entries = $this->mergeStringsWithTranslations($strings, $this->translations); $handle = fopen($this->tmpFilePath, 'w'); if ($handle) { fwrite($handle, "<?php\n"); fwrite($handle, $this->fileHeader()); fwrite($handle, "\$lang = array(\n"); foreach ($entries as $entry) { fwrite($handle, $this->formatString($entry, $outputFiles)); } fwrite($handle, ");\n"); fclose($handle); } rename($this->tmpFilePath, $this->filePath); }
} // update all language.php files by calling get_strings.php $output = array(); $return_var = null; exec('php get_strings.php', $output, $return_var); if ($return_var == 1) { die("\nCouln't execute get_strings.php\n\n"); } // calculate the percentage for each language.php $outputData = array(); $globalStats = array(); // $langmapping is set on lang/langmapping.php foreach ($langmapping as $lang => $null) { $filePath = "lang/{$lang}/language.php"; if (file_exists($filePath) && $lang != 'en') { $parseFile = new Language_File($filePath); $stats = $parseFile->getStats(); $outputData[$lang] = array('total' => $stats['total'], 'untranslated' => $stats['untranslated'], 'translated' => $stats['translated'], 'percentage' => $stats['percentage']); if ($stats['percentage'] >= 70) { $globalStats['70+']++; } else { if ($stats['percentage'] >= 30) { $globalStats['30+']++; } else { if ($stats['percentage'] < 30) { $globalStats['0+']++; } } } } }