This repository was archived by the owner on Jul 31, 2025. It is now read-only.
forked from dkaser/unraid-tailscale-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.php
More file actions
56 lines (46 loc) · 1.76 KB
/
Translator.php
File metadata and controls
56 lines (46 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Tailscale;
class Translator
{
/** @var array<string, string> $lang */
private array $lang;
/**
* @return array<string, string>
* @param array<mixed,mixed> $array
*/
private static function flattenArray(array $array, string $keys = ""): array
{
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, self::flattenArray($value, $keys . $key . "."));
} else {
if (is_string($value) && ! empty($value)) {
$result[$keys . $key] = $value;
}
}
}
return $result;
}
public function __construct()
{
global $login_locale;
if ( ! defined(__NAMESPACE__ . "\PLUGIN_ROOT")) {
throw new \RuntimeException("PLUGIN_ROOT not defined");
}
$dynamix = parse_ini_file('/boot/config/plugins/dynamix/dynamix.cfg', true) ?: array();
$locale = $_SESSION['locale'] ?? ($login_locale ?? ($dynamix['display']['locale'] ?? "none"));
$plugin_locale = (array) json_decode(file_get_contents(PLUGIN_ROOT . "/locales/en_US.json") ?: "{}", true);
$plugin_lang = self::flattenArray($plugin_locale);
if (file_exists(PLUGIN_ROOT . "/locales/{$locale}.json")) {
$current_locale = (array) json_decode(file_get_contents(PLUGIN_ROOT . "/locales/{$locale}.json") ?: "{}", true);
$current_lang = self::flattenArray($current_locale);
$plugin_lang = array_replace($plugin_lang, $current_lang);
}
$this->lang = $plugin_lang;
}
public function tr(string $message): string
{
return $this->lang[strtolower($message)];
}
}