mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
web/settings: add function for getting browser language
prep for migrating youtubeDubBrowserLang
This commit is contained in:
parent
7a50c89728
commit
7c9a824a69
1 changed files with 18 additions and 1 deletions
|
@ -78,6 +78,8 @@ export const youtubeLanguages = [
|
||||||
"zu"
|
"zu"
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
export type YoutubeLang = typeof youtubeLanguages[number];
|
||||||
|
|
||||||
export const namedYoutubeLanguages = () => {
|
export const namedYoutubeLanguages = () => {
|
||||||
return youtubeLanguages.reduce((obj, lang) => {
|
return youtubeLanguages.reduce((obj, lang) => {
|
||||||
const intlName = new Intl.DisplayNames([lang], { type: 'language' }).of(lang);
|
const intlName = new Intl.DisplayNames([lang], { type: 'language' }).of(lang);
|
||||||
|
@ -91,5 +93,20 @@ export const namedYoutubeLanguages = () => {
|
||||||
...obj,
|
...obj,
|
||||||
[lang]: name,
|
[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";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue