Skip to content

Commit 5033b92

Browse files
committed
import command refactor
1 parent 2ae5cc4 commit 5033b92

File tree

6 files changed

+224
-52
lines changed

6 files changed

+224
-52
lines changed

README.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![Laravel-Lang-Import-Export by HighSolutions](https://github.com/highsolutions/laravel-lang-import-export/master/intro.jpg)
2+
13
Laravel-Lang-Import-Export
24
==========================
35

@@ -69,32 +71,65 @@ The package currently provides two commands, one for exporting the files and one
6971

7072
```bash
7173
php artisan lang:export
72-
php artisan lang:export en * --output=export
73-
php artisan lang:export en auth --A --X
74+
php artisan lang:export en * path/to/export
75+
php artisan lang:export en auth -A -X
7476
```
7577

76-
When you call command without parameters, export file will be generated for all localization files within default locale. But you can define **locale** explicitly. You can also export only one file (second parameter - **group**).
78+
When you call command without parameters, export file will be generated for all localization files within default locale. But you can define **locale** explicitly. You can also export only one file (second parameter - **group**) and define where to store file (you can provide name with and without .csv extension). When you use **output** argument, default path is base_path() -> catalog of your whole project.
7779
But there is few more useful parameters:
7880

79-
| name of parameter | description | is required? | default value |
80-
|-------------------|------------------------------------------|--------------|------------------------------------|
81-
| locale | The locale to be exported | NO | default lang of application |
82-
| group | The name of translation file to export | NO | \* - all files |
83-
| --O / --output | Filename of exported translation files | NO | storage/app/lang-import-export.csv |
84-
| --A / --append | Append name of group to the name of file | NO | empty |
85-
| --X / --excel | Set file encoding for Excel | NO | UTF-8 |
86-
| --D / --delimiter | Field delimiter | NO | , |
87-
| --E / --enclosure | Field enclosure | NO | " |
81+
| name of parameter | description | is required? | default value |
82+
|-------------------|-----------------------------------------|--------------|------------------------------------|
83+
| locale | The locale to be exported | NO | default lang of application |
84+
| group | The name of translation file to export | NO | \* - all files |
85+
| output | Filename of exported translation files | NO | storage/app/lang-import-export.csv |
86+
| -A / --append | Append name of group to the name of file | NO | empty |
87+
| -X / --excel | Set file encoding (UTF-16) for Excel | NO | UTF-8 |
88+
| -D / --delimiter | Field delimiter | NO | , |
89+
| -E / --enclosure | Field enclosure | NO | " |
8890

8991
### Import
9092

9193
```
92-
php artisan lang-import:csv en auth /path/to/file
93-
php artisan lang-import:csv --delimiter=";" --enclosure='"' --escape='\\' en_US auth /some/file
94+
php artisan lang:import
95+
php artisan lang:import en * path/to/import
96+
php artisan lang:import en auth -X
9497
```
9598

96-
You have to pass the __locale__, the __group__ and the __path to the CSV file__ as arguments. The group is the name of the langauge file without its extension. When you exported all files, write *. You may define options to match the CSV format of your input file.
99+
When you call command without parameters - it will try to read default file of export command without parameters for default locale and all localization files. You can of course specify all parameters (**locale**, **group**, **input**) and there is few more options:
100+
101+
| name of parameter | description | is required? | default value |
102+
|-------------------|----------------------------------------------|--------------|------------------------------------|
103+
| locale | The locale to be imported | NO | default lang of application |
104+
| group | The name of translation file to import | NO | * - all files |
105+
| output | Filename of translation files to be imported | NO | storage/app/lang-import-export.csv |
106+
| -X / --excel | Set file encoding from Excel | NO | UTF-8 |
107+
| -D / --delimiter | Field delimiter | NO | , |
108+
| -E / --enclosure | Field enclosure | NO | " |
109+
| -C / --escape | Field escape | NO | \ |
110+
111+
Changelog
112+
------------
113+
5.4.1
114+
- improved Excel support
115+
- support of [LaravelLocalization](https://github.com/mcamara/laravel-localization) routes files
116+
117+
5.4.0
118+
- refactor whole repository
119+
- add support for Excel
120+
- add support for export and import all localization files
121+
- any arguments are not required
97122

98-
### Credits
123+
Roadmap
124+
------------
125+
126+
* Option for deleting export file after importing.
127+
* Option for excluding certain files (and system ones).
128+
* Unit tests!
129+
130+
Credits
131+
------------
99132

100133
This package was originally created by [UFirst](http://github.com/ufirstgroup) and is available here: [Laravel-lang-import-export](https://github.com/ufirstgroup/laravel-lang-import-export).
134+
135+
Currently is developed by [HighSolutions](http://highsolutions.pl), software house from Poland in love in Laravel.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"license": "MIT",
1616
"require": {
17-
"php": ">=5.4.0",
17+
"php": ">=5.6.4",
1818
"illuminate/support": "~5.1"
1919
},
2020
"autoload": {

intro.jpg

103 KB
Loading

src/HighSolutions/LangImportExport/Console/ExportToCsvCommand.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ExportToCsvCommand extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'lang:export
19-
{locale : The locale to be exported (default - default lang of application).}
20-
{group : The name of translation file to export (default - all files).}
21-
{--O|output= : Filename of file to store exported translation files (optional, default - storage/app/lang-import-export.csv).}
19+
{locale? : The locale to be exported (default - default lang of application).}
20+
{group? : The name of translation file to export (default - all files).}
21+
{output? : Filename of exported translation files (optional, default - storage/app/lang-import-export.csv).}
2222
{--A|append : Append name of group to the name of file (optional, default - empty).}
2323
{--X|excel : Set file encoding for Excel (optional, default - UTF-8).}
2424
{--D|delimiter=, : Field delimiter (optional, default - ",").}
@@ -29,7 +29,7 @@ class ExportToCsvCommand extends Command
2929
*
3030
* @var string
3131
*/
32-
protected $description = "Exports the language files to CSV file.";
32+
protected $description = "Exports the language files to CSV file";
3333

3434
/**
3535
* Parameters provided to command.
@@ -60,7 +60,7 @@ class ExportToCsvCommand extends Command
6060
public function __construct()
6161
{
6262
parent::__construct();
63-
$this->defaultPath = storage_path('app'. DIRECTORY_SEPARATOR .'lang-import-export');
63+
$this->defaultPath = storage_path('app'. DIRECTORY_SEPARATOR .'lang-import-export') . $this->ext;
6464
}
6565

6666
/**
@@ -90,8 +90,8 @@ private function getParameters()
9090
{
9191
$this->parameters = [
9292
'group' => $this->argument('group'),
93-
'locale' => $this->argument('locale') === false ? config('app.locale') : $this->argument('locale'),
94-
'output' => $this->option('output') === false ? $this->defaultPath : base_path($this->option('output')),
93+
'locale' => $this->argument('locale') === null ? config('app.locale') : $this->argument('locale'),
94+
'output' => $this->argument('output') === null ? $this->defaultPath : base_path($this->argument('output')),
9595
'append' => $this->option('append') !== false,
9696
'excel' => $this->option('excel') !== false,
9797
'delimiter' => $this->option('delimiter'),
@@ -122,7 +122,7 @@ private function setDefaultPath()
122122
private function sayItsBeginning()
123123
{
124124
$this->info(PHP_EOL
125-
. 'Translations export of '. ($this->parameters['group'] === false ? 'all groups' : $this->parameters['group'] .' group') .' has started.');
125+
. 'Translations export of '. ($this->parameters['group'] === null ? 'all groups' : $this->parameters['group'] .' group') .' - started.');
126126
}
127127

128128
/**
@@ -156,7 +156,10 @@ private function saveTranslations($translations)
156156
*/
157157
private function openFile()
158158
{
159-
if (!($output = fopen($this->parameters['output'] . $this->ext, 'w'))) {
159+
if(substr($this->parameters['output'], -4) != $this->ext)
160+
$this->parameters['output'] .= $this->ext;
161+
162+
if (!($output = fopen($this->parameters['output'], 'w'))) {
160163
$output = fopen($this->defaultPath . $this->ext, 'w');
161164
}
162165
return $output;
@@ -217,8 +220,8 @@ private function closeFile($output)
217220
*/
218221
private function adjustToExcel()
219222
{
220-
$data = file_get_contents($this->parameters['output'] . $this->ext);
221-
file_put_contents($this->parameters['output'] . $this->ext, chr(255) . chr(254) . mb_convert_encoding($data, 'UTF-16LE', 'UTF-8'));
223+
$data = file_get_contents($this->parameters['output']);
224+
file_put_contents($this->parameters['output'], chr(255) . chr(254) . mb_convert_encoding($data, 'UTF-16LE', 'UTF-8'));
222225
}
223226

224227
/**
@@ -228,7 +231,7 @@ private function adjustToExcel()
228231
*/
229232
private function sayItsFinish()
230233
{
231-
$this->info('Finished! Translations saved to: '. (substr($this->parameters['output'], strlen(base_path()) + 1)) . $this->ext
234+
$this->info('Finished! Translations saved to: '. (substr($this->parameters['output'], strlen(base_path()) + 1))
232235
. PHP_EOL);
233236
}
234237

src/HighSolutions/LangImportExport/Console/ImportFromCsvCommand.php

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class ImportFromCsvCommand extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'lang:import
19-
{locale : The locale to be imported (default - default lang of application).}
20-
{group : The name of translation file to imported (default - all files).}
21-
{--I|input= : Filename of file to be imported with translation files(optional, default - storage/app/lang-import-export.csv).}
19+
{locale? : The locale to be imported (default - default lang of application).}
20+
{group? : The name of translation file to imported (default - all files).}
21+
{input? : Filename of file to be imported with translation files(optional, default - storage/app/lang-import-export.csv).}
2222
{--D|delimiter=, : Field delimiter (optional, default - ",").}
2323
{--E|enclosure=" : Field enclosure (optional, default - \'"\').}
24-
{--C|escape=\\" : Field excape (optional, default - \'\\\').}';
24+
{--C|escape=" : Field escape (optional, default - \'"\').}
25+
{--X|excel : Set file encoding from Excel (optional, default - UTF-8).}';
2526

2627
/**
2728
* The console command description.
@@ -88,13 +89,17 @@ public function handle()
8889
private function getParameters()
8990
{
9091
$this->parameters = [
92+
'locale' => $this->argument('locale') === null ? config('app.locale') : $this->argument('locale'),
9193
'group' => $this->argument('group'),
92-
'locale' => $this->argument('locale') === false ? config('app.locale') : $this->argument('locale'),
93-
'input' => $this->option('input') === false ? $this->defaultPath : base_path($this->option('input')),
94+
'input' => $this->argument('input') === null ? $this->defaultPath : base_path($this->argument('input')),
9495
'delimiter' => $this->option('delimiter'),
9596
'enclosure' => $this->option('enclosure'),
9697
'escape' => $this->option('escape'),
98+
'excel' => $this->option('excel') !== false,
9799
];
100+
101+
if(substr($this->parameters['input'], -4) != $this->ext)
102+
$this->parameters['input'] .= $this->ext;
98103
}
99104

100105
/**
@@ -115,34 +120,84 @@ private function sayItsBeginning()
115120
*/
116121
private function getTranslations()
117122
{
118-
$translations = [];
123+
$input = $this->openFile();
124+
125+
$translations = $this->readFile($input);
119126

120-
// Create output device and write CSV.
121-
if (($input_fp = fopen($this->parameters['input'], 'r')) === false) {
127+
$this->closeFile($input);
128+
129+
return $translations;
130+
}
131+
132+
/**
133+
* Opens file to read content.
134+
*
135+
* @return FileInputPointer
136+
*/
137+
private function openFile()
138+
{
139+
if (($input = fopen($this->parameters['input'], 'r')) === false) {
122140
$this->error('Can\'t open the input file!');
123141
}
124142

125-
// Write CSV lintes
126-
while (($data = fgetcsv($input_fp, 0, $this->parameters['delimiter'], $this->parameters['enclosure'], $this->parameters['escape'])) !== false) {
143+
return $input;
144+
}
145+
146+
/**
147+
* Read content of file.
148+
*
149+
* @param FilePointer $input
150+
* @throws \Exception
151+
* @return array
152+
*/
153+
private function readFile($input)
154+
{
155+
if($this->parameters['excel'])
156+
$this->adjustFromExcel();
157+
158+
$translations = [];
159+
while (($data = fgetcsv($input, 0, $this->parameters['delimiter'], $this->parameters['enclosure'], $this->parameters['escape'])) !== false) {
127160
if(isset($translations[$data[0]]) == false)
128161
$translations[$data[0]] = [];
129162

163+
if(sizeof($data) != 3)
164+
throw new \Exception("Wrong format of file. Try launch command with -X option if you use Excel for editing file.");
165+
130166
$translations[$data[0]][$data[1]] = $data[2];
131167
}
132168

133-
fclose($input_fp);
134-
135169
return $translations;
136170
}
137171

172+
/**
173+
* Adjust file to Excel format.
174+
*
175+
* @return void
176+
*/
177+
private function adjustFromExcel()
178+
{
179+
$data = file_get_contents($this->parameters['input']);
180+
file_put_contents($this->parameters['input'], mb_convert_encoding($data, 'UTF-8', 'UTF-16'));
181+
}
182+
183+
/**
184+
* Close file.
185+
*
186+
* @return void
187+
*/
188+
private function closeFile($input)
189+
{
190+
fclose($input);
191+
}
192+
138193
/**
139194
* Save fetched translations to file.
140195
*
141196
* @return void
142197
*/
143198
private function saveTranslations($translations)
144199
{
145-
LangListService::writeLangList($locale, $group, $translations);
200+
LangListService::writeLangList($this->parameters['locale'], $this->parameters['group'], $translations);
146201
}
147202

148203
/**
@@ -152,7 +207,7 @@ private function saveTranslations($translations)
152207
*/
153208
private function sayItsFinish()
154209
{
155-
$this->info('Finished! Translations imported from: '. (substr($this->parameters['output'], strlen(base_path()) + 1)) . $this->ext
210+
$this->info('Finished! Translations imported from: '. (substr($this->parameters['input'], strlen(base_path()) + 1))
156211
. PHP_EOL);
157212
}
158213

0 commit comments

Comments
 (0)