11 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Helper utility to interact with Locales.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class locale {
|
|
|
27 |
/**
|
|
|
28 |
* Wrap for the native PHP function setlocale().
|
|
|
29 |
*
|
|
|
30 |
* @param int $category Specifying the category of the functions affected by the locale setting.
|
|
|
31 |
* @param string $locale E.g.: en_AU.utf8, en_GB.utf8, es_ES.utf8, fr_FR.utf8, de_DE.utf8.
|
|
|
32 |
* @return string|false Returns the new current locale, or FALSE on error.
|
|
|
33 |
*/
|
|
|
34 |
public static function set_locale(int $category = LC_ALL, string $locale = '0'): string|false {
|
|
|
35 |
if (strlen($locale) <= 255 || PHP_OS_FAMILY === 'BSD' || PHP_OS_FAMILY === 'Darwin') {
|
|
|
36 |
// We can set the whole locale all together.
|
|
|
37 |
return setlocale($category, $locale);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// Too long locale with linux or windows, let's split it into known and supported categories.
|
|
|
41 |
$split = explode(';', self::standardise_locale($locale));
|
|
|
42 |
foreach ($split as $element) {
|
|
|
43 |
[$category, $value] = explode('=', $element);
|
|
|
44 |
if (defined($category)) { // Only if the category exists, there are OS differences.
|
|
|
45 |
setlocale(constant($category), $value);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// Finally, return the complete configured locale.
|
|
|
50 |
return self::get_locale();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Get the current locale.
|
|
|
55 |
*
|
|
|
56 |
* @param int $category
|
|
|
57 |
* @return string|false
|
|
|
58 |
*/
|
|
|
59 |
public static function get_locale(int $category = LC_ALL): string|false {
|
|
|
60 |
return setlocale($category, "0");
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Standardise a string-based locale, removing any deprecated locale categories and ordering it.
|
|
|
65 |
*
|
|
|
66 |
* @param string $locale
|
|
|
67 |
* @return string
|
|
|
68 |
*/
|
|
|
69 |
public static function standardise_locale(string $locale): string {
|
|
|
70 |
$locales = array_filter(
|
|
|
71 |
explode(';', $locale),
|
|
|
72 |
function ($locale): bool {
|
|
|
73 |
[$category, ] = explode('=', $locale);
|
|
|
74 |
return defined($category);
|
|
|
75 |
},
|
|
|
76 |
);
|
|
|
77 |
sort($locales);
|
|
|
78 |
return implode(';', $locales);
|
|
|
79 |
}
|
|
|
80 |
}
|