66
77class NovaLangReorder extends AbstractDevCommand
88{
9- protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. The updated file has been output to [%s]. ' ;
9+ protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. ' ;
10+ protected const RENAMED_KEYS = 'Additionally, %d keys were renamed for Nova 4.0. ' ;
11+ protected const OUTPUT_FILE = 'The updated file has been output to [%s]. ' ;
1012 protected const NO_KEYS_OUT_OF_ORDER = '"%s" locale has no translation keys out of order. ' ;
1113 protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing %2$s` to add them. ' ;
1214
@@ -43,15 +45,29 @@ public function handleLocale(string $locale)
4345 $ inputFile = $ this ->directoryFrom ("$ locale.json " );
4446 $ outputFile = $ inputFile ;
4547
46- $ localeTranslations = $ this ->loadJson ($ inputFile );
48+ $ novaRenamedKeys = $ this ->getRenamedNovaKeys ();
49+
50+ $ unsortedLocaleTranslations = $ this ->loadJson ($ inputFile );
51+ $ localeTranslations = [];
52+
53+ $ renamedKeys = 0 ;
54+
55+ foreach ($ unsortedLocaleTranslations as $ key => $ translation ) {
56+ if ($ newKey = $ novaRenamedKeys [$ key ] ?? false ) {
57+ $ localeTranslations [$ newKey ] = $ translation ;
58+ $ renamedKeys ++;
59+ } else {
60+ $ localeTranslations [$ key ] = $ translation ;
61+ }
62+ }
4763
4864 $ localeKeys = array_values (array_diff (array_keys ($ localeTranslations ), static ::IGNORED_KEYS ));
4965
5066 $ commonKeys = array_values (array_intersect ($ this ->sourceKeys , $ localeKeys ));
5167
5268 $ diffs = $ this ->array_diff_order ($ commonKeys , $ localeKeys );
5369
54- if ($ diffs > 0 ) {
70+ if ($ diffs > 0 || $ renamedKeys > 0 ) {
5571 $ missingKeys = 0 ;
5672
5773 $ outputKeys = [];
@@ -66,7 +82,14 @@ public function handleLocale(string $locale)
6682
6783 $ this ->saveJson ($ outputFile , $ outputKeys );
6884
69- $ this ->info (sprintf (static ::KEYS_OUT_OF_ORDER , $ diffs , $ locale , $ outputFile ));
85+ $ this ->info (sprintf (static ::KEYS_OUT_OF_ORDER , $ diffs , $ locale ));
86+
87+ if ($ renamedKeys > 0 ) {
88+ $ this ->info (sprintf (static ::RENAMED_KEYS , $ renamedKeys ));
89+ }
90+
91+ $ this ->info (sprintf (static ::OUTPUT_FILE , $ outputFile ));
92+
7093 } else {
7194 $ this ->info (sprintf (static ::NO_KEYS_OUT_OF_ORDER , $ locale ));
7295
0 commit comments