This commit is contained in:
Roly Rudy Gutierrez Pinto
2018-10-30 10:15:30 -04:00
parent 8a969ebb22
commit 218fef9951
8 changed files with 513 additions and 67 deletions

View File

@@ -0,0 +1,210 @@
<?php
namespace App\Console\Commands;
use G;
use i18n_PO;
use Illuminate\Console\Command;
use ProcessMaker\Util\Translation\I18Next;
use stdClass;
/**
* Class PMTranslationsPlugins
* @package ProcessMaker\Console\Commands
*/
class PMTranslationsPlugins extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'translation:plugin
{--a|all : Translate all plugins}
{--name=* : Input Plugin name}
{--type=po : Input translation source(po|laravel)}';
/**
* The console command description.
* @var string
*/
protected $description = 'Translation plugin of Processmaker';
/**
* Path for plugins
* @var string
*/
protected $pluginsPath;
/**
* Class I18next
* @var I18next
*/
protected $i18next;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->pluginsPath = realpath(base_path() . DIRECTORY_SEPARATOR . "workflow" . DIRECTORY_SEPARATOR . "engine" . DIRECTORY_SEPARATOR . "plugins");
}
/**
* Execute the console command.
*/
public function handle()
{
$pluginNames = $this->option('name');
$processAll = $this->option('all');
$processType = $this->option('type');
$directoryPlugins = [];
if ($processAll) {
$directoryPlugins = $this->filterFiles(array_diff(scandir($this->pluginsPath), ['..', '.'])) ?: [];
} elseif ($pluginNames) {
$directoryPlugins = $pluginNames;
} else {
$this->comment("Please use the --all option or introduce the plugin name (--name=namePlugin)");
return;
}
$this->info('Start converting');
$bar = $this->output->createProgressBar(count($directoryPlugins));
foreach ($directoryPlugins as $name) {
if ($processType == 'po') {
$this->generateI18nFromPoFiles($name);
} elseif ($processType == 'laravel') {
$this->generateI18nFromLaravelLang($name);
}
$bar->advance();
}
$bar->finish();
$this->info("\nFinish");
}
/**
* Generate files i18n from .po.
*
* @param object $pluginName
* @return void
*/
private function generateI18nFromPoFiles($pluginName)
{
$pluginPath = $this->pluginsPath . DIRECTORY_SEPARATOR . $pluginName;
if (is_dir($pluginPath)) {
// Translate for files .po in plugin
$translationsDirectory = $pluginPath . DIRECTORY_SEPARATOR . 'translations';
$scannedDirectory = is_dir($translationsDirectory) ? array_diff(scandir($translationsDirectory), ['..', '.']) : null;
if ($scannedDirectory) {
$this->i18next = new I18Next();
foreach ($scannedDirectory as $index => $item) {
$filePath = $translationsDirectory . DIRECTORY_SEPARATOR . $item;
$pathParts = pathinfo($filePath);
$isPofile = !empty($pathParts['extension']) && $pathParts['extension'] === 'po';
if ($isPofile) {
$basename = explode('.', $pathParts['basename']);
$language = $basename[1];
$this->i18next->setLanguage($language);
//read file .po
$str = new stdClass();
$poFile = new i18n_PO($filePath);
$poFile->readInit();
while ($translation = $poFile->getTranslation()) {
$translatorComments = $poFile->translatorComments;
$references = $poFile->references;
$ifContinue = empty($translatorComments[0]) && empty($translatorComments[1]) && empty($references[0]);
if ($ifContinue) {
continue;
}
$ifNotTranslation = !($translatorComments[0] === 'TRANSLATION');
if ($ifNotTranslation) {
continue;
}
$key = explode("/", $translatorComments[1]);
$str->{$key[1]} = $translation['msgstr'];
}
$this->i18next->setPlugin($language, $pluginName, $str);
}
}
$this->saveFileJs($pluginName);
}
}
}
/**
* Generate files i18n from resource/lang/*.
*
* @param $pluginName
* @return void
*/
private function generateI18nFromLaravelLang($pluginName)
{
$pluginPath = $this->pluginsPath . DIRECTORY_SEPARATOR . $pluginName;
if (is_dir($pluginPath)) {
// Translate for files resources/lang in plugin
$translationsDirectory = $pluginPath . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'lang';
$scannedDirectory = is_dir($translationsDirectory) ? array_diff(scandir($translationsDirectory), ['..', '.']) : null;
if ($scannedDirectory) {
$this->i18next = new I18Next();
foreach ($scannedDirectory as $lang) {
$dirLanguage = $pluginPath . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $lang;
$scannedLanguage = is_dir($dirLanguage) ? array_diff(scandir($dirLanguage), ['..', '.']) : [];
foreach ($scannedLanguage as $index => $item) {
$filePath = $dirLanguage . DIRECTORY_SEPARATOR . $item;
$pathParts = pathinfo($filePath);
$isPhpFile = !empty($pathParts['extension']) && $pathParts['extension'] === 'php';
if ($isPhpFile) {
$file = explode(".", $item);
array_pop($file);
$filename = implode("_", $file);
$arrayLanguage = [$filename => require_once($dirLanguage . DIRECTORY_SEPARATOR . $item)];
$this->i18next->setLanguage($lang);
$this->i18next->setPlugin($lang, $pluginName, json_decode(json_encode($arrayLanguage)));
}
}
}
$this->saveFileJs($pluginName);
}
}
}
/**
* Save js file generate of translate files.
*
* @param string $pluginName
*/
private function saveFileJs($pluginName)
{
$folderToSave = $this->pluginsPath . DIRECTORY_SEPARATOR . $pluginName . DIRECTORY_SEPARATOR . "public_html" . DIRECTORY_SEPARATOR . "js";
if (!is_dir($folderToSave)) {
$create = $this->choice('The "js" folder does not exist, Do you want to create the folder?', ['Yes', 'No'], 0);
if (strtolower($create) == 'yes') {
G::mk_dir($folderToSave, 0775);
}
}
$this->i18next->saveJs($pluginName, $folderToSave . DIRECTORY_SEPARATOR . $pluginName . ".i18n");
}
/**
* Remove files, return only folders.
*
* @param $scannedDirectory
* @return array
*/
private function filterFiles($scannedDirectory)
{
$onlyFolders = [];
foreach ($scannedDirectory as $index => $item) {
$pluginPath = $this->pluginsPath . DIRECTORY_SEPARATOR . $item;
if (is_dir($pluginPath)) {
array_push($onlyFolders, $item);
}
}
return $onlyFolders;
}
}

