mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 19:02:59 +00:00
Support for nested TranslationContainers
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -130,14 +130,15 @@ class Language{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param (float|int|string)[] $params
|
||||
* @param (float|int|string|TranslationContainer)[] $params
|
||||
*/
|
||||
public function translateString(string $str, array $params = [], ?string $onlyPrefix = null) : string{
|
||||
$baseText = $this->get($str);
|
||||
$baseText = $this->parseTranslation(($onlyPrefix === null or strpos($str, $onlyPrefix) === 0) ? $baseText : $str, $onlyPrefix);
|
||||
|
||||
foreach($params as $i => $p){
|
||||
$baseText = str_replace("{%$i}", $this->parseTranslation((string) $p), $baseText);
|
||||
$replacement = $p instanceof TranslationContainer ? $this->translate($p) : $this->parseTranslation((string) $p);
|
||||
$baseText = str_replace("{%$i}", $replacement, $baseText);
|
||||
}
|
||||
|
||||
return $baseText;
|
||||
@ -148,7 +149,8 @@ class Language{
|
||||
$baseText = $this->parseTranslation($baseText ?? $c->getText());
|
||||
|
||||
foreach($c->getParameters() as $i => $p){
|
||||
$baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
|
||||
$replacement = $p instanceof TranslationContainer ? $this->translate($p) : $this->parseTranslation($p);
|
||||
$baseText = str_replace("{%$i}", $replacement, $baseText);
|
||||
}
|
||||
|
||||
return $baseText;
|
||||
|
@ -27,17 +27,21 @@ final class TranslationContainer{
|
||||
|
||||
/** @var string $text */
|
||||
protected $text;
|
||||
/** @var string[] $params */
|
||||
/** @var string[]|TranslationContainer[] $params */
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* @param (float|int|string)[] $params
|
||||
* @param (float|int|string|TranslationContainer)[] $params
|
||||
*/
|
||||
public function __construct(string $text, array $params = []){
|
||||
$this->text = $text;
|
||||
|
||||
foreach($params as $k => $str){
|
||||
$this->params[$k] = (string) $str;
|
||||
foreach($params as $k => $param){
|
||||
if(!($param instanceof TranslationContainer)){
|
||||
$this->params[$k] = (string) $param;
|
||||
}else{
|
||||
$this->params[$k] = $param;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,13 +50,13 @@ final class TranslationContainer{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @return string[]|TranslationContainer[]
|
||||
*/
|
||||
public function getParameters() : array{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
public function getParameter(int|string $i) : ?string{
|
||||
public function getParameter(int|string $i) : TranslationContainer|string|null{
|
||||
return $this->params[$i] ?? null;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user