5 * Compares translation files to find missing and redundant content.
8 $args = array_slice($argv, 1);
10 if (count($args) === 0) {
11 errorOut("Please provide a language code as the first argument (./check.php fr)");
15 // Get content from files
16 $lang = formatLang($args[0]);
17 $enContent = loadLang('en');
18 $langContent = loadLang($lang);
20 if (count($langContent) === 0) {
21 errorOut("No language content found for '{$lang}'");
24 info("Checking '{$lang}' translation content against 'en'");
26 // Track missing lang strings
27 $missingLangStrings = [];
28 foreach ($enContent as $enKey => $enStr) {
29 if (strpos($enKey, 'settings.language_select.') === 0) {
30 unset($langContent[$enKey]);
33 if (!isset($langContent[$enKey])) {
34 $missingLangStrings[$enKey] = $enStr;
37 unset($langContent[$enKey]);
40 if (count($missingLangStrings) > 0) {
41 info("\n========================");
42 info("Missing language content");
43 info("========================");
44 outputFlatArray($missingLangStrings, $lang);
47 if (count($langContent) > 0) {
48 info("\n==========================");
49 info("Redundant language content");
50 info("==========================");
51 outputFlatArray($langContent, $lang);
54 function outputFlatArray($arr, $lang) {
56 foreach ($arr as $key => $val) {
57 $explodedKey = explode('.', $key);
58 $group = $explodedKey[0];
59 $path = implode('.', array_slice($explodedKey, 1));
60 if (!isset($grouped[$group])) $grouped[$group] = [];
61 $grouped[$group][$path] = $val;
63 foreach ($grouped as $filename => $arr) {
64 echo "\e[36m" . $lang . '/' . $filename . ".php\e[0m\n";
65 echo json_encode($arr, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE) . "\n";
69 function formatLang($lang) {
70 $langParts = explode('_', strtoupper($lang));
71 $langParts[0] = strtolower($langParts[0]);
72 return implode('_', $langParts);
75 function loadLang(string $lang) {
76 $dir = __DIR__ . "/{$lang}";
77 if (!file_exists($dir)) {
78 errorOut("Expected directory '{$dir}' does not exist");
80 $files = scandir($dir);
82 foreach ($files as $file) {
83 if (substr($file, -4) !== '.php') continue;
84 $fileData = include ($dir . '/' . $file);
85 $name = substr($file, 0, -4);
86 $data[$name] = $fileData;
88 return flattenArray($data);
91 function flattenArray(array $arr) {
93 foreach ($arr as $key => $arrItem) {
94 if (!is_array($arrItem)) {
95 $data[$key] = $arrItem;
99 $toUse = flattenArray($arrItem);
100 foreach ($toUse as $innerKey => $item) {
101 $data[$key . '.' . $innerKey] = $item;
107 function info($text) {
108 echo "\e[34m" . $text . "\e[0m\n";
111 function errorOut($text) {
112 echo "\e[31m" . $text . "\e[0m\n";