aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-10-27 14:55:19 +0200
committerArmand Philippot <git@armandphilippot.com>2021-10-27 15:58:36 +0200
commit5c5633c53f53b703425bd19dd3f6836a049d093e (patch)
treefefe87167e75e03bdf8ec374003a83dd12142ad1
parent5a1ee340a617bf4b85d18bb46210d9b25a9204dc (diff)
feat: define app locale depending on navigator preferred language
-rw-r--r--htdocs/src/js/app.js22
1 files changed, 20 insertions, 2 deletions
diff --git a/htdocs/src/js/app.js b/htdocs/src/js/app.js
index 2837621..f3c6a03 100644
--- a/htdocs/src/js/app.js
+++ b/htdocs/src/js/app.js
@@ -1,5 +1,10 @@
import projects from './config/projects';
-import { translate, setLocale, currentLocale } from './i18n/i18n';
+import {
+ translate,
+ setLocale,
+ currentLocale,
+ supportedLanguages,
+} from './i18n/i18n';
import {
hideToBottom,
hideToLeft,
@@ -327,10 +332,23 @@ function translateHTMLContent() {
}
/**
+ * Translate the website according to the user preferred language.
+ */
+function setAppLocale() {
+ const preferredLanguage = navigator.language;
+ const supportedLanguage = supportedLanguages.find(
+ (lang) => preferredLanguage.startsWith(lang.code)
+ // eslint-disable-next-line function-paren-newline -- Conflict with Prettier
+ );
+ const locale = supportedLanguage.code || 'en';
+ setLocale(locale);
+}
+
+/**
* Initialize the website with the projects list.
*/
function init() {
- setLocale('en');
+ setAppLocale();
translateHTMLContent();
loadWebpackStyles();
printProjectsNav();