mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 18:59:00 +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\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
use pocketmine\lang\BaseLang;
|
use pocketmine\lang\BaseLang;
|
||||||
|
use pocketmine\lang\LanguageNotFoundException;
|
||||||
use pocketmine\lang\TextContainer;
|
use pocketmine\lang\TextContainer;
|
||||||
use pocketmine\level\biome\Biome;
|
use pocketmine\level\biome\Biome;
|
||||||
use pocketmine\level\format\io\LevelProvider;
|
use pocketmine\level\format\io\LevelProvider;
|
||||||
@ -1442,7 +1443,19 @@ class Server{
|
|||||||
define('pocketmine\DEBUG', (int) $this->getProperty("debug.level", 1));
|
define('pocketmine\DEBUG', (int) $this->getProperty("debug.level", 1));
|
||||||
|
|
||||||
$this->forceLanguage = (bool) $this->getProperty("settings.force-language", false);
|
$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()]));
|
$this->logger->info($this->getLanguage()->translateString("language.selected", [$this->getLanguage()->getName(), $this->getLanguage()->getLang()]));
|
||||||
|
|
||||||
if(\pocketmine\IS_DEVELOPMENT_BUILD){
|
if(\pocketmine\IS_DEVELOPMENT_BUILD){
|
||||||
|
@ -23,12 +23,16 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\lang;
|
namespace pocketmine\lang;
|
||||||
|
|
||||||
use pocketmine\utils\MainLogger;
|
|
||||||
|
|
||||||
class BaseLang{
|
class BaseLang{
|
||||||
|
|
||||||
public const FALLBACK_LANGUAGE = "eng";
|
public const FALLBACK_LANGUAGE = "eng";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws LanguageNotFoundException
|
||||||
|
*/
|
||||||
public static function getLanguageList(string $path = "") : array{
|
public static function getLanguageList(string $path = "") : array{
|
||||||
if($path === ""){
|
if($path === ""){
|
||||||
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
||||||
@ -45,10 +49,10 @@ class BaseLang{
|
|||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach($files as $file){
|
foreach($files as $file){
|
||||||
$strings = [];
|
$code = explode(".", $file)[0];
|
||||||
self::loadLang($path . $file, $strings);
|
$strings = self::loadLang($path, $code);
|
||||||
if(isset($strings["language.name"])){
|
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 */
|
/** @var string */
|
||||||
@ -67,20 +71,22 @@ class BaseLang{
|
|||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
protected $fallbackLang = [];
|
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){
|
public function __construct(string $lang, string $path = null, string $fallback = self::FALLBACK_LANGUAGE){
|
||||||
|
|
||||||
$this->langName = strtolower($lang);
|
$this->langName = strtolower($lang);
|
||||||
|
|
||||||
if($path === null){
|
if($path === null){
|
||||||
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
$path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!self::loadLang($file = $path . $this->langName . ".ini", $this->lang)){
|
$this->lang = self::loadLang($path, $this->langName);
|
||||||
MainLogger::getLogger()->error("Missing required language file $file");
|
$this->fallbackLang = self::loadLang($path, $fallback);
|
||||||
}
|
|
||||||
if(!self::loadLang($file = $path . $fallback . ".ini", $this->fallbackLang)){
|
|
||||||
MainLogger::getLogger()->error("Missing required language file $file");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName() : string{
|
public function getName() : string{
|
||||||
@ -91,13 +97,13 @@ class BaseLang{
|
|||||||
return $this->langName;
|
return $this->langName;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function loadLang(string $path, array &$d){
|
protected static function loadLang(string $path, string $languageCode) : array{
|
||||||
if(file_exists($path)){
|
$file = $path . $languageCode . ".ini";
|
||||||
$d = array_map('stripcslashes', parse_ini_file($path, false, INI_SCANNER_RAW));
|
if(file_exists($file)){
|
||||||
return true;
|
return array_map('stripcslashes', parse_ini_file($file, false, INI_SCANNER_RAW));
|
||||||
}else{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
namespace pocketmine\wizard;
|
||||||
|
|
||||||
use pocketmine\lang\BaseLang;
|
use pocketmine\lang\BaseLang;
|
||||||
|
use pocketmine\lang\LanguageNotFoundException;
|
||||||
use pocketmine\utils\Config;
|
use pocketmine\utils\Config;
|
||||||
use pocketmine\utils\Internet;
|
use pocketmine\utils\Internet;
|
||||||
|
|
||||||
@ -47,8 +48,9 @@ class SetupWizard{
|
|||||||
public function run() : bool{
|
public function run() : bool{
|
||||||
$this->message(\pocketmine\NAME . " set-up wizard");
|
$this->message(\pocketmine\NAME . " set-up wizard");
|
||||||
|
|
||||||
$langs = BaseLang::getLanguageList();
|
try{
|
||||||
if(empty($langs)){
|
$langs = BaseLang::getLanguageList();
|
||||||
|
}catch(LanguageNotFoundException $e){
|
||||||
$this->error("No language files found, please use provided builds or clone the repository recursively.");
|
$this->error("No language files found, please use provided builds or clone the repository recursively.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user