add-new-lang-keys.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. function array_diff_key_recursive (array $arr1, array $arr2) {
  3. $diff = array_diff_key($arr1, $arr2);
  4. $intersect = array_intersect_key($arr1, $arr2);
  5. foreach ($intersect as $k => $v) {
  6. if (is_array($arr1[$k]) && is_array($arr2[$k])) {
  7. $d = array_diff_key_recursive($arr1[$k], $arr2[$k]);
  8. if ($d) {
  9. $diff[$k] = $d;
  10. }
  11. }
  12. }
  13. return $diff;
  14. }
  15. // target lang
  16. $targetLang = $argv[1];
  17. if(empty($targetLang)) {
  18. 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,
  19. for example `sk` for the Slovak language'."\n");
  20. }
  21. // load master lang
  22. $masterLang = file_get_contents(__DIR__.'/../data/web/lang/lang.en-gb.json');
  23. $masterLang = json_decode($masterLang, true);
  24. // load target lang
  25. $lang = file_get_contents(__DIR__.'/../data/web/lang/lang.'.$targetLang.'.json');
  26. $lang = json_decode($lang, true);
  27. // compare lang keys
  28. $result = array_diff_key_recursive($masterLang, $lang);
  29. if(empty($result)) {
  30. die('No new keys were added. Looks like target lang is up to date.'."\n");
  31. }
  32. foreach($result as $key => $val) {
  33. // check if section key exists in target lang
  34. if(array_key_exists($key, $lang)) {
  35. // add only missing section keys
  36. foreach ($val as $k => $v) {
  37. $lang[$key][$k] = $v;
  38. }
  39. // sort keys
  40. ksort($lang[$key]);
  41. } else {
  42. // add whole section
  43. $lang[$key] = $val;
  44. ksort($lang);
  45. }
  46. }
  47. $lang = json_encode($lang, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  48. file_put_contents(__DIR__.'/../data/web/lang/lang.'.$targetLang.'.json', $lang);
  49. echo 'Following new lang keys were added and need translation:'."\n";
  50. print_r($result);