Skip to content

Commit

Permalink
fix: improve language detection by respecting oai language setting
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Jun 29, 2024
1 parent 69d0deb commit 6189cc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export const KEY_TIMESTAMP_MARKDOWN = 'exporter:timestamp_markdown'
export const KEY_TIMESTAMP_HTML = 'exporter:timestamp_html'
export const KEY_META_ENABLED = 'exporter:enable_meta'
export const KEY_META_LIST = 'exporter:meta_list'

export const KEY_OAI_LOCALE = 'oai/apps/locale'
export const KEY_OAI_HISTORY_DISABLED = 'oai/apps/historyDisabled'
22 changes: 15 additions & 7 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import { KEY_LANGUAGE } from './constants'
import { KEY_LANGUAGE, KEY_OAI_LOCALE } from './constants'
import en_US from './locales/en.json'
import es from './locales/es.json'
import id_ID from './locales/id.json'
Expand Down Expand Up @@ -113,13 +113,15 @@ const resources = LOCALES.reduce<Record<string, { translation: Record<string, st
return acc
}, {})

function standardizeLanguage(language: string) {
function standardizeLanguage(language: string | null) {
if (!language) return null

if (language in LanguageMapping) return LanguageMapping[language]

const shortLang = language.split('-')[0]
if (shortLang in LanguageMapping) return LanguageMapping[shortLang]

return language
return null
}

function getNavigatorLanguage() {
Expand All @@ -133,14 +135,20 @@ function getNavigatorLanguage() {
return null
}

function getOaiLanguage() {
const storedLanguage = window?.localStorage?.getItem(KEY_OAI_LOCALE)
return storedLanguage?.replace(/^"(.*)"$/, '$1') ?? null
}

function getDefaultLanguage() {
const storedLanguage = ScriptStorage.get<string>(KEY_LANGUAGE)
if (storedLanguage) return standardizeLanguage(storedLanguage)

const oaiLanguage = getOaiLanguage()
const browserLanguage = getNavigatorLanguage()
if (browserLanguage) return standardizeLanguage(browserLanguage)

return EN_US.code
return standardizeLanguage(storedLanguage)
?? standardizeLanguage(oaiLanguage)
?? standardizeLanguage(browserLanguage)
?? EN_US.code
}

i18n
Expand Down
4 changes: 2 additions & 2 deletions src/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { unsafeWindow } from 'vite-plugin-monkey/dist/client'
import { KEY_OAI_HISTORY_DISABLED } from './constants'
import { getBase64FromImageUrl, getBase64FromImg } from './utils/dom'

declare global {
Expand Down Expand Up @@ -41,9 +42,8 @@ declare global {
}
}

const historyDisabledKey = 'oai/apps/historyDisabled'
export function getHistoryDisabled(): boolean {
return localStorage.getItem(historyDisabledKey) === '"true"'
return localStorage.getItem(KEY_OAI_HISTORY_DISABLED) === '"true"'
}

function getUserProfile() {
Expand Down

0 comments on commit 6189cc0

Please sign in to comment.