From 8bd8346423f52b558bda4e7c7aab3bffcd92ef3f Mon Sep 17 00:00:00 2001 From: Jiaxin Peng Date: Thu, 19 Sep 2024 13:47:57 +0100 Subject: [PATCH] fix: obsidian language not set in English, Chinese and Japanese --- src/lang/I18n.ts | 42 +++++++++++++++++++++++++++++++++++++----- src/main.ts | 2 +- yarn.lock | 23 ----------------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/lang/I18n.ts b/src/lang/I18n.ts index c87d69e..f97b33f 100644 --- a/src/lang/I18n.ts +++ b/src/lang/I18n.ts @@ -8,10 +8,42 @@ export const I18n: {[key: string]:any} = { ja, }; -export const I18nConfig = (lang: string): any => { - return I18n[lang]; -}; +class I18nManager { + private currentLanguage: string; + + constructor() { + this.currentLanguage = this.detectLanguage(); + } + + // return the language to use + private detectLanguage(): string { + const storedLanguage = window.localStorage.getItem("language"); + if (storedLanguage && this.isLanguageSupported(storedLanguage)) { + console.log(`Using stored language: ${storedLanguage}`); + return storedLanguage; + } + + const browserLanguage = window.navigator.language.split("-")[0]; + if (this.isLanguageSupported(browserLanguage)) { + console.log(`Using browser language: ${browserLanguage}`); + return browserLanguage; + } + + // Default to English if no match is found + console.log("Using default language: en"); + return "en"; + } + + private isLanguageSupported(lang: string): boolean { + return Object.prototype.hasOwnProperty.call(I18n, lang); + } + + // Get the i18n configuration for the current language + public getConfig(): any { + return I18n[this.currentLanguage]; + } +} -const storedLanguage = window.localStorage.getItem("language"); +export const i18nManager = new I18nManager(); -export const i18nConfig = I18n[storedLanguage || window.navigator.language.split('-')[0] || "en"]; +export const i18nConfig = i18nManager.getConfig(); diff --git a/src/main.ts b/src/main.ts index 4938e18..ce6323f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,7 +20,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin { addIcons(); // This creates an icon in the left ribbon. - const ribbonIconEl = this.addRibbonIcon( + this.addRibbonIcon( "notion-logo", i18nConfig.ribbonIcon, async (evt: MouseEvent) => { diff --git a/yarn.lock b/yarn.lock index c3c78f7..e4eb921 100644 --- a/yarn.lock +++ b/yarn.lock @@ -460,14 +460,6 @@ commander@^8.3.0: resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66" integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww== -cors@^2.8.5: - version "2.8.5" - resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" - integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== - dependencies: - object-assign "^4" - vary "^1" - data-uri-to-buffer@^4.0.0: version "4.0.1" resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e" @@ -722,11 +714,6 @@ katex@^0.16.0: dependencies: commander "^8.3.0" -ky@^1.7.2: - version "1.7.2" - resolved "https://registry.yarnpkg.com/ky/-/ky-1.7.2.tgz#b97d9b997ba51ff1e152f0815d3d27b86513eb1c" - integrity sha512-OzIvbHKKDpi60TnF9t7UUVAF1B4mcqc02z5PIvrm08Wyb+yOcz63GRvEuVxNT18a9E1SrNouhB4W2NNLeD7Ykg== - longest-streak@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/longest-streak/-/longest-streak-2.0.4.tgz#b8599957da5b5dab64dee3fe316fa774597d90e4" @@ -1228,11 +1215,6 @@ node-fetch@^3.3.2: fetch-blob "^3.1.4" formdata-polyfill "^4.0.10" -object-assign@^4: - version "4.1.1" - resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" - integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== - obsidian@^1.6.6: version "1.6.6" resolved "https://registry.yarnpkg.com/obsidian/-/obsidian-1.6.6.tgz#d45c4021c291765e1b77ed4a1c645e562ff6e77f" @@ -1459,11 +1441,6 @@ unist-util-visit@^5.0.0: unist-util-is "^6.0.0" unist-util-visit-parents "^6.0.0" -vary@^1: - version "1.1.2" - resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" - integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== - vfile-message@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-2.0.4.tgz#5b43b88171d409eae58477d13f23dd41d52c371a"