[tor-commits] [snowflake/master] Get badge locale from navigator.language

arlo at torproject.org arlo at torproject.org
Mon Aug 26 19:15:10 UTC 2019


commit 1e33ae830f739a7d61e03ea1626f5e71ba175cbc
Author: Arlo Breault <arlolra at gmail.com>
Date:   Thu Aug 15 17:32:21 2019 -0400

    Get badge locale from navigator.language
---
 proxy/init-badge.js | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/proxy/init-badge.js b/proxy/init-badge.js
index b906f62..1acc923 100644
--- a/proxy/init-badge.js
+++ b/proxy/init-badge.js
@@ -82,6 +82,26 @@ function setSnowflakeCookie(val, expires) {
   document.cookie = `${COOKIE_NAME}=${val}; path=/; expires=${expires};`;
 }
 
+const defaultLang = 'en_US';
+const availableLangs = new Set([
+  'en_US',
+]);
+
+// Resolve as in,
+// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization#Localized_string_selection
+function getLang() {
+  let lang = navigator.language || defaultLang;
+  lang = lang.replace(/-/g, '_');
+  if (availableLangs.has(lang)) {
+    return lang;
+  }
+  lang = lang.split('_')[0];
+  if (availableLangs.has(lang)) {
+    return lang;
+  }
+  return defaultLang;
+}
+
 var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications, query;
 
 (function() {
@@ -171,8 +191,7 @@ var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotific
   };
 
   window.onload = function() {
-    const lang = 'en_US';
-    fetch(`./_locales/${lang}/messages.json`)
+    fetch(`./_locales/${getLang()}/messages.json`)
     .then((res) => {
       if (!res.ok) { return; }
       return res.json();





More information about the tor-commits mailing list