Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | function array_diff_key_recursive (array $arr1, array $arr2) { |
| 4 | $diff = array_diff_key($arr1, $arr2); |
| 5 | $intersect = array_intersect_key($arr1, $arr2); |
| 6 | |
| 7 | foreach ($intersect as $k => $v) { |
| 8 | if (is_array($arr1[$k]) && is_array($arr2[$k])) { |
| 9 | $d = array_diff_key_recursive($arr1[$k], $arr2[$k]); |
| 10 | |
| 11 | if ($d) { |
| 12 | $diff[$k] = $d; |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | return $diff; |
| 18 | } |
| 19 | |
| 20 | // target lang |
| 21 | $targetLang = $argv[1]; |
| 22 | |
| 23 | if(empty($targetLang)) { |
| 24 | die('Please specify target lang as the first argument, to which you want to add missing keys from master lang (EN). Use the lowercase name, |
| 25 | for example `sk` for the Slovak language'."\n"); |
| 26 | } |
| 27 | |
| 28 | // load master lang |
| 29 | $masterLang = file_get_contents(__DIR__.'/../data/web/lang/lang.en.json'); |
| 30 | $masterLang = json_decode($masterLang, true); |
| 31 | |
| 32 | // load target lang |
| 33 | $lang = file_get_contents(__DIR__.'/../data/web/lang/lang.'.$targetLang.'.json'); |
| 34 | $lang = json_decode($lang, true); |
| 35 | |
| 36 | // compare lang keys |
| 37 | $result = array_diff_key_recursive($masterLang, $lang); |
| 38 | |
| 39 | if(empty($result)) { |
| 40 | die('No new keys were added. Looks like target lang is up to date.'."\n"); |
| 41 | } |
| 42 | |
| 43 | foreach($result as $key => $val) { |
| 44 | // check if section key exists in target lang |
| 45 | if(array_key_exists($key, $lang)) { |
| 46 | // add only missing section keys |
| 47 | foreach ($val as $k => $v) { |
| 48 | $lang[$key][$k] = $v; |
| 49 | } |
| 50 | // sort keys |
| 51 | ksort($lang[$key]); |
| 52 | } else { |
| 53 | // add whole section |
| 54 | $lang[$key] = $val; |
| 55 | ksort($lang); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $lang = json_encode($lang, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); |
| 60 | file_put_contents(__DIR__.'/../data/web/lang/lang.'.$targetLang.'.json', $lang); |
| 61 | |
| 62 | echo 'Following new lang keys were added and need translation:'."\n"; |
| 63 | print_r($result); |