diff --git a/web/src/lib/settings/youtube-lang.ts b/web/src/lib/settings/youtube-lang.ts index 55667aa0..1a777552 100644 --- a/web/src/lib/settings/youtube-lang.ts +++ b/web/src/lib/settings/youtube-lang.ts @@ -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; + }, {}) as Record; +} + +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"; }