]> BookStack Code Mirror - bookstack/blob - resources/lang/check.php
Updated 'Spanish Argentina' translation.
[bookstack] / resources / lang / check.php
1 #!/usr/bin/env php
2 <?php
3
4 /**
5  * Compares translation files to find missing and redundant content.
6  */
7
8 $args = array_slice($argv, 1);
9
10 if (count($args) === 0) {
11     errorOut("Please provide a language code as the first argument (./check.php fr)");
12 }
13
14
15 // Get content from files
16 $lang = formatLang($args[0]);
17 $enContent = loadLang('en');
18 $langContent = loadLang($lang);
19
20 if (count($langContent) === 0) {
21     errorOut("No language content found for '{$lang}'");
22 }
23
24 info("Checking '{$lang}' translation content against 'en'");
25
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]);
31         continue;
32     }
33     if (!isset($langContent[$enKey])) {
34         $missingLangStrings[$enKey] = $enStr;
35         continue;
36     }
37     unset($langContent[$enKey]);
38 }
39
40 if (count($missingLangStrings) > 0) {
41     info("\n========================");
42     info("Missing language content");
43     info("========================");
44     outputFlatArray($missingLangStrings, $lang);
45 }
46
47 if (count($langContent) > 0) {
48     info("\n==========================");
49     info("Redundant language content");
50     info("==========================");
51     outputFlatArray($langContent, $lang);
52 }
53
54 function outputFlatArray($arr, $lang) {
55     $grouped = [];
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;
62     }
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";
66     }
67 }
68
69 function formatLang($lang) {
70     $langParts = explode('_', strtoupper($lang));
71     $langParts[0] = strtolower($langParts[0]);
72     return implode('_', $langParts);
73 }
74
75 function loadLang(string $lang) {
76     $dir = __DIR__ . "/{$lang}";
77     if (!file_exists($dir)) {
78        errorOut("Expected directory '{$dir}' does not exist");
79     }
80     $files = scandir($dir);
81     $data = [];
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;
87     }
88     return flattenArray($data);
89 }
90
91 function flattenArray(array $arr) {
92     $data = [];
93     foreach ($arr as $key => $arrItem) {
94         if (!is_array($arrItem)) {
95             $data[$key] = $arrItem;
96             continue;
97         }
98
99         $toUse = flattenArray($arrItem);
100         foreach ($toUse as $innerKey => $item) {
101             $data[$key . '.' . $innerKey] = $item;
102         }
103     }
104     return $data;
105 }
106
107 function info($text) {
108     echo "\e[34m" . $text . "\e[0m\n";
109 }
110
111 function errorOut($text) {
112     echo "\e[31m" . $text . "\e[0m\n";
113     exit(1);
114 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.