Skip to content

Commit cbcdfa4

Browse files
committed
fixed bugs
1 parent 54be38f commit cbcdfa4

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
![Laravel-Lang-Import-Export v6](https://raw.githubusercontent.com/AidasK/laravel-lang-import-export/master/logo.png)
22

3-
Laravel-Lang-Import-Export
3+
Laravel-Lang-Import-Export Refactored
44
==========================
55

6-
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.
6+
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. In practice, CSV format is supper easy to work with for any translator in Fiverr or for any other freelancer. Personally, I have tried every other format such as *php, yaml, docx, pod, txt* and all of them has too complex syntax and requires custom software to work with (Not to mention all those problems with file encodings). CSV solves it all.
7+
8+
# How It Works?
79

810
It turns some navigation.php file...
911

@@ -45,8 +47,6 @@ Installation
4547
composer require aidask/laravel-lang-import-export
4648
```
4749

48-
Run `composer update` to install the package.
49-
5050
This package uses Laravel 5.5 Package Auto-Discovery.
5151
For previous versions of Laravel, you need to update `config/app.php` by adding an entry for the service provider:
5252

@@ -66,8 +66,8 @@ The package currently provides two commands, one for exporting the files and one
6666

6767
```bash
6868
php artisan lang:export --locale en
69-
php artisan lang:export --locale en --target fr # export en translations only missing in fr locale
70-
php artisan lang:export -z all.zip # archive all the files
69+
php artisan lang:export --locale en --target fr,de,pt # export en translations only missing in fr,de,pt locales. Each in separate files
70+
php artisan lang:export -l fr,de,pt -z all.zip # archive all the files
7171
php artisan lang:export --locale en -g paggination,validation # export only cretain groups
7272
```
7373

@@ -100,6 +100,9 @@ php artisan lang:import
100100
Changelog
101101
------------
102102

103+
6.1.0
104+
* Validate placeholders feature
105+
103106
6.0.0
104107
* refactor whole repository
105108

src/Console/ExportToCsvCommand.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ class ExportToCsvCommand extends Command
4646
*/
4747
public function handle()
4848
{
49-
foreach ($this->strToArray($this->option('locale')) as $locale) {
50-
foreach ($this->strToArray($this->option('target'), [null]) as $target) {
51-
$translations = $this->getTranslations($locale, $target);
52-
$this->saveTranslations($locale, $target, $translations);
53-
$this->info(strtoupper($locale) . strtoupper($target ?: '') . ' Translations saved to: ' . $this->getOutputFileName($locale, $target));
49+
$exportLocales = $this->option('locale') ?: config('lang_import_export.export_locale');
50+
$targetLocales = $this->option('target') ?: config('lang_import_export.export_target');
51+
foreach ($this->strToArray($exportLocales) as $exportLocale) {
52+
foreach ($this->strToArray($targetLocales, [null]) as $targetLocale) {
53+
$translations = $this->getTranslations($exportLocale, $targetLocale);
54+
$this->saveTranslations($exportLocale, $targetLocale, $translations);
55+
$this->info(strtoupper($exportLocale) . strtoupper($targetLocale ?: '') . ' Translations saved to: ' . $this->getOutputFileName($exportLocale, $targetLocale));
5456
}
5557
}
5658
if ($zipName = $this->option('zip')) {
@@ -107,6 +109,7 @@ private function getTranslations($locale, $target = null)
107109
* @param $target
108110
* @param $translations
109111
* @return void
112+
* @throws \Exception
110113
*/
111114
private function saveTranslations($locale, $target, $translations)
112115
{
@@ -115,6 +118,10 @@ private function saveTranslations($locale, $target, $translations)
115118
$this->saveTranslationsToFile($output, $translations);
116119

117120
$this->closeFile($output);
121+
122+
if ($this->option('excel')) {
123+
$this->adjustToExcel($this->getOutputFileName($locale, $target));
124+
}
118125
}
119126

120127
/**
@@ -161,12 +168,7 @@ private function saveTranslationsToFile($output, $translations)
161168
/**
162169
* Put content of file to specified file with CSV parameters.
163170
*
164-
* @param FilePointerResource $output
165-
* @param string $group
166-
* @param string $key
167-
* @param string $value
168171
* @return void
169-
*
170172
*/
171173
private function writeFile()
172174
{
@@ -178,16 +180,12 @@ private function writeFile()
178180
/**
179181
* Close output file and check if adjust file to Excel format.
180182
*
181-
* @param FilePointerResource $output
183+
* @param resource $output
182184
* @return void
183185
*/
184186
private function closeFile($output)
185187
{
186188
fclose($output);
187-
188-
if ($this->option('excel')) {
189-
$this->adjustToExcel();
190-
}
191189
}
192190

193191
/**
@@ -196,11 +194,13 @@ private function closeFile($output)
196194
* @return void
197195
*
198196
*/
199-
private function adjustToExcel()
197+
private function adjustToExcel($fileName)
200198
{
201-
$data = file_get_contents($this->option('output'));
202-
file_put_contents($this->option('output'),
203-
chr(255) . chr(254) . mb_convert_encoding($data, 'UTF-16LE', 'UTF-8'));
199+
$data = file_get_contents($fileName);
200+
file_put_contents(
201+
$fileName,
202+
chr(255) . chr(254) . mb_convert_encoding($data, 'UTF-16LE', 'UTF-8')
203+
);
204204
}
205205

206206
/**
@@ -210,7 +210,7 @@ private function adjustToExcel()
210210
*/
211211
private function getOutputFileName($locale, $target = null)
212212
{
213-
$fileName = $this->option('output');
213+
$fileName = $this->option('output') ?: config('lang_import_export.export_path');
214214
$fileName = str_replace(':locale', $locale, $fileName);
215215
$fileName = str_replace(':target', $target, $fileName);
216216
return $fileName;

src/LangListService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private function getTranslations($locale, $group, $new_translations)
143143
return $translations;
144144
}
145145

146-
private function validatePlaceholders($targetTranslations, $baseTranslations)
146+
public function validatePlaceholders($targetTranslations, $baseTranslations)
147147
{
148148
foreach ($targetTranslations as $group => $translations) {
149149
foreach ($translations as $key => $translation) {

src/config/lang_import_export.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
return [
33
'base_locale' => 'en',
4+
'export_locale' => 'en',// locale list separated by comma
5+
'export_target' => null,
46
'base_group' => null, // all groups or comma separated list
57
'export_path' => storage_path(':locale:target.csv'),
68
];

0 commit comments

Comments
 (0)