Clean up BaseLang error handling, throw exceptions instead

This commit is contained in:
Dylan K. Taylor 2018-07-30 17:00:52 +01:00
parent cab2d52ff8
commit d26631d8e0
4 changed files with 71 additions and 22 deletions

View File

@ -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){

View File

@ -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");
}
/**

View 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{
}

View File

@ -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");
try{
$langs = BaseLang::getLanguageList();
if(empty($langs)){
}catch(LanguageNotFoundException $e){
$this->error("No language files found, please use provided builds or clone the repository recursively.");
return false;
}