blob: 1f5f377afc3e8527998aa16850b407f365ab7ed6 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?php
2
3function 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
23if(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
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010029$masterLang = file_get_contents(__DIR__.'/../data/web/lang/lang.en-gb.json');
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +010030$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
39if(empty($result)) {
40 die('No new keys were added. Looks like target lang is up to date.'."\n");
41}
42
43foreach($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);
60file_put_contents(__DIR__.'/../data/web/lang/lang.'.$targetLang.'.json', $lang);
61
62echo 'Following new lang keys were added and need translation:'."\n";
63print_r($result);