PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
Language.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\lang;
25
27use Symfony\Component\Filesystem\Path;
28use function array_filter;
29use function array_map;
30use function count;
31use function explode;
32use function file_exists;
33use function is_dir;
34use function ord;
35use function parse_ini_file;
36use function scandir;
37use function str_ends_with;
38use function str_replace;
39use function str_starts_with;
40use function strlen;
41use function strpos;
42use function strtolower;
43use function substr;
44use const INI_SCANNER_RAW;
45use const SCANDIR_SORT_NONE;
46
48
49 public const FALLBACK_LANGUAGE = "eng";
50
57 public static function getLanguageList(string $path = "") : array{
58 if($path === ""){
59 $path = \pocketmine\LOCALE_DATA_PATH;
60 }
61
62 if(is_dir($path)){
63 $allFiles = scandir($path, SCANDIR_SORT_NONE);
64
65 if($allFiles !== false){
66 $files = array_filter($allFiles, function(string $filename) : bool{
67 return str_ends_with($filename, ".ini");
68 });
69
70 $result = [];
71
72 foreach($files as $file){
73 try{
74 $code = explode(".", $file)[0];
75 $strings = self::loadLang($path, $code);
76 if(isset($strings[KnownTranslationKeys::LANGUAGE_NAME])){
77 $result[$code] = $strings[KnownTranslationKeys::LANGUAGE_NAME];
78 }
79 }catch(LanguageNotFoundException $e){
80 // no-op
81 }
82 }
83
84 return $result;
85 }
86 }
87
88 throw new LanguageNotFoundException("Language directory $path does not exist or is not a directory");
89 }
90
91 protected string $langName;
96 protected array $lang = [];
101 protected array $fallbackLang = [];
102
106 public function __construct(string $lang, ?string $path = null, string $fallback = self::FALLBACK_LANGUAGE){
107 $this->langName = strtolower($lang);
108
109 if($path === null){
110 $path = \pocketmine\LOCALE_DATA_PATH;
111 }
112
113 $this->lang = self::loadLang($path, $this->langName);
114 $this->fallbackLang = self::loadLang($path, $fallback);
115 }
116
117 public function getName() : string{
118 return $this->get(KnownTranslationKeys::LANGUAGE_NAME);
119 }
120
121 public function getLang() : string{
122 return $this->langName;
123 }
124
129 protected static function loadLang(string $path, string $languageCode) : array{
130 $file = Path::join($path, $languageCode . ".ini");
131 if(file_exists($file)){
132 $strings = array_map('stripcslashes', Utils::assumeNotFalse(parse_ini_file($file, false, INI_SCANNER_RAW), "Missing or inaccessible required resource files"));
133 if(count($strings) > 0){
134 return $strings;
135 }
136 }
137
138 throw new LanguageNotFoundException("Language \"$languageCode\" not found");
139 }
140
144 public function translateString(string $str, array $params = [], ?string $onlyPrefix = null) : string{
145 $baseText = ($onlyPrefix === null || str_starts_with($str, $onlyPrefix)) ? $this->internalGet($str) : null;
146 if($baseText === null){ //key not found, embedded inside format string, or doesn't match prefix
147 $baseText = $this->parseTranslation($str, $onlyPrefix);
148 }
149
150 foreach(Utils::promoteKeys($params) as $i => $p){
151 $replacement = $p instanceof Translatable ? $this->translate($p) : (string) $p;
152 $baseText = str_replace("{%$i}", $replacement, $baseText);
153 }
154
155 return $baseText;
156 }
157
158 public function translate(Translatable $c) : string{
159 $baseText = $this->internalGet($c->getText());
160 if($baseText === null){ //key not found or embedded inside format string
161 $baseText = $this->parseTranslation($c->getText());
162 }
163
164 foreach(Utils::promoteKeys($c->getParameters()) as $i => $p){
165 $replacement = $p instanceof Translatable ? $this->translate($p) : $p;
166 $baseText = str_replace("{%$i}", $replacement, $baseText);
167 }
168
169 return $baseText;
170 }
171
172 protected function internalGet(string $id) : ?string{
173 return $this->lang[$id] ?? $this->fallbackLang[$id] ?? null;
174 }
175
176 public function get(string $id) : string{
177 return $this->internalGet($id) ?? $id;
178 }
179
184 public function getAll() : array{
185 return $this->lang;
186 }
187
201 protected function parseTranslation(string $text, ?string $onlyPrefix = null) : string{
202 $newString = "";
203
204 $replaceString = null;
205
206 $len = strlen($text);
207 for($i = 0; $i < $len; ++$i){
208 $c = $text[$i];
209 if($replaceString !== null){
210 $ord = ord($c);
211 if(
212 ($ord >= 0x30 && $ord <= 0x39) // 0-9
213 || ($ord >= 0x41 && $ord <= 0x5a) // A-Z
214 || ($ord >= 0x61 && $ord <= 0x7a) || // a-z
215 $c === "." || $c === "-"
216 ){
217 $replaceString .= $c;
218 }else{
219 if(($t = $this->internalGet(substr($replaceString, 1))) !== null && ($onlyPrefix === null || strpos($replaceString, $onlyPrefix) === 1)){
220 $newString .= $t;
221 }else{
222 $newString .= $replaceString;
223 }
224 $replaceString = null;
225
226 if($c === "%"){
227 $replaceString = $c;
228 }else{
229 $newString .= $c;
230 }
231 }
232 }elseif($c === "%"){
233 $replaceString = $c;
234 }else{
235 $newString .= $c;
236 }
237 }
238
239 if($replaceString !== null){
240 if(($t = $this->internalGet(substr($replaceString, 1))) !== null && ($onlyPrefix === null || strpos($replaceString, $onlyPrefix) === 1)){
241 $newString .= $t;
242 }else{
243 $newString .= $replaceString;
244 }
245 }
246
247 return $newString;
248 }
249}
parseTranslation(string $text, ?string $onlyPrefix=null)
Definition Language.php:201
static loadLang(string $path, string $languageCode)
Definition Language.php:129
static getLanguageList(string $path="")
Definition Language.php:57
__construct(string $lang, ?string $path=null, string $fallback=self::FALLBACK_LANGUAGE)
Definition Language.php:106
translateString(string $str, array $params=[], ?string $onlyPrefix=null)
Definition Language.php:144