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

Add theme language files and separate files to use them with angular package ngx-translate #159

Open
wants to merge 1 commit 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
6 changes: 4 additions & 2 deletions src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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],
];
}
}
133 changes: 106 additions & 27 deletions src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -36,15 +39,15 @@ 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';

/**
* 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 = [])
Expand All @@ -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
*/
Expand All @@ -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;

}

/**
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]);
Expand All @@ -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');
Expand Down