Skip to content

Commit

Permalink
Supports multi-languages
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 1, 2023
1 parent 20044e7 commit ce5225e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 13 deletions.
2 changes: 2 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
availableLanguages: ["en_us"] # DON'T TOUCH THIS LINE
lang: en_us
5 changes: 5 additions & 0 deletions resources/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"message.cache.running": "§eTask is running! Please wait until it is finished",
"message.cache.failed": "§cCould not get Poggit plugins list from Poggit: {%reason}",
"message.cache.successfully": "§a({%count}) Poggit plugins has been cached successfully"
}
51 changes: 46 additions & 5 deletions src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,54 @@

namespace thebigcrafter\omp;

use pocketmine\lang\Language;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\SingletonTrait;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\Iodine\Iodine;
use thebigcrafter\omp\utils\Internet;
use thebigcrafter\omp\lang\OMPLanguage;
use thebigcrafter\Sulfur\Path;

final class OhMyPMMP extends PluginBase
{
use SingletonTrait;

private OMPLanguage $language;

public function onLoad(): void
{
self::setInstance($this);
}

public function onEnable(): void
{
$this->saveDefaultConfig();
$this->loadLanguage();

final class OhMyPMMP extends PluginBase {
public function onEnable() : void {
Fluorine::run();
}
}

public function loadLanguage(): void
{
$langFolder = Path::join($this->getDataFolder(), "lang");
$selectedLanguage = $this->getConfig()->get("lang");
$selectedLanguagePath = Path::join($this->getDataFolder(), "lang", "$selectedLanguage.json");

if (!is_dir($langFolder)) {
@mkdir($langFolder);
}

/** @var string $lang */
foreach ((array) $this->getConfig()->get("availableLanguages") as $lang) {
if (!is_file($lang)) {
$this->saveResource(Path::join("lang/", "$lang.json"));
}
}

OMPLanguage::loadLanguageFile($selectedLanguage, $selectedLanguagePath);
$this->language = new OMPLanguage($selectedLanguage);
}

public function getLanguage() {
return $this->language;
}
}
7 changes: 7 additions & 0 deletions src/thebigcrafter/omp/commands/OMPCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\omp\commands;

class OMPCommand {}
52 changes: 52 additions & 0 deletions src/thebigcrafter/omp/lang/OMPLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\omp\lang;

use Exception;
use thebigcrafter\Sulfur\Filesystem;

class OMPLanguage
{
/** @var array<string, string> $languages */
private static array $languages;

private $default;

public static function loadLanguageFile(string $name, string $path)
{
if (!file_exists($path)) {
throw new Exception('File not found.');
}

self::$languages[$name] = json_decode(\thebigcrafter\omp\utils\Filesystem::readFile($path)->await() ?: '', true);
}

public function __construct(string $default)
{
if (!array_key_exists($default, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $default;
}

public function setDefaultLanguage(string $name)
{
if (!array_key_exists($name, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $name;
}

public function get(string $key)
{
if (!array_key_exists($key, self::$languages[$this->default])) {
throw new Exception("$key not found");
}

return self::$languages[$this->default][$key];
}
}
25 changes: 25 additions & 0 deletions src/thebigcrafter/omp/utils/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\omp\utils;

use Exception;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\Iodine\DeferredFuture;
use thebigcrafter\Iodine\Iodine;
use thebigcrafter\Sulfur\Path;

class Filesystem
{
public static function readFile(string $path)
{
if (!file_exists($path)) {
throw new Exception("File not found");
}

return Iodine::async(function () use ($path) {
return file_get_contents($path);
});
}
}
12 changes: 4 additions & 8 deletions src/thebigcrafter/omp/utils/Internet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@
use thebigcrafter\Iodine\DeferredFuture;
use thebigcrafter\Iodine\Iodine;

class Internet
final class Internet
{
public static function fetch(string $url)
{
$deferred = new DeferredFuture();

Fluorine::defer(function () use ($deferred, $url) {
return Iodine::async(function () use ($url) {
try {
$res = \pocketmine\utils\Internet::getURL($url);

$deferred->complete($res->getBody());
return ($res->getBody());
} catch (InternetException $e) {
$deferred->error($e);
throw $e;
}
});

return $deferred->getFuture();
}

public static function getRemoteFileSize(string $url)
Expand Down

0 comments on commit ce5225e

Please sign in to comment.