id/common/strings.php

109 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2024-06-28 22:41:18 +01:00
<?php
const LANGAUGES = [
[
"code" => 'en',
"name" => "English (Traditional)",
"flag" => "uk"
],
[
'code' => 'en_US',
"name" => 'English (Simplified)',
'flag' => 'usa'
],
[
'code' => 'en_UWU',
'name' => 'Cute English',
'flag' => 'owo'
],
[
'code' => 'ga',
'name' => 'Irish',
'flag' => 'ie'
],
[
'code' => 'nb_NO',
'name' => 'Norwegian Bokmål',
'flag' => 'no'
]
];
2024-07-03 14:20:33 +01:00
function get_string($key="generic.generic", $substitutes=[]) {
2024-06-28 22:41:18 +01:00
global $LANG;
$key_path = explode('.', $key);
$result = $LANG;
foreach ($key_path as $k) {
if (isset($result[$k])) {
$result = $result[$k];
} else {
return $key;
}
}
2024-07-03 14:20:33 +01:00
if (count($substitutes) > 0) {
foreach ($substitutes as $key => $substitute) {
$re = '/{{('. $key .')}}/';
$subst = $substitute;
$result = preg_replace($re, $subst, $result, 1);
}
}
2024-06-28 22:41:18 +01:00
return $result;
}
function get_language_code_based_on_browser_locale(): string
{
$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locales = [ // Converts locale to its respective language.
'en_GB' => 'en',
'en_IE' => 'en',
'en' => 'en',
];
if (array_key_exists($locale, $locales)) {
return $locales[$locale];
}
if (str_starts_with("en", $locale)) {
return "en";
}
return $locale;
}
2024-07-03 14:20:33 +01:00
function patch_lang($lang="en"): void
{
2024-06-28 22:41:18 +01:00
global $LANG, $DOC_ROOT;
$temp = $LANG;
if (file_exists("$DOC_ROOT/strings/$lang.php")) {
require_once("$DOC_ROOT/strings/$lang.php");
}
function merge_arrays($original, $new) {
foreach ($new as $key => $value) {
if (is_array($value)) {
if (!isset($original[$key])) {
$original[$key] = [];
}
$original[$key] = merge_arrays($original[$key], $value);
} else {
// Replace only if the value is not blank
if ($value !== '') {
$original[$key] = $value;
}
}
}
return $original;
}
$LANG = merge_arrays($temp, $LANG);
}