From 66ba08de010396d22cc295492c23458cf6a0910b Mon Sep 17 00:00:00 2001 From: abderrahim Date: Mon, 15 Mar 2021 08:29:19 +0100 Subject: [PATCH] Initial Commit --- .../Commands/LangJsCommand.php | 6 +- .../Generators/LangJsGenerator.php | 133 ++++++++++++++---- .../LaravelJsLocalizationServiceProvider.php | 8 +- 3 files changed, 114 insertions(+), 33 deletions(-) diff --git a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php index d44d32d..7a6e65e 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php +++ b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php @@ -2,7 +2,7 @@ namespace Mariuzzo\LaravelJsLocalization\Commands; -use Config; +use Illuminate\Support\Facades\Config; use Illuminate\Console\Command; use Mariuzzo\LaravelJsLocalization\Generators\LangJsGenerator; use Symfony\Component\Console\Input\InputArgument; @@ -66,6 +66,7 @@ public function handle() 'json' => $this->option('json'), 'no-lib' => $this->option('no-lib'), 'source' => $this->option('source'), + 'no-sort' => $this->option('no-sort'), ]; if ($this->generator->generate($target, $options)) { @@ -96,7 +97,7 @@ protected function getArguments() */ protected function getDefaultPath() { - return Config::get('localization-js.path', public_path('messages.js')); + return Config::get('localization-js.path'); } /** @@ -111,6 +112,7 @@ protected function getOptions() ['no-lib', 'nl', InputOption::VALUE_NONE, 'Do not include the lang.js library.', null], ['json', 'j', InputOption::VALUE_NONE, 'Only output the messages json.', null], ['source', 's', InputOption::VALUE_REQUIRED, 'Specifying a custom source folder', null], + ['no-sort', 'ns', InputOption::VALUE_NONE, 'Do not sort the messages', null], ]; } } diff --git a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php index 58c7fb2..8cf1637 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php +++ b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php @@ -2,7 +2,10 @@ namespace Mariuzzo\LaravelJsLocalization\Generators; +use Illuminate\Support\Facades\Config; +use InvalidArgumentException; use Illuminate\Filesystem\Filesystem as File; +use Illuminate\Support\Str; use JShrink\Minifier; /** @@ -36,7 +39,7 @@ class LangJsGenerator /** * Name of the domain in which all string-translation should be stored under. * More about string-translation: https://laravel.com/docs/master/localization#retrieving-translation-strings - * + * * @var string */ protected $stringsDomain = 'strings'; @@ -44,7 +47,7 @@ class LangJsGenerator /** * Construct a new LangJsGenerator instance. * - * @param File $file The file service instance. + * @param File $file The file service instance. * @param string $sourcePath The source path of the language files. */ public function __construct(File $file, $sourcePath, $messagesIncluded = []) @@ -57,8 +60,8 @@ public function __construct(File $file, $sourcePath, $messagesIncluded = []) /** * Generate a JS lang file from all language files. * - * @param string $target The target directory. - * @param array $options Array of options. + * @param string $target The target directory. + * @param array $options Array of options. * * @return int */ @@ -68,26 +71,28 @@ public function generate($target, $options) $this->sourcePath = $options['source']; } - $messages = $this->getMessages(); - $this->prepareTarget($target); - - if ($options['no-lib']) { - $template = $this->file->get(__DIR__.'/Templates/messages.js'); - } else if ($options['json']) { - $template = $this->file->get(__DIR__.'/Templates/messages.json'); - } else { - $template = $this->file->get(__DIR__.'/Templates/langjs_with_messages.js'); - $langjs = $this->file->get(__DIR__.'/../../../../lib/lang.min.js'); - $template = str_replace('\'{ langjs }\';', $langjs, $template); - } - $template = str_replace('\'{ messages }\'', json_encode($messages), $template); + $langues = config('app.plateforme_langues'); + + + $template = $this->file->get(__DIR__ . '/Templates/messages.json'); - if ($options['compress']) { - $template = Minifier::minify($template); + + foreach ($langues as $langue) { + + $messages = $this->getMessages($options['no-sort'], $langue); + $template = str_replace('\'{ messages }\'', json_encode($messages, JSON_UNESCAPED_UNICODE), $template); + + if ($options['compress']) { + $template = Minifier::minify($template); + } + + $langue_target = Config::get('localization-js.path') . "/" . $langue . '.json'; + $this->file->put($langue_target, $template); } - return $this->file->put($target, $template); + return true; + } /** @@ -109,11 +114,12 @@ protected function sortMessages(&$messages) /** * Return all language messages. * + * @param bool $noSort Whether sorting of the messages should be skipped. * @return array * * @throws \Exception */ - protected function getMessages() + protected function getMessages($noSort, $langue = null) { $messages = []; $path = $this->sourcePath; @@ -122,6 +128,66 @@ protected function getMessages() throw new \Exception("${path} doesn't exists!"); } + foreach ($this->file->allFiles($path) as $file) { + $pathName = $file->getRelativePathName(); + $extension = $this->file->extension($pathName); + if ($extension != 'php' && $extension != 'json') { + continue; + } + + if ($this->isMessagesExcluded($pathName)) { + continue; + } + $lang_dir = substr($pathName, 0, 2); + + if ($langue != $lang_dir) + continue; + + + $key = substr($pathName, 0, -4); + $key = str_replace('\\', '.', $key); + $key = str_replace('/', '.', $key); + $key = str_replace($langue . ".", "", $key); + + + if (Str::startsWith($key, 'vendor')) { + $key = $this->getVendorKey($key); + } + + $fullPath = $path . DIRECTORY_SEPARATOR . $pathName; + + + if ($extension == 'php') { + $messages[$key] = include $fullPath; + } else { + $key = $key . $this->stringsDomain; + $fileContent = file_get_contents($fullPath); + $messages[$key] = json_decode($fileContent, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new InvalidArgumentException('Error while decode ' . basename($fullPath) . ': ' . json_last_error_msg()); + } + } + } + + $this->getThemeMessages($messages, $langue); + + if (!$noSort) { + $this->sortMessages($messages); + } + + + return $messages; + } + + protected function getThemeMessages(&$messages, $langue) + { + $path = theme_path('resources/lang', config('themes.active')); + + if (!$this->file->exists($path)) { + throw new \Exception("${path} doesn't exists!"); + } + foreach ($this->file->allFiles($path) as $file) { $pathName = $file->getRelativePathName(); $extension = $this->file->extension($pathName); @@ -133,25 +199,38 @@ protected function getMessages() continue; } + $lang_dir = substr($pathName, 0, 2); + + if ($langue != $lang_dir) + continue; + + $key = substr($pathName, 0, -4); $key = str_replace('\\', '.', $key); $key = str_replace('/', '.', $key); + $key = str_replace($langue . ".", "", $key); - if (starts_with($key, 'vendor')) { + if (Str::startsWith($key, 'vendor')) { $key = $this->getVendorKey($key); } - $fullPath = $path.DIRECTORY_SEPARATOR.$pathName; + $fullPath = $path . DIRECTORY_SEPARATOR . $pathName; + + $key = "theme." . $key; + if ($extension == 'php') { $messages[$key] = include $fullPath; } else { - $key = $key.$this->stringsDomain; + $key = $key . $this->stringsDomain; $fileContent = file_get_contents($fullPath); $messages[$key] = json_decode($fileContent, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new InvalidArgumentException('Error while decode ' . basename($fullPath) . ': ' . json_last_error_msg()); + } } } - $this->sortMessages($messages); return $messages; } @@ -196,12 +275,12 @@ protected function isMessagesExcluded($filePath) return true; } - + private function getVendorKey($key) { $keyParts = explode('.', $key, 4); unset($keyParts[0]); - return $keyParts[2] .'.'. $keyParts[1] . '::' . $keyParts[3]; + return $keyParts[2] . '.' . $keyParts[1] . '::' . $keyParts[3]; } } diff --git a/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php b/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php index b34b96a..eab4548 100644 --- a/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php +++ b/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php @@ -48,7 +48,7 @@ public function boot() if ($laravelMajorVersion === 4) { $config = $this->app['config']->get($configKey, []); $this->app['config']->set($configKey, array_merge(require $configPath, $config)); - } elseif ($laravelMajorVersion === 5) { + } elseif ($laravelMajorVersion >= 5) { $this->publishes([ $configPath => config_path("$configKey.php"), ]); @@ -67,12 +67,12 @@ public function register() $this->app->singleton('localization.js', function ($app) { $app = $this->app; $laravelMajorVersion = (int) $app::VERSION; - + $files = $app['files']; - + if ($laravelMajorVersion === 4) { $langs = $app['path.base'].'/app/lang'; - } elseif ($laravelMajorVersion === 5) { + } elseif ($laravelMajorVersion >= 5) { $langs = $app['path.base'].'/resources/lang'; } $messages = $app['config']->get('localization-js.messages');