-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
65 lines (48 loc) · 1.84 KB
/
index.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
<?php
require "vendor/autoload.php";
use Gettext\Translator;
use Gettext\TranslatorFunctions;
$translator = new Translator();
if (isset($_GET["lang"]))
$lang = $_GET["lang"]; //Detecting language from URL
else if (isset($_COOKIE["language"]))
$lang = $_COOKIE["language"]; //Detecting language from cookie
else
$lang = substr(locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2); //Detecting language from browser
//Supported languages
$languages = [
"en" => "English",
"fr" => "Français"
];
if (!in_array($lang, array_keys($languages))) {
$lang = 'en'; //Default language
}
if(!isset($_COOKIE['language']) || $_COOKIE['language'] != $lang)
setcookie("language", $lang, strtotime('+30 days')); //Setting a 30 days cookie for the selected language
$domain = "messages"; //Translation domain
$path = "locale/{$lang}/LC_MESSAGES/{$domain}.php"; //Translation files location
$translator->loadTranslations($path); //Loading the appropriate translation file
TranslatorFunctions::register($translator);
$value = 10;
?>
<!DOCTYPE html>
<!-- Setting the HTML lang attribute dynamically -->
<html lang="<?= $lang ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Gettext Template</title>
</head>
<body>
<!-- Translation of a simple string using gettext() -->
<h1><?= __("Hello world!"); ?></h1>
<!-- Translation of a plural string using sprintf() and ngettext() -->
<p><?= sprintf(/* Translators: %d is the number of items */n__("There is %d item", "There are %d items", $value), $value, $value); ?></p>
<!-- Listing available languages -->
<ul>
<?php foreach ($languages as $l => $name) { ?>
<li><a href="?lang=<?= $l; ?>" hreflang="<?= $l; ?>"><?= $name; ?></a></li>
<?php } ?>
</ul>
</body>
</html>