Scrap TextContainer, make TranslationContainer immutable

TextContainer provided zero real value as a base of TranslationContainer, given that it required its own logic to be handled wherever accepted. As such, it's no better than a simple string.
Removing it also allows fixing an ambiguity when embedding translations inside other translations, allowing it to be made immutable.
This commit is contained in:
Dylan K. Taylor
2020-02-08 13:38:02 +00:00
parent 2375e9519d
commit 4c51f1dda3
11 changed files with 53 additions and 104 deletions

View File

@@ -142,16 +142,12 @@ class Language{
return $baseText;
}
public function translate(TextContainer $c) : string{
if($c instanceof TranslationContainer){
$baseText = $this->internalGet($c->getText());
$baseText = $this->parseTranslation($baseText ?? $c->getText());
public function translate(TranslationContainer $c) : string{
$baseText = $this->internalGet($c->getText());
$baseText = $this->parseTranslation($baseText ?? $c->getText());
foreach($c->getParameters() as $i => $p){
$baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
}
}else{
$baseText = $this->parseTranslation($c->getText());
foreach($c->getParameters() as $i => $p){
$baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
}
return $baseText;

View File

@@ -1,46 +0,0 @@
<?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 TextContainer{
/** @var string $text */
protected $text;
public function __construct(string $text){
$this->text = $text;
}
public function setText(string $text) : void{
$this->text = $text;
}
public function getText() : string{
return $this->text;
}
public function __toString() : string{
return $this->getText();
}
}

View File

@@ -23,8 +23,10 @@ declare(strict_types=1);
namespace pocketmine\lang;
class TranslationContainer extends TextContainer{
final class TranslationContainer{
/** @var string $text */
protected $text;
/** @var string[] $params */
protected $params = [];
@@ -32,7 +34,7 @@ class TranslationContainer extends TextContainer{
* @param (float|int|string)[] $params
*/
public function __construct(string $text, array $params = []){
parent::__construct($text);
$this->text = $text;
$i = 0;
foreach($params as $str){
@@ -42,6 +44,10 @@ class TranslationContainer extends TextContainer{
}
}
public function getText() : string{
return $this->text;
}
/**
* @return string[]
*/
@@ -52,4 +58,8 @@ class TranslationContainer extends TextContainer{
public function getParameter(int $i) : ?string{
return $this->params[$i] ?? null;
}
public function __toString() : string{
return $this->getText();
}
}