-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocalize.php
81 lines (76 loc) · 3.19 KB
/
localize.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/* This plugin translates default English strings into another language and provides
Customize options for switching display language and time zone. */
$languages = [];
foreach (glob("localizations/*.php") as $file) {
$languages[] = basename($file, ".php");
}
// English is hardcoded and so always present. If en.php is missing then
// default strings are taken as is. If en.php is present then some strings
// may be overwritten in this installation.
if (!in_array("en", $languages)) {
$languages[] = "en";
}
$context->hooks->register("start", function () use ($languages) {
$accept = $this->request["lang"] ?? $this->cookie("kwvlang") ?? $this->server["HTTP_ACCEPT_LANGUAGE"] ?? "";
if (preg_match_all('/\w\w(-\w\w)?/', strtolower($accept), $matches)) {
foreach ($matches[0] as $lang) {
if (strlen($lang) > 2 && !in_array($lang, $languages)) {
// ru-RU -> ru
$lang = substr($lang, 0, 2);
}
if (in_array($lang, $languages)) {
$strings = is_file($file = "localizations/$lang.php")
? require "localizations/$lang.php" : [];
$this->language = $lang;
$this->locale = strncasecmp(PHP_OS, "win", 3)
? (isset($strings["localeUnix"]) ? $strings["localeUnix"].".UTF-8" : $this->locale)
: ($strings["localeWindows"] ?? $this->locale);
$this->tz = $this->request["tz"] ?? $this->cookie("kwvtz") ?? $this->config["locale.defaultTZ"] ?? $strings["tz"] ?? $this->tz;
// Unlike other filters, locale and TZ should be passed to other pages, e.g. viewCard.
$this->cookie("kwvlang", $this->language);
$this->cookie("kwvtz", $this->tz);
break;
}
}
}
});
$context->hooks->register("echo_empty", function (array &$vars) {
$tz = &$vars["bodyAttributes"]["data-tz"];
$tz = $this->tz;
});
$context->hooks->register("translate", function (...$args) {
$file = "localizations/$this->language.php";
if (preg_match('/^[\w-]+$/', $this->language) && is_file($file)) {
$strings = require $file;
$args[0] = $strings[$args[0]] ?? $args[0];
}
return sprintf(...$args);
});
$context->hooks->registerFirst("echo_boardCustomize", function () use ($languages) {
if ($this->profileID) {
// Don't use timezone_abbreviations_list() because it contains duplicates
// (returns all zones used historically for a location).
$zones = timezone_identifiers_list();
?>
<?php if (count($languages) > 1) {?>
<tr>
<th class="tbl__th"><?=$this("Language (%s):", "language")?></th>
<td>
<select name="lang">
<?=Kanbani\htmlOptions($languages, $languages, $this->language)?>
</select>
</td>
</tr>
<?php }?>
<tr>
<th class="tbl__th"><?=$this("Time zone:")?></th>
<td>
<select name="tz">
<?=Kanbani\htmlOptions($zones, $zones, $this->tz)?>
</select>
</td>
</tr>
<?php
}
});