|
3 | 3 | namespace UFirst\LangImportExport;
|
4 | 4 |
|
5 | 5 | use Lang;
|
| 6 | +use File; |
6 | 7 |
|
7 |
| -class LangListService { |
| 8 | +class LangListService |
| 9 | +{ |
8 | 10 |
|
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 | + { |
10 | 30 | $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); |
13 | 32 | }
|
14 | 33 |
|
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 | + { |
16 | 45 | $translations = Lang::getLoader()->load($locale, $group);
|
17 | 46 | foreach($new_translations as $key => $value) {
|
18 | 47 | array_set($translations, $key, $value);
|
19 | 48 | }
|
20 | 49 | $header = "<?php\n\nreturn ";
|
21 | 50 |
|
22 |
| - $language_file = base_path("resources/lang/{$locale}/{$group}.php"); |
| 51 | + $language_file = resource_path("lang/{$locale}/{$group}.php"); |
23 | 52 | 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"); |
26 | 54 | fclose($fp);
|
27 | 55 | } else {
|
28 | 56 | throw new \Exception("Cannot open language file at {$language_file} for writing. Check the file permissions.");
|
|
0 commit comments