Skip to content

Commit

Permalink
add Localization command
Browse files Browse the repository at this point in the history
  • Loading branch information
GIGABAIT93 committed Feb 19, 2023
1 parent 9e733d2 commit 2ba1d70
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 12 deletions.
163 changes: 163 additions & 0 deletions src/Commands/LangCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Wemx\Installer\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Matriphe\ISO639\ISO639;
use Wemx\Installer\FileEditor;

class LangCommand extends Command
{

protected $signature = 'billing:lang {--action= : import/generate} {--locale= : en}';
protected $description = 'Install Billing localization for Pterodactyl';

private const SEP = DIRECTORY_SEPARATOR;
private $locale, $fallback_locale, $lang_path, $default_file, $iosLangs;
private $args = [];

private const ACTIONS = [
'import' => 'Import localization (Imports additional Billing translations into all existing localizations, if any)',
'generate' => 'Generate new localization (This command will generate files of the selected localization of Billing and Pterodactyl)',
];

public function __construct()
{
parent::__construct();
$this->locale = config('app.locale') ? config('app.locale') : 'en';
$this->fallback_locale = config('app.fallback_locale') ? config('app.fallback_locale') : 'en';
$this->lang_path = resource_path('lang');
$this->default_file = $this->lang_path . self::SEP . 'default.php';
$this->createDir([
$this->lang_path . self::SEP . $this->locale . self::SEP . 'billing',
$this->lang_path . self::SEP . $this->fallback_locale . self::SEP . 'billing',
]);

}

public function handle()
{
$this->args['ACTIONS'] = $this->option('action') ?? $this->choice(
'Choose an action',
self::ACTIONS
);

switch ($this->args['ACTIONS']) {
case 'import':
$this->import();
break;
case 'generate':
$this->generate();
break;
default:
$this->import();
break;
}

}

private function import()
{
foreach ($this->getExistLocalization() as $key => $value) {
$file = $value . self::SEP . 'billing' . self::SEP . 'client.php';
if (!file_exists($file)) {
$content = file_get_contents("https://raw.githubusercontent.com/Mubeen142/billing-docs/main/lang/{$key}/billing/client.php");
if (strlen($content) > 100) {
file_put_contents($file, $content);
$this->info("The localization {$this->languages()[$key]} was found on GITHUB and added");
continue;
}
File::copy($this->default_file, $file);
$this->info("The localization {$this->languages()[$key]} was generated with a standard translation");
} else {
$this->info("Update localization {$this->languages()[$key]}");
$this->updateLocalization($file);
}
}
$this->info("Import done");
}

private function generate()
{
$code = $this->option('locale') ?? $this->choice(
'Choose an locale',
$this->languages()
);

$file = $this->lang_path . self::SEP . $code . self::SEP . 'billing' . self::SEP . 'client.php';
if (!file_exists($file)) {
$this->copyDirectory($this->lang_path . self::SEP . 'en', $this->lang_path . self::SEP . $code);
$content = file_get_contents("https://raw.githubusercontent.com/Mubeen142/billing-docs/main/lang/{$code}/billing/client.php");
if (strlen($content) > 100) {
file_put_contents($file, $content);
$this->info("The localization {$this->languages()[$code]} was found on GITHUB and added");
return;
}
File::copy($this->default_file, $file);
$this->info("The localization {$this->languages()[$code]} was generated with a standard translation");
} else {
$this->warn("Canceled {$this->languages()[$code]}!!! Localization already exists");
}

}

private function getExistLocalization()
{
$langs_dirs = File::directories($this->lang_path);
$langs = [];
foreach ($langs_dirs as $value) {
$langs[basename($value)] = $value;
}
return $langs;
}

private function createDir($path)
{
if (is_array($path)) {
foreach ($path as $dir) {
if (!file_exists($dir)) {
File::makeDirectory($dir, 0755, true);
}
}
return;
}
if (!file_exists($path)) {
File::makeDirectory($path, 0755, true);

}
}

private function copyDirectory($source_path, $destination_path)
{
if (!File::isDirectory($destination_path)) {
File::makeDirectory($destination_path, 0755, true);
}
File::copyDirectory($source_path, $destination_path);

}

private function languages()
{
$this->iosLangs = new ISO639;
$codes = [];
foreach ($this->iosLangs->allLanguages() as $key => $value) {
$codes[$value[0]] = $value[4];
}
return $codes;
}

private function updateLocalization($edit_locale_path)
{
$default_locale = include $this->default_file;
$edit_locale = include $edit_locale_path;
foreach ($default_locale as $key => $value) {
if (isset($edit_locale[$key])) {
continue;
}
FileEditor::appendBefore($edit_locale_path, '];', " '{$key}' => " . json_encode($value) . ",");
}
return;
}

}
2 changes: 1 addition & 1 deletion src/Commands/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public function handle()
// ]);
// }
}
}
}
4 changes: 3 additions & 1 deletion src/CommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Wemx\Installer\Commands\UninstallCommand;
use Wemx\Installer\Commands\VersionCommand;
use Wemx\Installer\Commands\YarnCommand;
use Wemx\Installer\Commands\LangCommand;
use Wemx\Installer\FileEditor;

class CommandsServiceProvider extends ServiceProvider
Expand All @@ -38,7 +39,8 @@ public function boot()
DeleteMySQLUser::class,
LicenseCommand::class,
BackupCommand::class,
VersionCommand::class
VersionCommand::class,
LangCommand::class
]);
}

Expand Down
35 changes: 25 additions & 10 deletions src/FileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,32 @@ public function issetText($text)

public static function appendAfter($file, $word, $text)
{
if (file_exists($file)) {
$contents = file_get_contents($file);
$isset = strpos($contents, $text) !== false;
if (!$isset) {
$position = strpos($contents, $word);
if ($position !== false) {
$start = substr($contents, 0, $position + strlen($word));
$end = substr($contents, $position + strlen($word));
file_put_contents($file, $start . "\n" . $text . "\n" . $end);
if (file_exists($file)) {
$contents = file_get_contents($file);
$isset = strpos($contents, $text) !== false;
if (!$isset) {
$position = strpos($contents, $word);
if ($position !== false) {
$start = substr($contents, 0, $position + strlen($word));
$end = substr($contents, $position + strlen($word));
file_put_contents($file, $start . "\n" . $text . "\n" . $end);
}
}
}
}

public static function appendBefore($file, $word, $text)
{
if (file_exists($file)) {
$contents = file_get_contents($file);
$isset = strpos($contents, $text) !== false;
if (!$isset) {
$position = strpos($contents, $word);
if ($position !== false) {
$newContents = substr_replace($contents, "\n" . $text . "\n", $position, 0);
file_put_contents($file, $newContents);
}
}
}
}
}
}

0 comments on commit 2ba1d70

Please sign in to comment.