|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Lang |
|
6
|
|
|
{ |
|
7
|
|
|
private static string $language = 'en'; // Default language |
|
8
|
|
|
private static array $messages = []; |
|
9
|
|
|
private static string $langPath = __DIR__ . '/../locale'; // Path to language files |
|
10
|
|
|
private static string $appLangPath = __DIR__ . '/../../app/locale'; // Path to language files |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Set the language to use. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $language |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function setLanguage(string $language): void |
|
19
|
|
|
{ |
|
20
|
|
|
self::$language = $language; |
|
21
|
|
|
self::loadLanguage(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Load the language file into memory. |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
private static function loadLanguage(): void |
|
30
|
|
|
{ |
|
31
|
|
|
self::$messages = []; |
|
32
|
|
|
foreach ([self::$langPath, self::$appLangPath] as $langPath) { |
|
33
|
|
|
$langDir = $langPath . '/' . self::$language; |
|
34
|
|
|
$files = glob($langDir . '/*.php'); |
|
35
|
|
|
foreach ($files as $filePath) { |
|
36
|
|
|
$messages = include $filePath; |
|
37
|
|
|
self::$messages = array_merge(self::$messages, $messages); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a specific message by key. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $key The message key. |
|
46
|
|
|
* @param string $default The default message if the key does not exist. |
|
47
|
|
|
* @param array $replace Variables to replace in the message. |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function get(string $key, string $default = '', array $replace = []): string |
|
51
|
|
|
{ |
|
52
|
|
|
if ($default === '') { |
|
53
|
|
|
$default = ucfirst(str_replace('_', ' ', $key)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$key = strtolower($key); |
|
57
|
|
|
$message = self::$messages[$key] ?? $default; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($replace as $search => $value) { |
|
60
|
|
|
$message = str_replace(':' . $search, $value, $message); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $message; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|