GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
BaseLang Class Reference

Public Member Functions

 __construct ($lang, $path=null, $fallback=self::FALLBACK_LANGUAGE)
 
 getName ()
 
 getLang ()
 
 translateString ($str, array $params=[], $onlyPrefix=null)
 
 translate (TextContainer $c)
 
 internalGet ($id)
 
 get ($id)
 

Data Fields

const FALLBACK_LANGUAGE = "eng"
 

Protected Member Functions

 loadLang ($path, array &$d)
 
 parseTranslation ($text, $onlyPrefix=null)
 

Protected Attributes

 $langName
 
 $lang = []
 
 $fallbackLang = []
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $lang,
  $path = null,
  $fallback = self::FALLBACK_LANGUAGE 
)

BaseLang constructor.

Parameters
$lang
null$path
string$fallback
43  {
44 
45  $this->langName = strtolower($lang);
46 
47  if($path === null){
48  $path = \pocketmine\PATH . "src/pocketmine/lang/locale/";
49  }
50 
51  $this->loadLang($path . $this->langName . ".ini", $this->lang);
52  $this->loadLang($path . $fallback . ".ini", $this->fallbackLang);
53  }

Member Function Documentation

◆ get()

get (   $id)
Parameters
$id
Returns
mixed
156  {
157  if(isset($this->lang[$id])){
158  return $this->lang[$id];
159  }elseif(isset($this->fallbackLang[$id])){
160  return $this->fallbackLang[$id];
161  }
162 
163  return $id;
164  }

◆ getLang()

getLang ( )
Returns
string
65  {
66  return $this->langName;
67  }

◆ getName()

getName ( )
Returns
string
58  : string{
59  return $this->get("language.name");
60  }

◆ internalGet()

internalGet (   $id)
Parameters
$id
Returns
mixed|null
141  {
142  if(isset($this->lang[$id])){
143  return $this->lang[$id];
144  }elseif(isset($this->fallbackLang[$id])){
145  return $this->fallbackLang[$id];
146  }
147 
148  return null;
149  }

◆ loadLang()

loadLang (   $path,
array &  $d 
)
protected
Parameters
$path
array$d
73  {
74  if(file_exists($path) and strlen($content = file_get_contents($path)) > 0){
75  foreach(explode("\n", $content) as $line){
76  $line = trim($line);
77  if($line === "" or $line{0} === "#"){
78  continue;
79  }
80 
81  $t = explode("=", $line);
82  if(count($t) < 2){
83  continue;
84  }
85 
86  $key = trim(array_shift($t));
87  $value = trim(implode("=", $t));
88 
89  if($value === ""){
90  continue;
91  }
92 
93  $d[$key] = $value;
94  }
95  }
96  }

◆ parseTranslation()

parseTranslation (   $text,
  $onlyPrefix = null 
)
protected
Parameters
$text
null$onlyPrefix
Returns
string
172  {
173  $newString = "";
174 
175  $replaceString = null;
176 
177  $len = strlen($text);
178  for($i = 0; $i < $len; ++$i){
179  $c = $text{$i};
180  if($replaceString !== null){
181  $ord = ord($c);
182  if(
183  ($ord >= 0x30 and $ord <= 0x39) // 0-9
184  or ($ord >= 0x41 and $ord <= 0x5a) // A-Z
185  or ($ord >= 0x61 and $ord <= 0x7a) or // a-z
186  $c === "." or $c === "-"
187  ){
188  $replaceString .= $c;
189  }else{
190  if(($t = $this->internalGet(substr($replaceString, 1))) !== null and ($onlyPrefix === null or strpos($replaceString, $onlyPrefix) === 1)){
191  $newString .= $t;
192  }else{
193  $newString .= $replaceString;
194  }
195  $replaceString = null;
196 
197  if($c === "%"){
198  $replaceString = $c;
199  }else{
200  $newString .= $c;
201  }
202  }
203  }elseif($c === "%"){
204  $replaceString = $c;
205  }else{
206  $newString .= $c;
207  }
208  }
209 
210  if($replaceString !== null){
211  if(($t = $this->internalGet(substr($replaceString, 1))) !== null and ($onlyPrefix === null or strpos($replaceString, $onlyPrefix) === 1)){
212  $newString .= $t;
213  }else{
214  $newString .= $replaceString;
215  }
216  }
217 
218  return $newString;
219  }

◆ translate()

translate ( TextContainer  $c)
Parameters
TextContainer$c
Returns
mixed|null|string
121  {
122  if($c instanceof TranslationContainer){
123  $baseText = $this->internalGet($c->getText());
124  $baseText = $this->parseTranslation($baseText !== null ? $baseText : $c->getText());
125 
126  foreach($c->getParameters() as $i => $p){
127  $baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
128  }
129  }else{
130  $baseText = $this->parseTranslation($c->getText());
131  }
132 
133  return $baseText;
134  }

◆ translateString()

translateString (   $str,
array  $params = [],
  $onlyPrefix = null 
)
Parameters
string$str
string[]$params
string | null$onlyPrefix
Returns
string
105  {
106  $baseText = $this->get($str);
107  $baseText = $this->parseTranslation(($baseText !== null and ($onlyPrefix === null or strpos($str, $onlyPrefix) === 0)) ? $baseText : $str, $onlyPrefix);
108 
109  foreach($params as $i => $p){
110  $baseText = str_replace("{%$i}", $this->parseTranslation((string) $p), $baseText, $onlyPrefix);
111  }
112 
113  return str_replace("%0", "", $baseText); //fixes a client bug where %0 in translation will cause freeze
114  }

Field Documentation

◆ $fallbackLang

$fallbackLang = []
protected

◆ $lang

$lang = []
protected

◆ $langName

$langName
protected

◆ FALLBACK_LANGUAGE

const FALLBACK_LANGUAGE = "eng"

The documentation for this class was generated from the following file: