|
| 1 | +<?php |
| 2 | + |
| 3 | +// Adapted from: https://github.com/barryvdh/laravel-translation-manager/blob/master/src/Manager.php |
| 4 | +$translationKeys = []; |
| 5 | +$functions = ['@lang', '__']; |
| 6 | +$stringPattern = |
| 7 | + "[^\w]". // Must not have an alphanum before real method |
| 8 | + '('.implode('|', $functions).')'. // Must start with one of the functions |
| 9 | + "\(\s*". // Match opening parenthesis |
| 10 | + "(?P<quote>['\"])". // Match " or ' and store in {quote} |
| 11 | + "(?P<string>(?:\\\k{quote}|(?!\k{quote}).)*)". // Match any string that can be {quote} escaped |
| 12 | + "\k{quote}". // Match " or ' previously matched |
| 13 | + "\s*[\),]"; // Close parentheses or new parameter |
| 14 | + |
| 15 | +$labelPattern = |
| 16 | + "label=" . // Match `label=` |
| 17 | + "(?P<quote>['\"])". // Match " or ' and store in {quote} |
| 18 | + "(?P<string>(?:\\\k{quote}|(?!\k{quote}).)*)". // Match any string that can be {quote} escaped |
| 19 | + "\k{quote}"; // Match " or ' previously matched |
| 20 | + |
| 21 | +$files = []; |
| 22 | +$iterator = new RecursiveDirectoryIterator('resources'); |
| 23 | +foreach(new RecursiveIteratorIterator($iterator) as $file) { |
| 24 | + if(strpos($file , '.blade.php') !== false){ |
| 25 | + $files[] = $file->getRealPath(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +foreach ($files as $file) { |
| 30 | + $contents = file_get_contents($file); |
| 31 | + if (preg_match_all("/$stringPattern/siU", $contents, $matches)) { |
| 32 | + foreach ($matches['string'] as $key) { |
| 33 | + $translationKeys[] = $key; |
| 34 | + } |
| 35 | + } |
| 36 | + if (preg_match_all("/$labelPattern/siU", $contents, $matches)) { |
| 37 | + foreach ($matches['string'] as $key) { |
| 38 | + if (str_starts_with($key, '@lang(') || str_starts_with($key, '__(') || $key == 'false' || !$key) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + $translationKeys[] = $key; |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +$translationKeys = array_unique($translationKeys); |
| 47 | +$translations = (array)json_decode(file_get_contents('lang/nl.json')); |
| 48 | + |
| 49 | +$missing = 0; |
| 50 | +foreach($translationKeys as $key) { |
| 51 | + if (! array_key_exists($key, $translations)) { |
| 52 | + $missing++; |
| 53 | + error_log("missing translation: \"$key\""); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if ($missing > 0) { |
| 58 | + if ($missing == 1) { |
| 59 | + throw new Exception('1 translation is missing.'); |
| 60 | + } else { |
| 61 | + throw new Exception($missing . ' translations are missing.'); |
| 62 | + } |
| 63 | +} |
0 commit comments