Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
🌐 Add support for the Accept-Language header
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Nov 20, 2023
1 parent f76bd0b commit 3417dc0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default defineConfig({
baseURL: 'http://127.0.0.1:4173',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry'
trace: 'on-first-retry',

locale: 'de-DE'
},

/* Configure projects for major browsers */
Expand Down
3 changes: 2 additions & 1 deletion src/languages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const languages = {
export const languageShortcuts = Object.keys(languages) as (keyof typeof languages)[];
export type Languages = keyof typeof languages;

export let lang: Languages = 'de';
// export let lang: Languages = 'de';
export let lang: Languages;

currentLanguage.subscribe((value) => {
lang = value;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/layout/Language.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { currentLanguage } from '../../stores';
import type { Languages } from '../../languages/i18n';
export let defaultLanguage: Languages;
currentLanguage.set(defaultLanguage);
currentLanguage.subscribe((l) => {
if (browser) document.getElementsByTagName('html')[0].lang = l;
});
Expand Down
26 changes: 26 additions & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { languageShortcuts, type Languages } from '../languages/i18n';

const defaultLanguage: Languages = 'de';

export const load = ({ request }) => {
const header = request.headers;
const accpetedLanguages = header.get('accept-language') as string | null;
if (!accpetedLanguages) return { requestLanguage: defaultLanguage };
const possibilities = languageShortcuts.filter((i) => accpetedLanguages.indexOf(i) > -1);

const sortedPossibilities = [...possibilities].sort((a, b) => {
const indexA = accpetedLanguages.indexOf(a);
const indexB = accpetedLanguages.indexOf(b);
return indexA - indexB;
});

/**
* The language that should be used if no lang is specified in localstorage
* Defaults to German
*/
const lang = sortedPossibilities.length > 0 ? sortedPossibilities[0] : defaultLanguage;

return {
requestLanguage: lang
} as const;
};
9 changes: 8 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
import Theme from '$lib/layout/Theme.svelte';
import SubjectColors from '$lib/layout/SubjectColors.svelte';
import Launcher from '$lib/layout/launcher/launcher.svelte';
import type { Languages } from '../languages/i18n';
let footerHeight = 0;
let navbarHeight = 0;
interface DataInterface {
requestLanguage: Languages;
}
export let data: DataInterface;
const updateCSSVariables = () => {
if (browser) {
document.documentElement.style.setProperty('--navbar-height', `${navbarHeight}px`);
Expand Down Expand Up @@ -52,7 +59,7 @@
<Network />
<Settings />
<Title />
<Language />
<Language defaultLanguage={data.requestLanguage} />
<Theme />
<Logout />
<SubjectColors />
Expand Down

0 comments on commit 3417dc0

Please sign in to comment.