gnucash-htdocs beta: Refactor locale detection.
John Ralls
jralls at code.gnucash.org
Thu Jul 7 18:50:39 EDT 2022
discards https://github.com/Gnucash/gnucash-htdocs/commit/4f16cd61 (commit)
Updated via https://github.com/Gnucash/gnucash-htdocs/commit/9d11ddd6 (commit)
This update added new revisions after undoing existing revisions. That is
to say, the old revision is not a strict subset of the new revision. This
situation occurs when you --force push a change and generate a repository
containing something like this:
* -- * -- B -- O -- O -- O (https://github.com/Gnucash/gnucash-htdocs/commit/4f16cd61)
\
N -- N -- N (https://github.com/Gnucash/gnucash-htdocs/commit/9d11ddd6)
When this happens we assume that you've already had alert emails for all
of the O revisions, and so we here report only the revisions in the N
branch from the common base, B.
commit 9d11ddd678e6ca631e056822a1d1715683f662a3
Author: John Ralls <jralls at ceridwen.us>
Date: Thu Jul 7 15:24:16 2022 -0700
Refactor locale detection.
Parse all language strings the same regardless of source.
Use language quality factors if included.
diff --git a/lang.php b/lang.php
index f1848ac..2a46ecd 100644
--- a/lang.php
+++ b/lang.php
@@ -2,6 +2,27 @@
$top_dir = ".";
$locale_dir = "locale";
+# key: locale, value: lang_dir
+$supported_languages = array(
+ 'ca_ES' => 'ca',
+ 'de_DE' => 'de',
+ 'en_US' => 'en',
+ 'es_ES' => 'es',
+ 'fr_FR' => 'fr',
+ 'he_IL' => 'he',
+ 'hr_HR' => 'hr',
+ 'hu_HU' => 'hu',
+ 'id_ID' => 'id',
+ 'it_IT' => 'it',
+ 'ja_JP' => 'ja',
+ 'nb_NO' => 'nb',
+ 'nl_NL' => 'nl',
+ 'pl_PL' => 'pl',
+ 'pt_PT' => 'pt',
+ 'zh_CN' => 'zh_CN',
+ 'zh_TW' => 'zh_TW'
+);
+
if (!array_key_exists('HTTP_HOST', $_SERVER) || ($_SERVER["HTTP_HOST"] == "lists.gnucash.org"))
{
$home = "https://www.gnucash.org";
@@ -21,76 +42,52 @@ if (array_key_exists('lang_cookie', $_COOKIE)) {
}
# allow user override.
-$get_lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
-
-if ($get_lang) { $locale = $get_lang; }
+$languages = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
# choose a default language based on the client browser's preferred
# language list
#echo ("<!-- top_dir: $top_dir, me: ".__FILE__."-->\n");
+# Find the locale from Client Accept language
+# Get user preferred languages, and match against supported language
+if ($languages == "" and isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+{
+ # tolower() => remove space => '-' -> '_'
+ $accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITZE_STRING);
+ $languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
+}
-# key: locale, value: lang_dir
-$supported_languages = array(
- 'ca_ES' => 'ca',
- 'de_DE' => 'de',
- 'en_US' => 'en',
- 'es_ES' => 'es',
- 'fr_FR' => 'fr',
- 'he_IL' => 'he',
- 'hr_HR' => 'hr',
- 'hu_HU' => 'hu',
- 'id_ID' => 'id',
- 'it_IT' => 'it',
- 'ja_JP' => 'ja',
- 'nb_NO' => 'nb',
- 'nl_NL' => 'nl',
- 'pl_PL' => 'pl',
- 'pt_PT' => 'pt',
- 'zh_CN' => 'zh_CN',
- 'zh_TW' => 'zh_TW'
-);
-
-# Find the full locale name for short language name.
-if (strlen($locale) == 2) {
- foreach($supported_languages as $loc_lang => $loc_dir)
+$ranked_langs = [];
+$languages = explode(",", $languages);
+if (count($languages) > 1) {
+ foreach ($languages as $item)
{
- if ( (strtolower($locale) == strtolower($loc_dir))
- || (strtolower($locale) == substr($loc_lang, 0, 2 )) )
- {
- $locale = $loc_lang;
- break;
+ $parts = explode(";", $item);
+ if (!$parts[1]) {
+ $ranked_langs[$parts[0]] = 1.0;
+ } else {
+ $ranked_langs[$parts[0]] = substr($parts[1], 2, NULL);
}
}
+ arsort($ranked_langs, SORT_NUMERIC);
+} else {
+ $ranked_langs[$languages] = 1.0;
}
-
-# Find the locale from Client Accept language
-if ($locale == "") {
- # Get user preferred languages, and match against supported language
- if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
+foreach (array_keys($ranked_langs) as $lang) {
+ if ($ranked_langs[$lang] == 0) {
+ break;
+ }
+ $lang_short = substr($lang, 0, 2);
+ # full match is prefer, but short match is acceptable.
+ foreach ($supported_languages as $loc_lang => $loc_dir)
{
- # tolower() => remove space => '-' -> '_'
- # "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
- $accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITZE_STRING);
- $languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
- $languages = explode(",", $languages);
- foreach ($languages as $item)
- {
- # "zh_cn;q=0.7" => "zh_cn"
- $lang = substr($item, 0, strcspn($item, ';'));
- $lang_short = substr($lang, 0, 2);
- # full match is prefer, but short match is acceptable.
- foreach ($supported_languages as $loc_lang => $loc_dir)
- {
- if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
- if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
- }
- if ($locale != "") { break; }
- }
+ if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
+ if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
}
- # nothing matched, use default language
- if ($locale == "") { $locale = "en_US"; }
+ if ($locale != "") { break; }
}
+# nothing matched, use default language
+if ($locale == "") { $locale = "en_US"; }
$lang_dir = array_key_exists($locale, $supported_languages) ?
$supported_languages[$locale] : "en";
Summary of changes:
lang.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
More information about the gnucash-changes
mailing list