39
app/Console/Kernel.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\PMTranslationsPlugins::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
}
}

View File

@@ -4,7 +4,6 @@ use Illuminate\Contracts\Console\Kernel as Kernel2;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Http\Kernel as Kernel4;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Console\Kernel;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Foundation\Http\Kernel as Kernel3;
use Monolog\Formatter\LineFormatter;
@@ -43,7 +42,7 @@ $app->singleton(
$app->singleton(
Kernel2::class,
Kernel::class
App\Console\Kernel::class
);
$app->singleton(

View File

@@ -36,7 +36,7 @@
"colosa/pmdynaform": "release/3.2.4-dev",
"google/apiclient": "1.1.6",
"dapphp/securimage": "^3.6",
"psr/log":"1.0.0",
"psr/log": "1.0.0",
"monolog/monolog": "1.19.0",
"geshi/geshi": "dev-master",
"libchart/libchart": "1.4.0",
@@ -46,8 +46,8 @@
"TYPO3/class-alias-loader": "^1.0",
"ralouphie/getallheaders": "^2.0",
"smarty/smarty": "2.6.30",
"pdepend/pdepend" : "@stable",
"chumper/zipper" : "^1.0"
"pdepend/pdepend": "@stable",
"chumper/zipper": "^1.0"
},
"require-dev": {
"fzaninotto/faker": "^1.7",
@@ -64,8 +64,9 @@
"ProcessMaker\\": "workflow/engine/src"
},
"psr-4": {
"Maveriks\\": "framework/src/Maveriks/",
"Tests\\": "tests/"
"App\\": "app/",
"Maveriks\\": "framework/src/Maveriks/",
"Tests\\": "tests/"
},
"classmap": [
"gulliver/system/",
@@ -97,7 +98,7 @@
},
"scripts": {
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wimg/php-compatibility",
"post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wimg/php-compatibility"
"post-update-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wimg/php-compatibility"
},
"extra": {
"typo3/class-alias-loader": {

2
gulliver/js/i18next/i18next.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,18 @@
<?php
namespace ProcessMaker;
class Application extends \Illuminate\Foundation\Application
{
/**
* Overrides the path to the application "app" directory.
*
* @param string $path Optionally, a path to append to the app path
* @return string
*/
public function path($path = '')
{
return $this->basePath . DIRECTORY_SEPARATOR . 'workflow' . DIRECTORY_SEPARATOR .
'engine' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'ProcessMaker';
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace ProcessMaker\Util\Translation;
use stdClass;
/**
* Class I18next
* @package ProcessMaker\Util\Translation
*/
class I18Next
{
protected $languages;
/**
* I18next constructor.
*/
public function __construct()
{
$this->languages = new stdClass();
}
/**
* Add translate globals.
*
* @param string $lan
* @param array $translation
*/
public function setTranslation($lan, array $translation)
{
foreach ($translation as $index => $item) {
$this->languages->{$lan}->translation->{$index} = $item;
}
}
/**
* Get a language.
*
* @param string $lan
* @return stdClass|null
*/
public function getLanguage($lan)
{
if (property_exists($this->languages, $lan)) {
return $this->languages->{$lan};
}
return null;
}
/**
* Add language in object.
*
* @param string $lan
*/
public function setLanguage($lan)
{
if (!property_exists($this->languages, $lan)) {
$this->languages->{$lan} = new stdClass();
$this->languages->{$lan}->translation = new stdClass();
}
}
/**
* Get plugin.
*
* @param string $lan
* @param string $pluginName
* @return stdClass
*/
public function getPlugin($lan, $pluginName)
{
return $this->languages->{$lan}->translation->{$pluginName};
}
/**
* Set plugin.
*
* @param string $lan
* @param string $pluginName
* @param object $plugin
*/
public function setPlugin($lan, $pluginName, $plugin)
{
if (!property_exists($this->languages->{$lan}->translation, $pluginName)) {
$this->languages->{$lan}->translation->{$pluginName} = new stdClass();
}
$objMerged = (object) array_merge(
(array) $this->languages->{$lan}->translation->{$pluginName}, (array) $plugin
);
$this->languages->{$lan}->translation->{$pluginName} = $objMerged;
}
/**
* Generate file i18n in json.
*
* @param string $filename
* @return bool
*/
public function saveJson($filename)
{
if ($filename) {
return file_put_contents($filename, json_encode($this->languages)) !== false;
}
return false;
}
/**
* Generate file i18n in js.
*
* @param string $pluginName
* @param string $filename
* @return bool
*/
public function saveJs($pluginName, $filename)
{
if ($filename) {
$fileContent = "$pluginName = {};";
$fileContent .= "$pluginName.i18n = function() { return " . json_encode($this->languages) . "; }";
return file_put_contents($filename . ".js", $fileContent) !== false;
}
return false;
}
}