translation-checker.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $valid = 0;
  15. $errors = [];
  16. $apps = new \DirectoryIterator(__DIR__ . '/../apps');
  17. foreach ($apps as $app) {
  18. if ($app->isDot() || in_array($app->getBasename(), $untranslatedApps, true)) {
  19. continue;
  20. }
  21. if (!file_exists($app->getPathname() . '/l10n')) {
  22. if (!str_contains($txConfig, '[o:nextcloud:p:nextcloud:r:' . $app->getBasename() . ']')) {
  23. $errors[] = $app->getBasename() . "\n" . ' App is not translation synced via transifex and also not marked as untranslated' . "\n";
  24. }
  25. continue;
  26. }
  27. $directories[] = $app->getPathname() . '/l10n';
  28. }
  29. foreach ($directories as $dir) {
  30. if (!file_exists($dir)) {
  31. continue;
  32. }
  33. $directory = new \DirectoryIterator($dir);
  34. foreach ($directory as $file) {
  35. if ($file->getExtension() !== 'json') {
  36. continue;
  37. }
  38. $content = file_get_contents($file->getPathname());
  39. $json = json_decode($content, true);
  40. $translations = json_encode($json['translations']);
  41. if (strpos($translations, '|') !== false) {
  42. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a | in the translations.' . "\n";
  43. }
  44. if (json_last_error() !== JSON_ERROR_NONE) {
  45. $errors[] = $file->getPathname() . "\n" . ' ' . json_last_error_msg() . "\n";
  46. } else {
  47. $valid++;
  48. }
  49. if ($isDebug && $file->getFilename() === 'en_GB.json') {
  50. $sourceStrings = json_encode(array_keys($json['translations']));
  51. if (strpos($sourceStrings, '\u2019') !== false) {
  52. $errors[] = $file->getPathname() . "\n"
  53. . ' ' . 'Contains a unicode single quote "’" in the english source string, please replace with normal single quotes.' . "\n"
  54. . ' ' . 'Please note that this only updates after a sync to transifex.' . "\n";
  55. }
  56. }
  57. }
  58. }
  59. if (count($errors) > 0) {
  60. echo "\033[0;31m" . sprintf('ERROR: There were %d errors:', count($errors)) . "\033[0m\n\n";
  61. echo implode("\n", $errors);
  62. exit(1);
  63. }
  64. echo "\033[0;32m" . 'OK: ' . $valid . ' files parse' . "\033[0m\n";
  65. exit(0);