mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 10:49:10 +00:00
Clean up BaseLang error handling, throw exceptions instead
This commit is contained in:
parent
cab2d52ff8
commit
d26631d8e0
@ -46,6 +46,7 @@ use pocketmine\item\enchantment\Enchantment;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\lang\BaseLang;
|
||||
use pocketmine\lang\LanguageNotFoundException;
|
||||
use pocketmine\lang\TextContainer;
|
||||
use pocketmine\level\biome\Biome;
|
||||
use pocketmine\level\format\io\LevelProvider;
|
||||
@ -1442,7 +1443,19 @@ class Server{
|
||||
define('pocketmine\DEBUG', (int) $this->getProperty("debug.level", 1));
|
||||
|
||||
$this->forceLanguage = (bool) $this->getProperty("settings.force-language", false);
|
||||
$this->baseLang = new BaseLang($this->getProperty("settings.language", BaseLang::FALLBACK_LANGUAGE));
|
||||
$selectedLang = $this->getProperty("settings.language", BaseLang::FALLBACK_LANGUAGE);
|
||||
try{
|
||||
$this->baseLang = new BaseLang($selectedLang);
|
||||
}catch(LanguageNotFoundException $e){
|
||||
$this->logger->error($e->getMessage());
|
||||
try{
|
||||
$this->baseLang = new BaseLang(BaseLang::FALLBACK_LANGUAGE);
|
||||
}catch(LanguageNotFoundException $e){
|
||||
$this->logger->emergency("Fallback language \"" . BaseLang::FALLBACK_LANGUAGE . "\" not found");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->info($this->getLanguage()->translateString("language.selected", [$this->getLanguage()->getName(), $this->getLanguage()->getLang()]));
|
||||
|
||||
if(\pocketmine\IS_DEVELOPMENT_BUILD){
|
||||
|
@ -23,12 +23,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\lang;
|
||||
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
class BaseLang{
|
||||
|
||||
public const FALLBACK_LANGUAGE = "eng";
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return array
|
||||
* @throws LanguageNotFoundException
|
||||
*/
|
||||
public static function getLanguageList(string $path = "") : array{
|
||||
if($path === ""){
|
||||
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
||||
@ -45,10 +49,10 @@ class BaseLang{
|
||||
$result = [];
|
||||
|
||||
foreach($files as $file){
|
||||
$strings = [];
|
||||
self::loadLang($path . $file, $strings);
|
||||
$code = explode(".", $file)[0];
|
||||
$strings = self::loadLang($path, $code);
|
||||
if(isset($strings["language.name"])){
|
||||
$result[substr($file, 0, -4)] = $strings["language.name"];
|
||||
$result[$code] = $strings["language.name"];
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,7 +60,7 @@ class BaseLang{
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
throw new LanguageNotFoundException("Language directory $path does not exist or is not a directory");
|
||||
}
|
||||
|
||||
/** @var string */
|
||||
@ -67,20 +71,22 @@ class BaseLang{
|
||||
/** @var string[] */
|
||||
protected $fallbackLang = [];
|
||||
|
||||
/**
|
||||
* @param string $lang
|
||||
* @param string|null $path
|
||||
* @param string $fallback
|
||||
*
|
||||
* @throws LanguageNotFoundException
|
||||
*/
|
||||
public function __construct(string $lang, string $path = null, string $fallback = self::FALLBACK_LANGUAGE){
|
||||
|
||||
$this->langName = strtolower($lang);
|
||||
|
||||
if($path === null){
|
||||
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
||||
}
|
||||
|
||||
if(!self::loadLang($file = $path . $this->langName . ".ini", $this->lang)){
|
||||
MainLogger::getLogger()->error("Missing required language file $file");
|
||||
}
|
||||
if(!self::loadLang($file = $path . $fallback . ".ini", $this->fallbackLang)){
|
||||
MainLogger::getLogger()->error("Missing required language file $file");
|
||||
}
|
||||
$this->lang = self::loadLang($path, $this->langName);
|
||||
$this->fallbackLang = self::loadLang($path, $fallback);
|
||||
}
|
||||
|
||||
public function getName() : string{
|
||||
@ -91,13 +97,13 @@ class BaseLang{
|
||||
return $this->langName;
|
||||
}
|
||||
|
||||
protected static function loadLang(string $path, array &$d){
|
||||
if(file_exists($path)){
|
||||
$d = array_map('stripcslashes', parse_ini_file($path, false, INI_SCANNER_RAW));
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
protected static function loadLang(string $path, string $languageCode) : array{
|
||||
$file = $path . $languageCode . ".ini";
|
||||
if(file_exists($file)){
|
||||
return array_map('stripcslashes', parse_ini_file($file, false, INI_SCANNER_RAW));
|
||||
}
|
||||
|
||||
throw new LanguageNotFoundException("Language \"$languageCode\" not found");
|
||||
}
|
||||
|
||||
/**
|
||||
|
28
src/pocketmine/lang/LanguageNotFoundException.php
Normal file
28
src/pocketmine/lang/LanguageNotFoundException.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\lang;
|
||||
|
||||
class LanguageNotFoundException extends \RuntimeException{
|
||||
|
||||
}
|
@ -28,6 +28,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\wizard;
|
||||
|
||||
use pocketmine\lang\BaseLang;
|
||||
use pocketmine\lang\LanguageNotFoundException;
|
||||
use pocketmine\utils\Config;
|
||||
use pocketmine\utils\Internet;
|
||||
|
||||
@ -47,8 +48,9 @@ class SetupWizard{
|
||||
public function run() : bool{
|
||||
$this->message(\pocketmine\NAME . " set-up wizard");
|
||||
|
||||
$langs = BaseLang::getLanguageList();
|
||||
if(empty($langs)){
|
||||
try{
|
||||
$langs = BaseLang::getLanguageList();
|
||||
}catch(LanguageNotFoundException $e){
|
||||
$this->error("No language files found, please use provided builds or clone the repository recursively.");
|
||||
return false;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user