translation-checker.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. $directories = [
  22. __DIR__ . '/../core/l10n',
  23. ];
  24. $isDebug = in_array('--debug', $argv, true) || in_array('-d', $argv, true);
  25. $apps = new \DirectoryIterator(__DIR__ . '/../apps');
  26. foreach ($apps as $app) {
  27. if (!file_exists($app->getPathname() . '/l10n')) {
  28. continue;
  29. }
  30. $directories[] = $app->getPathname() . '/l10n';
  31. }
  32. $errors = [];
  33. $valid = 0;
  34. foreach ($directories as $dir) {
  35. if (!file_exists($dir)) {
  36. continue;
  37. }
  38. $directory = new \DirectoryIterator($dir);
  39. foreach ($directory as $file) {
  40. if ($file->getExtension() !== 'json') {
  41. continue;
  42. }
  43. $content = file_get_contents($file->getPathname());
  44. $json = json_decode($content, true);
  45. $translations = json_encode($json['translations']);
  46. if (strpos($translations, '|') !== false) {
  47. $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a | in the translations.' . "\n";
  48. }
  49. if (json_last_error() !== JSON_ERROR_NONE) {
  50. $errors[] = $file->getPathname() . "\n" . ' ' . json_last_error_msg() . "\n";
  51. } else {
  52. $valid++;
  53. }
  54. if ($isDebug && $file->getFilename() === 'en_GB.json') {
  55. $sourceStrings = json_encode(array_keys($json['translations']));
  56. if (strpos($sourceStrings, '\u2019') !== false) {
  57. $errors[] = $file->getPathname() . "\n"
  58. . ' ' . 'Contains a unicode single quote "’" in the english source string, please replace with normal single quotes.' . "\n"
  59. . ' ' . 'Please note that this only updates after a sync to transifex.' . "\n";
  60. }
  61. }
  62. }
  63. }
  64. if (count($errors) > 0) {
  65. echo sprintf('ERROR: There were %d errors:', count($errors)) . "\n\n";
  66. echo implode("\n", $errors);
  67. exit(1);
  68. }
  69. echo 'OK: ' . $valid . ' files parse' . "\n";
  70. exit(0);