Skip to content

Commit 1f4c6ed

Browse files
committed
export command refactor
1 parent aa40945 commit 1f4c6ed

File tree

18 files changed

+300
-135
lines changed

18 files changed

+300
-135
lines changed

.travis.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
6-
- 5.5
74
- 5.6
8-
- hhvm
5+
- 7.0
6+
7+
matrix:
8+
include:
9+
- php: 5.6
10+
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
911

1012
before_script:
1113
- composer self-update
1214
- composer install --prefer-source --no-interaction --dev
15+
- composer dump-autoload
1316

1417
script: phpunit

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2014 UFirst Group
4+
Copyright (c) 2017 HighSolutions
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
laravel-lang-import-export
1+
Laravel-Lang-Import-Export
22
==========================
33

44
This package provides artisan commands to import and export language files from and to CSV. This can be used to send translations to agencies that normally work with Excel-like files.
@@ -43,7 +43,7 @@ Add the following line to the `require` section of your Laravel webapp's `compos
4343

4444
```javascript
4545
"require": {
46-
"ufirst/lang-import-export": "dev-master"
46+
"HighSolutions/laravel-lang-import-export": "5.4.*"
4747
}
4848
```
4949

@@ -56,7 +56,7 @@ Finally add the following line to the `providers` array of your `app/config/app.
5656
```php
5757
'providers' => array(
5858
/* ... */
59-
'UFirst\LangImportExport\LangImportExportServiceProvider'
59+
'HighSolutions\LangImportExport\LangImportExportServiceProvider'
6060
)
6161
```
6262

@@ -68,19 +68,33 @@ The package currently provides two commands, one for exporting the files and one
6868
### Export
6969

7070
```bash
71-
php artisan lang-export:csv en_US navigation
72-
php artisan lang-export:csv --output /some/file en_US navigation
73-
php artisan lang-export:csv --delimiter=";" --enclosure='"' --output=/some/file en_US navigation
71+
php artisan lang:export
72+
php artisan lang:export en * --output=export
73+
php artisan lang:export en auth --A --X
7474
```
7575

76-
You have to pass the __locale__ and the __group__ as arguments. The group is the name of the langauge file without its extension. You may define options for your desired CSV format.
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**).
77+
But there is few more useful parameters:
7778

78-
### Import
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 | " |
7988

89+
### Import
8090

8191
```
82-
php artisan lang-import:csv en_US navigation /some/file
83-
php artisan lang-import:csv --delimiter=";" --enclosure='"' --escape='\\' en_US navigation /some/file
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
8494
```
8595

86-
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. You may define options to match the CSV format of your input file.
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.
97+
98+
### Credits
99+
100+
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).

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
2-
"name": "ufirst/lang-import-export",
2+
"name": "highsolutions/laravel-lang-import-export",
33
"description": "A Laravel package providing artisan commands to import and export language files from and to CSV.",
44
"keywords": ["laravel", "localization", "translation", "messages", "import", "export", "CSV"],
55
"authors": [
66
{
77
"name": "Michael Ruoss",
8-
"email": "michael.ruoss@ufirstgroup.com"
8+
"email": "michael.ruoss@UFirstgroup.com"
9+
},
10+
{
11+
"name": "HighSolutions",
12+
"email": "adam@highsolutions.pl"
913
}
1014
],
1115
"license": "MIT",
@@ -14,11 +18,8 @@
1418
"illuminate/support": "~5.1"
1519
},
1620
"autoload": {
17-
"classmap": [
18-
"src/migrations"
19-
],
2021
"psr-0": {
21-
"UFirst\\LangImportExport\\": "src/"
22+
"HighSolutions\\LangImportExport\\": "src/"
2223
}
2324
},
2425
"extra": {

public/.gitkeep

Whitespace-only changes.

src/Console/ExportToCsvCommand.php

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php
2+
3+
namespace HighSolutions\LangImportExport\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use HighSolutions\LangImportExport\Facades\LangListService;
9+
10+
class ExportToCsvCommand extends Command
11+
{
12+
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
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 exported translation files (optional, default - storage/app/lang-import-export.csv).}
22+
{--A|append : Append name of group to the name of file (optional, default - empty).}
23+
{--X|excel : Set file encoding for Excel (optional, default - UTF-8).}
24+
{--D|delimiter=, : Field delimiter (optional, default - ",").}
25+
{--E|enclosure=" : Field enclosure (optional, default - \'"\').} ';
26+
27+
/**
28+
* The console command description.
29+
*
30+
* @var string
31+
*/
32+
protected $description = "Exports the language files to CSV file";
33+
34+
/**
35+
* Parameters provided to command.
36+
*
37+
* @var array
38+
*/
39+
protected $parameters = [];
40+
41+
/**
42+
* Default path for file save.
43+
*
44+
* @var string
45+
*/
46+
protected $defaultPath;
47+
48+
/**
49+
* File extension (default .csv).
50+
*
51+
* @var string
52+
*/
53+
protected $ext = '.csv';
54+
55+
/**
56+
* Class constructor.
57+
*
58+
* @return void
59+
*/
60+
public function __construct()
61+
{
62+
parent::__construct();
63+
$this->defaultPath = storage_path('app'. DIRECTORY_SEPARATOR .'lang-import-export');
64+
}
65+
66+
/**
67+
* Execute the console command.
68+
*
69+
* @return void
70+
*/
71+
public function handle()
72+
{
73+
$this->getParameters();
74+
75+
$this->sayItsBeginning();
76+
77+
$translations = $this->getTranslations();
78+
79+
$this->saveTranslations($translations);
80+
81+
$this->sayItsFinish();
82+
}
83+
84+
/**
85+
* Display output that command has started and which groups are being exported.
86+
*
87+
* @return void
88+
*/
89+
private function sayItsBeginning()
90+
{
91+
$this->info(PHP_EOL
92+
. 'Translations export of '. ($this->parameters['group'] === false ? 'all groups' : $this->parameters['group'] .' group') .' - started.');
93+
}
94+
95+
/**
96+
* Fetch command parameters (arguments and options) and analyze them.
97+
*
98+
* @return void
99+
*/
100+
private function getParameters()
101+
{
102+
$this->parameters = [
103+
'group' => $this->argument('group'),
104+
'locale' => $this->argument('locale') === false ? config('app.locale') : $this->argument('locale'),
105+
'output' => $this->option('output') === false ? $this->defaultPath : base_path($this->option('output')),
106+
'append' => $this->option('append') !== false,
107+
'excel' => $this->option('excel') !== false,
108+
'delimiter' => $this->option('delimiter'),
109+
'enclosure' => $this->option('enclosure'),
110+
];
111+
112+
$this->setDefaultPath();
113+
}
114+
115+
/**
116+
* Set possible file names.
117+
*
118+
* @return void
119+
*/
120+
private function setDefaultPath()
121+
{
122+
if($this->parameters['append']) {
123+
$this->parameters['output'] .= '-'. $this->parameters['group'];
124+
$this->defaultPath .= '-'. $this->parameters['group'];
125+
}
126+
}
127+
128+
/**
129+
* Get translations from localization files.
130+
*
131+
* @return array
132+
*/
133+
private function getTranslations()
134+
{
135+
return LangListService::loadLangList($this->parameters['locale'], $this->parameters['group']);
136+
}
137+
138+
/**
139+
* Save fetched translations to file.
140+
*
141+
* @return void
142+
*/
143+
private function saveTranslations($translations)
144+
{
145+
$output = $this->openFile();
146+
147+
$this->saveTranslationsToFile($output, $translations);
148+
149+
$this->closeFile($output);
150+
}
151+
152+
/**
153+
* Open specified file (if not possible, open default one).
154+
*
155+
* @return FilePointerResource
156+
*/
157+
private function openFile()
158+
{
159+
if (!($output = fopen($this->parameters['output'] . $this->ext, 'w'))) {
160+
$output = fopen($this->defaultPath . $this->ext, 'w');
161+
}
162+
return $output;
163+
}
164+
165+
/**
166+
* Save content of translation files to specified file.
167+
*
168+
* @param FilePointerResource $output
169+
* @param array $translations
170+
* @return void
171+
*/
172+
private function saveTranslationsToFile($output, $translations)
173+
{
174+
foreach ($translations as $group => $files) {
175+
foreach($files as $key => $value) {
176+
$this->writeFile($output, $group, $key, $value);
177+
}
178+
}
179+
}
180+
181+
/**
182+
* Put content of file to specified file with CSV parameters.
183+
*
184+
* @param FilePointerResource $output
185+
* @param string $group
186+
* @param string $key
187+
* @param string $value
188+
* @return void
189+
*
190+
*/
191+
private function writeFile()
192+
{
193+
$data = func_get_args();
194+
$output = array_shift($data);
195+
fputcsv($output, $data, $this->parameters['delimiter'], $this->parameters['enclosure']);
196+
}
197+
198+
/**
199+
* Close output file and check if adjust file to Excel format.
200+
*
201+
* @param FilePointerResource $output
202+
* @return void
203+
*/
204+
private function closeFile($output)
205+
{
206+
fclose($output);
207+
208+
if($this->parameters['excel'])
209+
$this->adjustToExcel();
210+
}
211+
212+
/**
213+
* Adjust file to Excel format.
214+
*
215+
* @return void
216+
*
217+
*/
218+
private function adjustToExcel()
219+
{
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'));
222+
}
223+
224+
/**
225+
* Display output that command is finished and where to find file.
226+
*
227+
* @return void
228+
*/
229+
private function sayItsFinish()
230+
{
231+
$this->info('Finished! Translations saved to: '. (substr($this->parameters['output'], strlen(base_path()) + 1)) . $this->ext
232+
. PHP_EOL);
233+
}
234+
235+
}

src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php renamed to src/Console/ImportFromCsvCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3-
namespace UFirst\LangImportExport\Console;
3+
namespace HighSolutions\LangImportExport\Console;
44

55
use Illuminate\Console\Command;
66
use Symfony\Component\Console\Input\InputOption;
77
use Symfony\Component\Console\Input\InputArgument;
8-
use \UFirst\LangImportExport\Facades\LangListService;
8+
use HighSolutions\LangImportExport\Facades\LangListService;
99

10-
class ImportFromCsvCommand extends Command {
10+
class ImportFromCsvCommand extends Command
11+
{
1112

1213
/**
1314
* The console command name.
@@ -84,4 +85,5 @@ public function fire()
8485
fclose($input_fp);
8586
LangListService::writeLangList($locale, $group, $strings);
8687
}
88+
8789
}

0 commit comments

Comments
 (0)