Skip to content

Commit 67c46ca

Browse files
committed
feat: exclude group
1 parent 14ad72e commit 67c46ca

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,22 @@ The package currently provides two commands, one for exporting the files and one
6868
php artisan lang:export --locale en
6969
php artisan lang:export --locale en --target fr,de,pt # export en translations only missing in fr,de,pt locales. Each in separate files
7070
php artisan lang:export -l fr,de,pt -z all.zip # archive all the files
71-
php artisan lang:export --locale en -g paggination,validation # export only cretain groups
71+
php artisan lang:export --locale en -g pagination,validation # export only cretain groups
72+
php artisan lang:export --locale en --exclude pagination,validation # export all files except pagination and validation
7273
```
7374

7475
### Import
7576
```bash
7677
php artisan lang:import es.csv # localed autodetected from file name
7778
php artisan lang:import espaniol.csv -l es
78-
php artisan lang:import espaniol.csv -l es -g paggination,validation # import only cretain groups
79-
php artisan lang:import es.csv -p # validate imported translations for missing placeholders (see below)
79+
php artisan lang:import espaniol.csv -l es -g pagination,validation # import only cretain groups
80+
php artisan lang:import es.csv -p --html # validate imported translations for missing placeholders and bad html (see below)
81+
php artisan lang:import es.xls -p --column-map A,B,D # import translations from different column. E.g. C column was left with base language
8082
```
8183

8284
### Validate
8385
```bash
84-
php artisan lang:validate ar -m --html -v
86+
php artisan lang:validate ar -m --html -v # find missing keys, bad html and placeholders
8587
```
8688
![Laravel-Lang-Import-Export validation example](https://raw.githubusercontent.com/AidasK/laravel-lang-import-export/master/validation.png)
8789

src/Console/ExportToCsvCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class ExportToCsvCommand extends Command
1818
protected $signature = 'lang:export
1919
{--l|locale= : The locales to be exported. Separated by comma (default - base locale from config).}
2020
{--t|target= : Target languages, only missing keys are exported. Separated by comma.}
21-
{--g|group= : The name of translation file to export (default - base group from config).}
21+
{--g|group= : The names of translation files to export (default - group from config).}
22+
{--exclude= : The names of translation files to exclude (default - group from config).}
2223
{--o|output= : Filename of exported translation, :locale, :target is replaced (default - export_path from config).}
2324
{--z|zip= : Zip all files.}
2425
{--X|excel : Set file encoding for Excel (optional, default - UTF-8).}
@@ -89,10 +90,11 @@ private function strToArray($string, $fallback = [])
8990
*/
9091
private function getTranslations($locale, $target = null)
9192
{
92-
$group = $this->option('group') ?: config('lang_import_export.base_group');
93-
$from = LangListService::loadLangList($locale, $group);
93+
$group = $this->option('group') ?: config('lang_import_export.groups');
94+
$exclude = $this->option('exclude') ?: config('lang_import_export.exclude_groups');
95+
$from = LangListService::loadLangList($locale, $group, $exclude);
9496
if ($target) {
95-
$targetList = LangListService::loadLangList($target, $group);
97+
$targetList = LangListService::loadLangList($target, $group, $exclude);
9698
foreach ($targetList as $group => $translations) {
9799
foreach ($translations as $key => $v) {
98100
unset($from[$group][$key]);

src/Console/ValidationCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class ValidationCommand extends Command
1818
protected $signature = 'lang:validate
1919
{target? : Locale to be checked.}
2020
{--l|locale= : Base locale (default - base locale from config).}
21-
{--g|group= : The name of translation file to export (default - base group from config).}
21+
{--g|group= : The names of translation files to validate (default - group from config).}
22+
{--exclude= : The names of translation files to be excluded (default - group from config).}
2223
{--html : Compare HTML}
2324
{--m|missing : Show missing translations}
2425
';
@@ -39,14 +40,15 @@ class ValidationCommand extends Command
3940
public function handle()
4041
{
4142
$baseLocale = $this->option('locale') ?: config('lang_import_export.base_locale');
42-
$groups = $this->option('group') ?: config('lang_import_export.base_group');
43-
$baseTranslations = LangListService::loadLangList($baseLocale, $groups);
43+
$groups = $this->option('group') ?: config('lang_import_export.groups');
44+
$exclude = $this->option('exclude') ?: config('lang_import_export.exclude_groups');
45+
$baseTranslations = LangListService::loadLangList($baseLocale, $groups, $exclude);
4446
$target = $this->argument('target');
4547
if (empty($target)) {
4648
$this->error('--target is required');
4749
}
4850
foreach ($this->strToArray($target) as $locale) {
49-
$targetTranslations = LangListService::loadLangList($locale, $groups);
51+
$targetTranslations = LangListService::loadLangList($locale, $groups, $exclude);
5052
$this->validatePlaceholders($targetTranslations, $baseTranslations, $locale);
5153
if ($this->option('html')) {
5254
$this->validateHTML($targetTranslations, $baseTranslations, $locale);

src/LangListService.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ class LangListService
1515
*
1616
* @param string $locale
1717
* @param string $group
18+
* @param string $excludeGroups
1819
* @return array
1920
*/
20-
public function loadLangList($locale, $group)
21+
public function loadLangList($locale, $group, $excludeGroups = '')
2122
{
23+
$exclude = explode(',', $excludeGroups);
2224
$result = [];
2325
if ($this->isGroupList($group)) {
2426
$groups = explode(',', $group);
25-
foreach ($groups as $group) {
26-
$result[$group] = $this->getGroup($locale, $group);
27+
} else {
28+
$path = resource_path('lang/' . $locale . '/');
29+
$files = $this->getAllFiles($path);
30+
$groups = [];
31+
foreach ($files as $file) {
32+
$groups[] = substr($file->getRealPath(), strlen($path), -4);
2733
}
28-
return $result;
2934
}
30-
31-
$path = resource_path('lang/' . $locale . '/');
32-
$files = $this->getAllFiles($path);
33-
foreach ($files as $file) {
34-
$file_path = substr($file->getRealPath(), strlen($path), -4);
35-
$result[$file_path] = $this->getGroup($locale, $file_path);
35+
foreach ($groups as $group) {
36+
if (!in_array($group, $exclude)) {
37+
$result[$group] = $this->getGroup($locale, $group);
38+
}
3639
}
3740
return $result;
3841
}

src/config/lang_import_export.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* In practice export_locale value is 'en' and export_target is a list of all locales supported by your website
55
*/
66
'base_locale' => 'en',
7+
'groups' => null, // all groups or comma separated list (e.g. translation file names)
8+
'exclude_groups' => null, // comma separated list
79
'export_locale' => 'en',// locale list separated by comma
810
'export_target' => null,// target locale list separated by comma
9-
'base_group' => null, // all groups or comma separated list
1011
'export_path' => storage_path(':locale:target.csv'),
1112
'import_validate_placeholders' => false, // validate placeholders by default
1213
'import_validate_html' => false, // validate html by default

0 commit comments

Comments
 (0)