mirror of
https://github.com/heyman/heynote.git
synced 2026-02-06 11:27:25 +00:00
Merge pull request #435 from heyman/fix/sanitize-system-locale
Sanitize system locale
This commit is contained in:
commit
f5435e8f57
@ -1,6 +1,7 @@
|
||||
import { toRaw, nextTick, watch } from 'vue';
|
||||
import { defineStore } from "pinia"
|
||||
import { NoteFormat } from "../common/note-format"
|
||||
import { toSafeBrowserLocale } from "../util/locale.js"
|
||||
import { useEditorCacheStore } from "./editor-cache"
|
||||
import {
|
||||
SCRATCH_FILE_NAME, WINDOW_FULLSCREEN_STATE, WINDOW_FOCUS_STATE,
|
||||
@ -380,5 +381,5 @@ export async function initHeynoteStore() {
|
||||
watch(() => heynoteStore.currentBufferPath, () => heynoteStore.saveTabsState())
|
||||
watch(() => heynoteStore.openTabs, () => heynoteStore.saveTabsState())
|
||||
|
||||
heynoteStore.systemLocale = await window.heynote.getSystemLocale()
|
||||
heynoteStore.systemLocale = toSafeBrowserLocale(await window.heynote.getSystemLocale())
|
||||
}
|
||||
|
||||
23
src/util/locale.js
Normal file
23
src/util/locale.js
Normal file
@ -0,0 +1,23 @@
|
||||
export function toSafeBrowserLocale(locale) {
|
||||
// first attempt: maybe it's already fine
|
||||
try {
|
||||
return Intl.getCanonicalLocales(locale)[0];
|
||||
} catch { }
|
||||
|
||||
// underscores -> hyphens (en_US -> en-US)
|
||||
locale = locale.replace(/_/g, "-")
|
||||
// drop ".UTF-8", ".utf8", etc.
|
||||
locale = locale.replace(/\.[A-Za-z0-9_-]+$/, "")
|
||||
|
||||
// If there's an ICU/POSIX modifier like @calendar=... or @euro, we must drop it
|
||||
// (Intl doesn’t understand "@...").
|
||||
locale = locale.split("@", 1)[0];
|
||||
|
||||
// try again after normalization
|
||||
try {
|
||||
return Intl.getCanonicalLocales(locale)[0]
|
||||
} catch { }
|
||||
|
||||
// last resort
|
||||
return navigator.language
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user