translation-checker.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. ];
  40. $valid = 0;
  41. $errors = [];
  42. $apps = new \DirectoryIterator(__DIR__ . '/../apps');
  43. foreach ($apps as $app) {
  44. if ($app->isDot() || in_array($app->getBasename(), $untranslatedApps, true)) {
  45. continue;
  46. }
  47. if (!file_exists($app->getPathname() . '/l10n')) {
  48. if (!str_contains($txConfig, '[o:nextcloud:p:nextcloud:r:' . $app->getBasename() . ']')) {
  49. $errors[] = $app->getBasename() . "\n" . ' App is not translation synced via transifex and also not marked as untranslated' . "\n";
  50. }
  51. continue;
  52. }
  53. $directories[] = $app->getPathname() . '/l10n';
  54. }
  55. foreach ($directories as $dir) {
  56. if (!file_exists($dir)) {
  57. continue;
  58. }
  59. $directory = new \DirectoryIterator($dir);
  60. foreach ($directory as $file) {
  61. if ($file->getExtension() !== 'json') {
  62. continue;
  63. }
  64. $content = file_get_contents($file->getPathname());
  65. $language = pathinfo($file->getFilename(), PATHINFO_FILENAME);
  66. if (!in_array($language, $rtlLanguages, true) && preg_match_all('/^(.+[' . implode('', $rtlCharacters) . '].+)$/mu', $content, $matches)) {
  67. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a RTL limited characters in the translations. Offending strings:' . "\n" . implode("\n", $matches[0]) . "\n";
  68. }
  69. $json = json_decode($content, true);
  70. $translations = json_encode($json['translations']);
  71. if (strpos($translations, '|') !== false) {
  72. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a | in the translations.' . "\n";
  73. }
  74. if (json_last_error() !== JSON_ERROR_NONE) {
  75. $errors[] = $file->getPathname() . "\n" . ' ' . json_last_error_msg() . "\n";
  76. } else {
  77. $valid++;
  78. }
  79. if ($isDebug && $file->getFilename() === 'en_GB.json') {
  80. $sourceStrings = json_encode(array_keys($json['translations']));
  81. if (strpos($sourceStrings, '\u2019') !== false) {
  82. $errors[] = $file->getPathname() . "\n"
  83. . ' ' . 'Contains a unicode single quote "’" in the english source string, please replace with normal single quotes.' . "\n"
  84. . ' ' . 'Please note that this only updates after a sync to transifex.' . "\n";
  85. }
  86. }
  87. }
  88. }
  89. if (count($errors) > 0) {
  90. echo "\033[0;31m" . sprintf('ERROR: There were %d errors:', count($errors)) . "\033[0m\n\n";
  91. echo implode("\n", $errors);
  92. exit(1);
  93. }
  94. echo "\033[0;32m" . 'OK: ' . $valid . ' files parse' . "\033[0m\n";
  95. exit(0);