web/settings: add function for getting browser language

prep for migrating youtubeDubBrowserLang
This commit is contained in:
jj 2024-10-30 12:05:49 +00:00
parent 7a50c89728
commit 7c9a824a69
No known key found for this signature in database

View file

@ -78,6 +78,8 @@ export const youtubeLanguages = [
"zu"
] as const;
export type YoutubeLang = typeof youtubeLanguages[number];
export const namedYoutubeLanguages = () => {
return youtubeLanguages.reduce((obj, lang) => {
const intlName = new Intl.DisplayNames([lang], { type: 'language' }).of(lang);
@ -91,5 +93,20 @@ export const namedYoutubeLanguages = () => {
...obj,
[lang]: name,
};
}, {}) as Record<typeof youtubeLanguages[number], string>;
}, {}) as Record<YoutubeLang, string>;
}
export const getBrowserLanguage = (): YoutubeLang => {
if (typeof navigator === 'undefined')
return "original";
const browserLanguage = navigator.language as YoutubeLang;
if (youtubeLanguages.includes(browserLanguage))
return browserLanguage;
const shortened = browserLanguage.split('-')[0] as YoutubeLang;
if (youtubeLanguages.includes(shortened))
return shortened;
return "original";
}