-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizationHelper.php
More file actions
executable file
·143 lines (126 loc) · 6.11 KB
/
LocalizationHelper.php
File metadata and controls
executable file
·143 lines (126 loc) · 6.11 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace TestPlugin {
class LocalizationHelper {
/**
* Returns the list of locales (sorted based on priority) from HTTP_ACCEPT_LANGUAGE.
*
* @return array Array of locales (sorted based on priority), where the locale is the key and the value is the priority
*/
public static function getAllLocalesFromRequest() {
$preferredLocales = [];
if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
$preferredLocales = array_reduce(
explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']),//get array of all locales from this header
function ($res, $el) {
list($l, $q) = array_merge(explode(';q=', $el), [1]);//split on the "priority" number of each (the q= part of each local entry)
$res[$l] = (float)$q;//store priority with locale it belongs to
return $res;
},
[]//start with empty array as output
);
arsort($preferredLocales);//sort the array by priority
}
return $preferredLocales;
}
/**
* Removes "specialization" of a locale string
* EG: en-US -> en, zh-hk -> zh
*
* @param string The locale string to simplify
*
* @return string The locale, without the specialization
*/
public static function removeLocaleSpecialization($locale) {
return explode('-',$locale)[0];
}
/**
* Retrieves "specialization" of a locale string
* EG: en-US -> US, zh-hk -> hk
*
* @param string The locale string to get the specialization for
*
* @return string The specialization of the locale (or "" if locale doesn't have a specialization)
*/
public static function getLocaleSpecialization($locale) {
$spec = "";
if (strpos($locale,'-') !== false) {
$specArray = explode('-',$locale);
array_shift($specArray);
$spec = implode('-',$specArray);
}
return $spec;
}
/**
* Check if a locale is valid formatted.
*
* @param string $locale The locale to check.
*
* @return bool True if is a valid locale format, false if not.
*/
public static function isValidLocale($locale) {
return (bool) preg_match('#^[a-z]{2}_[A-Z]{2}(\\S+)?$#', $locale);
}
public static function getLocalizationInfo(){
$localesWanted = LocalizationHelper::getAllLocalesFromRequest();
$localeToUse = "en";//the default
if(!empty($localesWanted)) {
reset($localesWanted);//ensure "iterator" of array is set to the front
$localeToUse = key($localesWanted);//get first locale from array (locales in this array are KEYS)
}
$spec = LocalizationHelper::getLocaleSpecialization($localeToUse);
$simplifiedLocale = LocalizationHelper::removeLocaleSpecialization($localeToUse);
return [
"locales_preferred" => $localesWanted,
"preferred_locale" => $localeToUse,
"preferred_simplified_locale" => $simplifiedLocale,
"preferred_locale_specialization" => $spec
];
}
/**
* Outputs HTML for localization settings. The HTML has an id of "localize_settings"
*
* @param array $localizeInfo The localization information to use to output
* @param string $pageName The name of the page this is for, used to load the right JSON for the page
* @param string $rootDir The root directory of the JS folder
*/
public static function outputLocalizationInfo($localizeInfo, $pageName, $rootDir) {
$localizeInfo["page_name"] = $pageName;
$langJSONPath = implode(DIRECTORY_SEPARATOR,[$rootDir,"js","localization","i18n"]);
$langJSON = JSONHelper::load_json_file($pageName."-".$localizeInfo["preferred_simplified_locale"], $langJSONPath);
if($langJSON === FALSE) {
//error
} else {
$decodedJson = NULL;
//ensure data is "flat" from the file
if(is_array($langJSON)) {
foreach ($langJSON as $json) {
$decodedJson = array_merge($decodedJson, json_decode($json, true));
}
} else {
$decodedJson = json_decode($langJSON, true);
}
//get available languages
$localizeInfo["available_languages"] = LocalizationHelper::getAvailableLanguagesForPage($pageName, $rootDir);
$localizeInfo["preferred_lang_messages"] = $decodedJson;
unset($localizeInfo["preferred_lang_messages"]["@metadata"]);
?>
<span class="hidden" id="localize_settings" data-localization-settings="<?php echo htmlspecialchars(json_encode($localizeInfo),ENT_QUOTES,'UTF-8'); ?>"></span>
<?php
}
}
public static function doLocalizationSetupForPagename($pageName, $rootDir) {
$locale_info = LocalizationHelper::getLocalizationInfo();
LocalizationHelper::outputLocalizationInfo($locale_info, $pageName, $rootDir);
}
public static function getAvailableLanguagesForPage($pageName, $rootDir) {
$result = [];
$languageJSONFilePath = implode(DIRECTORY_SEPARATOR,[$rootDir,"js","localization","i18n"]);
$langFiles = glob($languageJSONFilePath.DIRECTORY_SEPARATOR."{".$pageName."}-*.json", GLOB_BRACE);
foreach ($langFiles as $file) {
$lang = str_replace($pageName."-","",basename($file,'.json'));
array_push($result, $lang);
}
return $result;
}
}
}