Created
August 10, 2010 11:46
-
-
Save gimler/517132 to your computer and use it in GitHub Desktop.
Find mising translations string for all or a given language in a symfony1 i18n folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pathToTranslationXMLFiles = 'lib/symfony/plugins/sfDoctrinePlugin/i18n'; | |
// config null for all languages or a specific language e.g. de or fr | |
$myLang = null; | |
if (isset($argv[1])) | |
{ | |
$myLang = $argv[1]; | |
echo 'Use '.$myLang . PHP_EOL; | |
} | |
// code | |
include('lib/symfony/yaml/sfYaml.php'); | |
include('lib/symfony/util/sfFinder.class.php'); | |
// find translation files | |
$files = sfFinder::type('file')->name('*.xml')->in($pathToTranslationXMLFiles); | |
// find all languages | |
$languages = array(); | |
foreach($files as $key => $file) | |
{ | |
$lang = explode('.', $file); | |
$lang = $lang[1]; | |
$languages[$lang] = 'missing'; | |
} | |
$translations = array(); | |
$languages = array_fill_keys(array_keys($languages), 'missing'); | |
// each translation xml file | |
foreach($files as $key => $file) { | |
// get language | |
$lang = explode('.', $file); | |
$lang = $lang[1]; | |
$xml = new SimpleXmlElement($file, null, true); | |
$result = $xml->xpath('/xliff/file/body/trans-unit'); | |
while(list( , $node) = each($result)) { | |
$source = (string) $node->source; | |
$target = (string) $node->target; | |
// initial translation souirce | |
if (!isset($translations[$source])) { | |
$translations[$source] = $languages; | |
} | |
// the source is not translated it is the same string | |
if ($source == $target || empty($target)) { | |
$translations[$source][$lang] = 'not translated'; | |
} else { | |
unset($translations[$source][$lang]); | |
} | |
} | |
} | |
$counter = array(); | |
foreach($translations as $source => $translation) { | |
// skip if the source is translated in each language | |
if (empty($translation)) { | |
continue; | |
} | |
// all languages | |
if ($myLang === null) { | |
echo $source . PHP_EOL; | |
foreach($translation as $lang => $target) { | |
echo sprintf(' %s: %s', $lang, $target) . PHP_EOL; | |
$counter[$lang]++; | |
} | |
} else { | |
// source is translated for the specifi language | |
if (!isset($translation[$myLang])) { | |
continue; | |
} | |
echo $source . PHP_EOL; | |
echo sprintf(' %s: %s', $myLang, $translation[$myLang]) . PHP_EOL; | |
$counter[$myLang]++; | |
} | |
echo PHP_EOL; | |
} | |
// ouput language stats | |
echo 'Missing: ' . PHP_EOL; | |
foreach($counter as $lang => $number) { | |
echo sprintf('%s: %u', $lang, $number) . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment