Skip to content

Commit aa40945

Browse files
committed
support for all lang files import/export
1 parent e93c8a1 commit aa40945

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/UFirst/LangImportExport/Console/ExportToCsvCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,18 @@ public function fire()
7272
}
7373

7474
// Write CSV lintes
75-
foreach ($strings as $key => $value) {
76-
fputcsv($out, array($key, $value), $delimiter, $enclosure);
75+
foreach ($strings as $group => $files) {
76+
foreach($files as $key => $value) {
77+
$this->writeFile($out, $group, $key, $value, $delimiter, $enclosure);
78+
}
7779
}
7880

7981
fclose($out);
8082
}
83+
84+
private function writeFile($out, $group, $key, $value, $delimiter, $enclosure)
85+
{
86+
fputcsv($out, array($group, $key, $value), $delimiter, $enclosure);
87+
}
88+
8189
}

src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public function fire()
7575

7676
// Write CSV lintes
7777
while (($data = fgetcsv($input_fp, 0, $delimiter, $enclosure, $escape)) !== FALSE) {
78-
$strings[$data[0]] = $data[1];
78+
if(isset($strings[$data[0]]) == false)
79+
$strings[$data[0]] = [];
80+
81+
$strings[$data[0]][$data[1]] = $data[2];
7982
}
8083

8184
fclose($input_fp);

src/UFirst/LangImportExport/LangListService.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,54 @@
33
namespace UFirst\LangImportExport;
44

55
use Lang;
6+
use File;
67

7-
class LangListService {
8+
class LangListService
9+
{
810

9-
public function loadLangList($locale, $group) {
11+
public function loadLangList($locale, $group)
12+
{
13+
$result = [];
14+
if($group != '*') {
15+
$result[$group] = $this->getGroup($locale, $group);
16+
return $result;
17+
}
18+
19+
$path = resource_path('lang/'. $locale.'/');
20+
$files = File::allFiles($path);
21+
foreach($files as $file) {
22+
$file_path = substr($file->getRealPath(), strlen($path), -4);
23+
$result[$file_path] = $this->getGroup($locale, $file_path);
24+
}
25+
return $result;
26+
}
27+
28+
private function getGroup($locale, $group)
29+
{
1030
$translations = Lang::getLoader()->load($locale, $group);
11-
$translations_with_prefix = array_dot(array($group => $translations));
12-
return $translations_with_prefix;
31+
return array_dot($translations);
1332
}
1433

15-
public function writeLangList($locale, $group, $new_translations) {
34+
public function writeLangList($locale, $group, $new_translations)
35+
{
36+
if($group != '*')
37+
return $this->writeLangFile($locale, $group, $new_translations);
38+
39+
foreach($new_translations as $group => $translations)
40+
$this->writeLangFile($locale, $group, $translations);
41+
}
42+
43+
private function writeLangFile($locale, $group, $new_translations)
44+
{
1645
$translations = Lang::getLoader()->load($locale, $group);
1746
foreach($new_translations as $key => $value) {
1847
array_set($translations, $key, $value);
1948
}
2049
$header = "<?php\n\nreturn ";
2150

22-
$language_file = base_path("resources/lang/{$locale}/{$group}.php");
51+
$language_file = resource_path("lang/{$locale}/{$group}.php");
2352
if (is_writable($language_file) && ($fp = fopen($language_file, 'w')) !== FALSE) {
24-
25-
fputs($fp, $header.var_export($translations[$group], TRUE).";\n");
53+
fputs($fp, $header . var_export($translations, TRUE).";\n");
2654
fclose($fp);
2755
} else {
2856
throw new \Exception("Cannot open language file at {$language_file} for writing. Check the file permissions.");

0 commit comments

Comments
 (0)