personal-website/utils/localization.php

105 lines
2.9 KiB
PHP
Raw Normal View History

2021-11-28 12:48:45 +00:00
<?php
2023-01-10 14:54:42 +00:00
/////////////////////////////////////////////////
// Avaliable variables after importing this file
2021-11-28 12:48:45 +00:00
/////////////////////////////////////////////////
// The actual language being used
$lang = null;
// Avaliable detected languages
$langs = [];
// Function that gets the strings
2023-01-10 14:54:42 +00:00
$trans = function () {
return 'N/A';
};
2021-11-28 12:48:45 +00:00
/* I use functions to create scopes. Anything below
* shouldn't be accesible from outside
*/
/* -------------------------------------------------*/
2023-01-10 14:54:42 +00:00
$__langfunc = function () use (&$lang, &$langs, &$trans) {
2021-11-28 12:48:45 +00:00
2023-01-10 14:54:42 +00:00
///////////////
/// Get basedir
2021-11-28 12:48:45 +00:00
///////////////
// DOCUMENT_ROOT makes sure we always get the right folder
$basedir = sprintf('%s/lang', $_SERVER['DOCUMENT_ROOT']);
2023-01-10 14:54:42 +00:00
///////////////////////////////////////
/// Get the languages we have avaliable
2021-11-28 12:48:45 +00:00
///////////////////////////////////////
// To get the langs we scan the folder with the ini files
$langs = scandir($basedir);
2023-01-10 14:54:42 +00:00
// Filter for those that have two letters and end in ".ini"
$langs = array_filter($langs, fn ($x) => preg_match('/.*\.ini/', $x) === 1);
2021-11-28 12:48:45 +00:00
// And we remove the ".ini" part
2023-01-10 14:54:42 +00:00
$langs = array_map(fn ($x) => substr($x, 0, 2), $langs);
2021-11-28 12:48:45 +00:00
2023-01-10 14:54:42 +00:00
////////////////////////////////////
/// Get the optimal language to use
2021-11-28 12:48:45 +00:00
////////////////////////////////////
/* Try to get the language from browser-sent headers. The
* format for the header is like this: "en_US,es;q=0.5,ar;q=0.1".
* q=N represents the weight of preference, no q means q=1.0.
*/
2023-01-10 14:54:42 +00:00
if ($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? false) {
2021-11-28 12:48:45 +00:00
// Divide language string per language
$http_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
// Sanitize lang string and cast weight
2023-01-10 14:54:42 +00:00
$http_langs = array_map(function ($x) {
$x = explode(';', $x);
2021-11-28 12:48:45 +00:00
$x[0] = strtolower(substr($x[0], 0, 2));
2023-01-10 14:54:42 +00:00
$x[1] = (float)(explode('=', $x[1] ?? 'q=1.0')[1] ?? 1.0);
2021-11-28 12:48:45 +00:00
return $x;
}, $http_langs);
// Sort by weight
2023-01-10 14:54:42 +00:00
usort($http_langs, function ($a, $b) {
if ($a[1] === $b[1]) return 0;
2021-11-28 12:48:45 +00:00
return ($a[1] > $b[1]) ? -1 : 1;
});
// Create new neat array like this: ['en', 'es', 'ar']
$user_langs = [];
2023-01-10 14:54:42 +00:00
array_walk($http_langs, function ($arr) use (&$user_langs) {
if (!in_array($arr[0], $user_langs)) $user_langs[] = $arr[0];
2021-11-28 12:48:45 +00:00
});
// Assign the first match
2023-01-10 14:54:42 +00:00
foreach ($user_langs as $ilang) {
if (in_array($ilang, $langs)) {
2021-11-28 12:48:45 +00:00
$lang = $ilang;
break;
}
}
}
// Manully selected lang > HTTP lang > fallback lang
2023-01-10 14:54:42 +00:00
$lang = ($_GET['lang'] ?? null) ?: $lang ?: 'en';
2021-11-28 12:48:45 +00:00
2023-01-10 14:54:42 +00:00
///////////////////////////////////////////////////
/// Get file and build function to get the strings
2021-11-28 12:48:45 +00:00
///////////////////////////////////////////////////
$text = parse_ini_file("$basedir/$lang.ini", true);
2023-01-10 14:54:42 +00:00
$trans = function (string $index) use ($text): string {
// return htmlspecialchars($text[$index] ?? 'N/A', ENT_QUOTES);
return $text[$index] ?? 'N/A';
2021-11-28 12:48:45 +00:00
};
};
// Burn after reading
$__langfunc();
unset($__langfunc);