@@ -19,6 +19,7 @@ class ValidationCommand extends Command
1919 {target? : Locale to be checked.}
2020 {--l|locale= : The locales to be exported. Separated by comma (default - default lang of application).}
2121 {--g|group= : The name of translation file to export (default all groups).}
22+ {--m|missing : Show missing translations}
2223 ' ;
2324
2425 /**
@@ -46,20 +47,14 @@ public function handle()
4647 $ this ->getParameters ();
4748
4849 $ baseTranslations = LangListService::loadLangList ($ this ->parameters ['locale ' ], $ this ->parameters ['group ' ]);
50+ if (empty ($ this ->parameters ['target ' ])) {
51+ $ this ->error ('--target is required ' );
52+ }
4953 foreach ($ this ->strToArray ($ this ->parameters ['target ' ]) as $ locale ) {
5054 $ targetTranslations = LangListService::loadLangList ($ locale , $ this ->parameters ['group ' ]);
51- foreach ($ targetTranslations as $ group => $ translations ) {
52- foreach ($ translations as $ key => $ translation ) {
53- if (isset ($ baseTranslations [$ group ][$ key ])) {
54- $ placeholders = $ this ->matchPlaceholders ($ baseTranslations [$ group ][$ key ]);
55- foreach ($ placeholders as $ placeholder ) {
56- if (strpos ($ translation , $ placeholder ) === false ) {
57- $ this ->warning ("$ locale/ $ group. $ key is missing \"$ placeholder \". " );
58- $ this ->info ($ translation , 'v ' );
59- }
60- }
61- }
62- }
55+ $ this ->validatePlaceholders ($ targetTranslations , $ baseTranslations , $ locale );
56+ if ($ this ->parameters ['missing ' ]) {
57+ $ this ->showMissing ($ targetTranslations , $ baseTranslations , $ locale );
6358 }
6459 }
6560 }
@@ -81,18 +76,58 @@ private function getParameters()
8176 {
8277 $ parameters = [
8378 'target ' => $ this ->argument ('target ' ),
84- 'locale ' => $ this ->option ('locale ' ),
79+ 'locale ' => $ this ->option ('locale ' ) ?: \App:: getLocale () ,
8580 'group ' => $ this ->option ('group ' ),
81+ 'missing ' => $ this ->option ('missing ' ),
8682 ];
8783 $ parameters = array_filter ($ parameters , function ($ var ) {
8884 return !is_null ($ var );
8985 });
90- $ this ->parameters = array_merge (config ('lang_import_export.check ' ), $ parameters );
86+ $ this ->parameters = array_merge (config ('lang_import_export.validate ' , [] ), $ parameters );
9187 }
9288
9389 private function matchPlaceholders ($ translation )
9490 {
9591 preg_match_all ('~(:[a-zA-Z0-9_]+)~ ' , $ translation , $ m );
9692 return $ m [1 ] ?? [];
9793 }
94+
95+ /**
96+ * @param $targetTranslations
97+ * @param $baseTranslations
98+ * @param $locale
99+ */
100+ private function validatePlaceholders ($ targetTranslations , $ baseTranslations , $ locale ): void
101+ {
102+ $ this ->info ('Searching for missing placeholers... ' );
103+ foreach ($ targetTranslations as $ group => $ translations ) {
104+ foreach ($ translations as $ key => $ translation ) {
105+ if (isset ($ baseTranslations [$ group ][$ key ]) && is_string ($ baseTranslations [$ group ][$ key ])) {
106+ $ placeholders = $ this ->matchPlaceholders ($ baseTranslations [$ group ][$ key ]);
107+ foreach ($ placeholders as $ placeholder ) {
108+ if (strpos ($ translation , $ placeholder ) === false ) {
109+ $ this ->warn ("$ locale/ $ group. $ key is missing \"$ placeholder \". " );
110+ $ this ->info ($ translation , 'v ' );
111+ }
112+ }
113+ }
114+ }
115+ }
116+ }
117+
118+ private function showMissing ($ targetTranslations , $ baseTranslations , $ locale )
119+ {
120+ $ this ->info ('Searching for missing keys... ' );
121+ foreach ($ baseTranslations as $ group => $ translations ) {
122+ if (!isset ($ targetTranslations [$ group ])) {
123+ $ this ->warn ("$ locale/ $ group entire group is missing " );
124+ continue ;
125+ }
126+ foreach ($ translations as $ key => $ translation ) {
127+ if (!isset ($ targetTranslations [$ group ][$ key ])) {
128+ $ this ->warn ("$ locale/ $ group. $ key is missing " );
129+ }
130+ }
131+ }
132+ }
98133}
0 commit comments