-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathlocale.php
108 lines (96 loc) · 3.47 KB
/
locale.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
session_set_cookie_params(['samesite' => 'Strict']);
session_start();
include_once 'config.php';
include_once 'db_pdo.php';
if ($OF_USE_LOCALES) {
$locale = null;
if (isset($_GET["lang"])) {
$lang = $_GET["lang"];
// We're accepting a random lang parameter... We should probably check it actually exists!
if (locale_exists($dbh, $lang)) {
$locale = $lang;
}
} elseif (($_SESSION["locale"] ?? null) !== null) {
// If it's already in the session and not null... it's probably ok...
$locale = $_SESSION["locale"];
}
if ($locale === null || $locale === '') {
// https://github.com/jpatokal/openflights/issues/1322
// If we've not already got a locale set (from $_SESSION or $_GET),
// see if we can use the HTTP ACCEPT_LANGUAGE header value
// TODO: We don't attempt any of the fallbacks...
$acceptLang = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
// We know we can do en_US, so don't do a DB lookup
if ($acceptLang === "en_US" || locale_exists($dbh, $acceptLang)) {
$locale = $acceptLang;
} else {
// Fallback incase we don't have a locale at this point
$locale = "en_US";
}
}
// Push whatever locale we've worked out back into the session
$_SESSION["locale"] = $locale;
$locale .= ".utf8";
setlocale(LC_ALL, $locale);
if (substr_count($_SERVER['SCRIPT_NAME'], '/') == 1) {
$path = ".";
} else {
$path = "..";
}
bindtextdomain("messages", $path . "/locale");
textdomain("messages");
} else {
$locale = "en_US.utf8";
// This probably isn't necessary, and the function should just be removed.
// But in modern packaged PHP (Debian and Ubuntu, at least), php-common includes
// gettext (though, potentially not enabled by default).
// Composer checks PHP extensions install time, but not run time.
// Composer requires ext-gettext to be installed anyway, but of course,
// it could be technically ignored by `--ignore-platform-reqs`...
if (!function_exists('_')) {
function _($string) {
return $string;
}
}
}
header("Content-type: text/html; charset=utf-8");
/**
* Check if we support a locale...
*
* @param $dbh PDO OpenFlights DB handler
* @param $locale string locale string
* @return bool
*/
function locale_exists($dbh, $locale) {
$sth = $dbh->prepare("SELECT * FROM locales WHERE locale = ?");
$sth->execute([$locale]);
return $sth->rowCount() === 1;
}
/**
* Generate select box (pulldown) with all known locales
* Box ID is "locale" and it triggers JS changeLocale() when selection is changed
*
* @param $dbh PDO OpenFlights DB handler
* @param $locale string Currently selected locale
*/
function locale_pulldown($dbh, $locale) {
global $OF_USE_LOCALES;
echo "<select id='locale' onChange='JavaScript:changeLocale()'>\n";
if ($OF_USE_LOCALES) {
$sql = "SELECT * FROM locales ORDER BY name ASC";
foreach ($dbh->query($sql) as $row) {
$selected = $row["locale"] . ".utf8" == $locale ? "SELECTED" : "";
printf(
"<option value='%s' %s>%s (%s)</option>\n",
$row["locale"],
$selected,
$row["name"],
substr($row["locale"], 0, 2)
);
}
} else {
echo "<option value='en_US' SELECTED>English</option>\n";
}
echo "</select>";
}