Skip to content

Commit a16e4be

Browse files
committed
validation feature
1 parent c65841e commit a16e4be

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ php artisan lang:import espaniol.csv -l es
7878
php artisan lang:import espaniol.csv -l es -g paggination,validation # import only cretain groups
7979
```
8080

81+
### Validate
82+
```bash
83+
php artisan lang:validate ar -m -v
84+
```
85+
![Laravel-Lang-Import-Export validation example](https://raw.githubusercontent.com/AidasK/laravel-lang-import-export/master/validation.png)
86+
8187

8288
### Config
8389

src/Console/ExportToCsvCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function getParameters()
107107
$parameters = array_filter($parameters, function ($var) {
108108
return !is_null($var);
109109
});
110-
$this->parameters = array_merge(config('lang_import_export.export'), $parameters);
110+
$this->parameters = array_merge(config('lang_import_export.export', []), $parameters);
111111
}
112112

113113
/**

src/Console/ImportFromCsvCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private function getParameters()
8080
$parameters = array_filter($parameters, function ($var) {
8181
return !is_null($var);
8282
});
83-
$this->parameters = array_merge(config('lang_import_export.import'), $parameters);
83+
$this->parameters = array_merge(config('lang_import_export.import', []), $parameters);
8484
}
8585

8686

src/Console/ValidationCommand.php

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/config/lang_import_export.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22
return [
33
'export' => [
4-
// * for all or comma separated
5-
'locale' => 'en',
4+
'locale' => 'en', // * for all or comma separated
65
'target' => null,
76
'group' => null,
87
'excel' => null,
@@ -17,5 +16,10 @@
1716
'enclosure' => null,
1817
'escape' => null,
1918
'excel' => null,
20-
]
19+
],
20+
'validate' => [
21+
'target' => null,
22+
'locale' => null,
23+
'group' => null,
24+
],
2125
];

validation.png

171 KB
Loading

0 commit comments

Comments
 (0)