translation-checker.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. $directories = [
  7. __DIR__ . '/../core/l10n',
  8. ];
  9. $isDebug = in_array('--debug', $argv, true) || in_array('-d', $argv, true);
  10. $txConfig = file_get_contents(__DIR__ . '/../.tx/config');
  11. $untranslatedApps = [
  12. 'testing',
  13. ];
  14. // Next line only looks messed up, but it works. Don't touch it!
  15. $rtlCharacters = [
  16. '\x{061C}', // ARABIC LETTER MARK
  17. '\x{0623}', // ARABIC LETTER ALEF WITH HAMZA ABOVE
  18. '\x{200E}', // LEFT-TO-RIGHT MARK
  19. '\x{200F}', // RIGHT-TO-LEFT MARK
  20. '\x{202A}', // LEFT-TO-RIGHT EMBEDDING
  21. '\x{202B}', // RIGHT-TO-LEFT EMBEDDING
  22. '\x{202C}', // POP DIRECTIONAL FORMATTING
  23. '\x{202D}', // LEFT-TO-RIGHT OVERRIDE
  24. '\x{202E}', // RIGHT-TO-LEFT OVERRIDE
  25. '\x{2066}', // LEFT-TO-RIGHT ISOLATE
  26. '\x{2067}', // RIGHT-TO-LEFT ISOLATE
  27. '\x{2068}', // FIRST STRONG ISOLATE
  28. '\x{2069}', // POP DIRECTIONAL ISOLATE
  29. '\x{206C}', // INHIBIT ARABIC FORM SHAPING
  30. '\x{206D}', // ACTIVATE ARABIC FORM SHAPING
  31. ];
  32. $rtlLanguages = [
  33. 'ar', // Arabic
  34. 'fa', // Persian
  35. 'he', // Hebrew
  36. 'ps', // Pashto,
  37. 'ug', // 'Uyghurche / Uyghur
  38. 'ur_PK', // Urdu
  39. 'uz', // Uzbek Afghan
  40. ];
  41. $valid = 0;
  42. $errors = [];
  43. $apps = new \DirectoryIterator(__DIR__ . '/../apps');
  44. foreach ($apps as $app) {
  45. if ($app->isDot() || in_array($app->getBasename(), $untranslatedApps, true)) {
  46. continue;
  47. }
  48. if (!file_exists($app->getPathname() . '/l10n')) {
  49. if (!str_contains($txConfig, '[o:nextcloud:p:nextcloud:r:' . $app->getBasename() . ']')) {
  50. $errors[] = $app->getBasename() . "\n" . ' App is not translation synced via transifex and also not marked as untranslated' . "\n";
  51. }
  52. continue;
  53. }
  54. $directories[] = $app->getPathname() . '/l10n';
  55. }
  56. foreach ($directories as $dir) {
  57. if (!file_exists($dir)) {
  58. continue;
  59. }
  60. $directory = new \DirectoryIterator($dir);
  61. foreach ($directory as $file) {
  62. if ($file->getExtension() !== 'json') {
  63. continue;
  64. }
  65. $content = file_get_contents($file->getPathname());
  66. $language = pathinfo($file->getFilename(), PATHINFO_FILENAME);
  67. if (!in_array($language, $rtlLanguages, true) && preg_match_all('/^(.+[' . implode('', $rtlCharacters) . '].+)$/mu', $content, $matches)) {
  68. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a RTL limited characters in the translations. Offending strings:' . "\n" . implode("\n", $matches[0]) . "\n";
  69. }
  70. $json = json_decode($content, true);
  71. $translations = json_encode($json['translations']);
  72. if (strpos($translations, '|') !== false) {
  73. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a | in the translations.' . "\n";
  74. }
  75. if (json_last_error() !== JSON_ERROR_NONE) {
  76. $errors[] = $file->getPathname() . "\n" . ' ' . json_last_error_msg() . "\n";
  77. } else {
  78. $valid++;
  79. }
  80. if ($isDebug && $file->getFilename() === 'en_GB.json') {
  81. $sourceStrings = json_encode(array_keys($json['translations']));
  82. if (strpos($sourceStrings, '\u2019') !== false) {
  83. $errors[] = $file->getPathname() . "\n"
  84. . ' ' . 'Contains a unicode single quote "’" in the english source string, please replace with normal single quotes.' . "\n"
  85. . ' ' . 'Please note that this only updates after a sync to transifex.' . "\n";
  86. }
  87. }
  88. }
  89. }
  90. if (count($errors) > 0) {
  91. echo "\033[0;31m" . sprintf('ERROR: There were %d errors:', count($errors)) . "\033[0m\n\n";
  92. echo implode("\n", $errors);
  93. exit(1);
  94. }
  95. echo "\033[0;32m" . 'OK: ' . $valid . ' files parse' . "\033[0m\n";
  96. exit(0);