diff --git a/src/UFirst/LangImportExport/Console/ExportToCsvCommand.php b/src/UFirst/LangImportExport/Console/ExportToCsvCommand.php index 249fca9..66b3441 100644 --- a/src/UFirst/LangImportExport/Console/ExportToCsvCommand.php +++ b/src/UFirst/LangImportExport/Console/ExportToCsvCommand.php @@ -47,6 +47,7 @@ protected function getOptions() array('output', 'o', InputOption::VALUE_OPTIONAL, 'Redirect the output to this file'), array('locale', 'l', InputOption::VALUE_OPTIONAL, 'The locale to be exported'), array('group', 'g', InputOption::VALUE_OPTIONAL, 'The group (which is the name of the language file without the extension)'), + array('module', 'm', InputOption::VALUE_OPTIONAL, 'Module name for translation'), ); } @@ -61,6 +62,12 @@ public function handle() $enclosure = $this->option('enclosure'); $groupOption = $this->option('group'); $locale = $this->option('locale'); + $module = $this->option('module'); + + if($module) { + LangListService::setModule($module); + } + $languages = LangListService::allLanguages()->all(); if ($locale && !in_array($locale, $languages)) { $this->error("Locale ${locale} does not exist"); diff --git a/src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php b/src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php index 9d66934..5ba39db 100644 --- a/src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php +++ b/src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php @@ -51,6 +51,7 @@ protected function getOptions() array('enclosure', 'c', InputOption::VALUE_OPTIONAL, 'The optional enclosure parameter sets the field enclosure (one character only).', '"'), array('escape', 'e', InputOption::VALUE_OPTIONAL, 'The escape character (one character only). Defaults as a backslash.', '\\'), array('merge', 'm', InputOption::VALUE_OPTIONAL, 'Merge translations in single file instead of overwriting whole file.', false), + array('module', 'k', InputOption::VALUE_OPTIONAL, 'Module we should import to', null), ); } @@ -67,6 +68,7 @@ public function handle() $enclosure = $this->option('enclosure'); $escape = $this->option('escape'); $merge = $this->option('merge'); + $module = $this->option('module'); // Create output device and write CSV. if (($input_fp = fopen($file, 'r')) === FALSE) { @@ -87,27 +89,33 @@ public function handle() } } fclose($input_fp); - $this->writeLangList($languages, $translations, $merge); + $this->writeLangList($languages, $translations, $merge, $module); } - private function getGroupsFromNewTranslations($new_translations) + private function getGroupsFromNewTranslations($new_translations, ?string $module = null) { + $moduleRequested = $module; $groups = []; foreach ($new_translations as $key => $value) { - $group = explode('.', $key)[0]; - $groups[$group] = $group; + $group = explode('.', $key)[$moduleRequested ? 1 : 0]; + $groups[$group] = $group; } return $groups; } - private function writeLangList($languages, $new_translations, $should_merge_translations = false) + private function writeLangList($languages, $new_translations, $should_merge_translations = false, ?string $module = null) { - $groups = $this->getGroupsFromNewTranslations($new_translations); + $groups = $this->getGroupsFromNewTranslations($new_translations, $module); + + if($module) { + LangListService::setModule($module); + } + foreach ($languages as $locale) { foreach ($groups as $group) { $translations = LangListService::loadLangList($locale, $group); - $override_translations = array_filter($new_translations, function ($key) use ($group) { - return strpos($key, $group) === 0; + $override_translations = array_filter($new_translations, function ($key) use ($group, $module) { + return strpos($key, $group) !== -1; }, ARRAY_FILTER_USE_KEY); if (count($override_translations) === 0) { $this->info("No translations were found for locale ${locale} within group ${group}"); @@ -132,18 +140,19 @@ private function writeLangList($languages, $new_translations, $should_merge_tran $translations = $undotted_translations; } $header = "error("Language directory $language_dir does not exist or is not writeable. Skipping"); continue; } - $language_file = base_path("resources/lang/{$locale}/{$group}.php"); + $language_file = $module ? base_path("resources/lang/{$module}/{$locale}/{$group}.php") : base_path("resources/lang/{$locale}/{$group}.php"); if (!is_writable($language_file)) { $this->info("Creating language file: $language_file"); touch($language_file); } if (($fp = fopen($language_file, 'w')) !== FALSE) { - fputs($fp, $header . VarExporter::export($translations[$group]) . ";\n"); + $exported = $module ? $translations[$module][$group] : $translations[$group]; + fputs($fp, $header . VarExporter::export($exported) . ";\n"); fclose($fp); } else { $this->error("Cannot open language file at {$language_file} for writing. Check the file permissions."); diff --git a/src/UFirst/LangImportExport/LangListService.php b/src/UFirst/LangImportExport/LangListService.php index 0b0fe84..5940c3b 100644 --- a/src/UFirst/LangImportExport/LangListService.php +++ b/src/UFirst/LangImportExport/LangListService.php @@ -5,21 +5,38 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Filesystem\Filesystem; -use Lang; +use Illuminate\Support\Facades\Lang; class LangListService { + /** + * @var string|null + */ + private $module; - public function __construct(Filesystem $disk, $languageFilesPath) + public function __construct(Filesystem $disk, $languageFilesPath, ?string $module = null) { $this->disk = $disk; $this->languageFilesPath = $languageFilesPath; + $this->module = $module; + } + + public function setModule(string $module) + { + $this->module = $module; } public function loadLangList($locale, $group) { - $translations = Lang::getLoader()->load($locale, $group); - $translations_with_prefix = Arr::dot(array($group => $translations)); + $translations = Lang::getLoader()->load($this->module ? "{$this->module}/{$locale}" : $locale, $group); + $prefix_array = [$group => $translations]; + + if($this->module) { + $prefix_array = [$this->module => $prefix_array]; + } + + $translations_with_prefix = Arr::dot($prefix_array); + return $translations_with_prefix; } @@ -30,7 +47,7 @@ public function loadLangList($locale, $group) */ public function allLanguages() { - $directories = Collection::make($this->disk->directories($this->languageFilesPath)); + $directories = Collection::make($this->disk->directories($this->getLanguagePath())); return $directories->mapWithKeys(function ($directory) { $language = basename($directory); return [$language => $language]; @@ -46,7 +63,7 @@ public function allLanguages() */ public function allGroup($language) { - $groupPath = "{$this->languageFilesPath}" . DIRECTORY_SEPARATOR . "{$language}"; + $groupPath = $this->getLanguagePath() . DIRECTORY_SEPARATOR . "{$language}"; if (!$this->disk->exists($groupPath)) { return []; } @@ -59,4 +76,9 @@ public function allGroup($language) } }); } + + public function getLanguagePath() + { + return $this->module ? "{$this->languageFilesPath}/{$this->module}" : $this->languageFilesPath; + } }