From ce1665c9f96bbd88af1d0a549ee899363dac2834 Mon Sep 17 00:00:00 2001 From: OowhitecatoO Date: Thu, 19 Nov 2026 23:35:55 +0800 Subject: [PATCH] :bug: Fix Chinese browser language autodetection (#13157) * :bug: Fix Chinese browser language autodetection * :recycle: Pass the browser language to autodetect * :recycle: Share locale resolution between autodetect and set-locale --- frontend/src/app/util/i18n.cljs | 49 +++++++++++-------- frontend/test/frontend_tests/runner.cljs | 2 + .../test/frontend_tests/util_i18n_test.cljs | 34 +++++++++++++ 3 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 frontend/test/frontend_tests/util_i18n_test.cljs diff --git a/frontend/src/app/util/i18n.cljs b/frontend/src/app/util/i18n.cljs index d36b2872c7..5986474c61 100644 --- a/frontend/src/app/util/i18n.cljs +++ b/frontend/src/app/util/i18n.cljs @@ -59,34 +59,47 @@ {:label "简体中文 (community)" :value "zh_cn"} {:label "繁體中文 (community)" :value "zh_hant"}]) +;; Browser tags that name a supported locale under another code. Bare +;; "zh" defaults to Simplified Chinese, as in CLDR likely subtags. +(def ^:private locale-aliases + {"zh" "zh_cn" + "zh_hans" "zh_cn" + "zh_tw" "zh_hant" + "zh_hk" "zh_hant" + "zh_mo" "zh_hant"}) + +;; Candidate locale codes for a BCP 47 tag, most specific first. (defn- parse-locale [locale] - (let [locale (-> (str/lower locale) - (str/replace "-" "_"))] - (cond-> [locale] - (str/includes? locale "_") - (conj (subs locale 0 2))))) - -(def ^:private browser-locales - (delay - (-> (.-language globals/navigator) - (parse-locale)))) + (let [parts (-> (str/lower locale) + (str/replace "-" "_") + (str/split "_"))] + (->> (range (count parts) 0 -1) + (map #(str/join "_" (take % parts))) + (map #(get locale-aliases % %)) + (distinct) + (vec)))) ;; Set initial translation loading state as globaly stored variable; ;; this facilitates hot reloading (when-not (exists? (unchecked-get globals/global "penpotTranslations")) (unchecked-set globals/global "penpotTranslations" #js {})) -(defn- autodetect - [] - (let [supported (into #{} (map :value supported-locales))] - (loop [locales (seq @browser-locales)] +;; First supported locale for a language tag, or the default language. +(defn- resolve-locale + [tag] + (let [supported (into #{} (map :value) supported-locales)] + (loop [locales (seq (parse-locale tag))] (if-let [locale (first locales)] (if (contains? supported locale) locale (recur (rest locales))) cf/default-language)))) +(defn- autodetect + [] + (resolve-locale (.-language globals/navigator))) + (defn get-current "Get the currently memoized locale or execute the autodetection" [] @@ -136,13 +149,7 @@ (let [lname (if (or (nil? lname) (str/empty? lname)) (autodetect) - (let [supported (into #{} (map :value) supported-locales)] - (loop [locales (seq (parse-locale lname))] - (if-let [locale (first locales)] - (if (contains? supported locale) - locale - (recur (rest locales))) - cf/default-language))))] + (resolve-locale lname))] (->> (load lname) (p/fnly (fn [_r _c] diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 5e3e187f96..c7394bd455 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -197,6 +197,7 @@ [frontend-tests.ui.workspace-libraries-test] [frontend-tests.util-clipboard-test] [frontend-tests.util-http-test] + [frontend-tests.util-i18n-test] [frontend-tests.util-object-test] [frontend-tests.util-queue-test] [frontend-tests.util-range-tree-test] @@ -428,6 +429,7 @@ 'frontend-tests.ui.workspace-libraries-test 'frontend-tests.util-clipboard-test 'frontend-tests.util-http-test + 'frontend-tests.util-i18n-test 'frontend-tests.util-object-test 'frontend-tests.util-queue-test 'frontend-tests.util-range-tree-test diff --git a/frontend/test/frontend_tests/util_i18n_test.cljs b/frontend/test/frontend_tests/util_i18n_test.cljs new file mode 100644 index 0000000000..ae06d055e0 --- /dev/null +++ b/frontend/test/frontend_tests/util_i18n_test.cljs @@ -0,0 +1,34 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.util-i18n-test + (:require + [app.config :as cf] + [app.util.i18n :as i18n] + [cljs.test :as t])) + +(defn- resolve-locale + [language] + (#'i18n/resolve-locale language)) + +(t/deftest traditional-chinese-browser-languages + (doseq [language ["zh-TW" "zh-HK" "zh-MO" "zh-Hant" "zh-Hant-TW" "zh-Hant-HK"]] + (t/is (= "zh_hant" (resolve-locale language)) language))) + +(t/deftest simplified-chinese-browser-languages + (doseq [language ["zh" "zh-CN" "zh-SG" "zh-Hans" "zh-Hans-CN"]] + (t/is (= "zh_cn" (resolve-locale language)) language))) + +(t/deftest stored-locales-still-resolve + (doseq [{:keys [value]} i18n/supported-locales] + (t/is (= value (resolve-locale value)) value))) + +(t/deftest region-falls-back-to-language + (t/is (= "en" (resolve-locale "en-US"))) + (t/is (= "pt_br" (resolve-locale "pt-BR"))) + (t/is (= "fr" (resolve-locale "fr-BE"))) + (t/is (= "ja" (resolve-locale "ja-JP"))) + (t/is (= cf/default-language (resolve-locale "xx-YY"))))