check.translation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. $options = getopt("dhl:r");
  3. $ignore = include __DIR__ . '/translation.ignore.php';
  4. if (array_key_exists('h', $options)) {
  5. help();
  6. }
  7. if (array_key_exists('l', $options)) {
  8. $langPattern = sprintf('/%s/', $options['l']);
  9. } else {
  10. $langPattern = '/*/';
  11. }
  12. $displayErrors = array_key_exists('d', $options);
  13. $displayReport = array_key_exists('r', $options);
  14. $i18nPath = __DIR__ . '/../app/i18n/';
  15. $errors = array();
  16. $report = array();
  17. foreach (glob($i18nPath . 'en/*.php') as $i18nFileReference) {
  18. $en = flatten(include $i18nFileReference);
  19. foreach (glob(str_replace('/en/', $langPattern, $i18nFileReference)) as $i18nFile) {
  20. preg_match('#(?P<lang>[^/]+)/(?P<file>[^/]*.php)#', $i18nFile, $matches);
  21. $lang = $matches['lang'];
  22. $file = $matches['file'];
  23. if ('en' === $lang) {
  24. continue;
  25. }
  26. if (!array_key_exists($lang, $report)) {
  27. $report[$lang]['total'] = 0;
  28. $report[$lang]['errors'] = 0;
  29. }
  30. $i18n = flatten(include $i18nFile);
  31. foreach ($en as $key => $value) {
  32. $report[$lang]['total'] ++;
  33. if (array_key_exists($lang, $ignore) && array_key_exists($file, $ignore[$lang]) && in_array($key, $ignore[$lang][$file])) {
  34. continue;
  35. }
  36. if (!array_key_exists($key, $i18n)) {
  37. $errors[$lang][$file][] = sprintf('Missing key %s', $key);
  38. $report[$lang]['errors'] ++;
  39. continue;
  40. }
  41. if ($i18n[$key] === $value) {
  42. $errors[$lang][$file][] = sprintf('Untranslated key %s - %s', $key, $value);
  43. $report[$lang]['errors'] ++;
  44. continue;
  45. }
  46. }
  47. }
  48. }
  49. if ($displayErrors) {
  50. foreach ($errors as $lang => $value) {
  51. echo 'Language: ', $lang, PHP_EOL;
  52. foreach ($value as $file => $messages) {
  53. echo ' - File: ', $file, PHP_EOL;
  54. foreach ($messages as $message) {
  55. echo ' - ', $message, PHP_EOL;
  56. }
  57. }
  58. echo PHP_EOL;
  59. }
  60. }
  61. if ($displayReport) {
  62. foreach ($report as $lang => $value) {
  63. $completion = ($value['total'] - $value['errors']) / $value['total'] * 100;
  64. echo sprintf('Translation for %-5s is %5.1f%% complete.', $lang, $completion), PHP_EOL;
  65. }
  66. }
  67. if (!empty($errors)) {
  68. exit(1);
  69. }
  70. /**
  71. * Flatten an array of translation
  72. *
  73. * @param array $translation
  74. * @param string $prependKey
  75. * @return array
  76. */
  77. function flatten($translation, $prependKey = '') {
  78. $a = array();
  79. if ('' !== $prependKey) {
  80. $prependKey .= '.';
  81. }
  82. foreach ($translation as $key => $value) {
  83. if (is_array($value)) {
  84. $a += flatten($value, $prependKey . $key);
  85. } else {
  86. $a[$prependKey . $key] = $value;
  87. }
  88. }
  89. return $a;
  90. }
  91. /**
  92. * Output help message.
  93. */
  94. function help() {
  95. $help = <<<HELP
  96. NAME
  97. %s
  98. SYNOPSIS
  99. php %s [OPTION]...
  100. DESCRIPTION
  101. Check if translation files have missing keys or missing translations.
  102. -d display results.
  103. -h display this help and exit.
  104. -l=LANG filter by LANG.
  105. -r display completion report.
  106. HELP;
  107. $file = str_replace(__DIR__ . '/', '', __FILE__);
  108. echo sprintf($help, $file, $file);
  109. exit;
  110. }