Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for laravel modules #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/UFirst/LangImportExport/Console/ExportToCsvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}

Expand All @@ -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");
Expand Down
31 changes: 20 additions & 11 deletions src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand All @@ -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) {
Expand All @@ -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}");
Expand All @@ -132,18 +140,19 @@ private function writeLangList($languages, $new_translations, $should_merge_tran
$translations = $undotted_translations;
}
$header = "<?php\n\nreturn ";
$language_dir = base_path("resources/lang/{$locale}");
$language_dir = $module ? base_path("resources/lang/{$module}/{$locale}") : base_path("resources/lang/{$locale}");
if (!is_writable($language_dir)) {
$this->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.");
Expand Down
34 changes: 28 additions & 6 deletions src/UFirst/LangImportExport/LangListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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];
Expand All @@ -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 [];
}
Expand All @@ -59,4 +76,9 @@ public function allGroup($language)
}
});
}

public function getLanguagePath()
{
return $this->module ? "{$this->languageFilesPath}/{$this->module}" : $this->languageFilesPath;
}
}