Skip to content

Commit 41d23f0

Browse files
committed
Add lang cookies to examples
1 parent 98a7f7b commit 41d23f0

File tree

15 files changed

+124
-38
lines changed

15 files changed

+124
-38
lines changed

examples/component-scoped-csr/src/lib/translations/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import i18n from 'sveltekit-i18n';
22
import lang from './lang.json';
33

4+
export const defaultLocale = 'en';
5+
46
/** @type {import('sveltekit-i18n').Config} */
57
export const config = {
68
translations: {
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { loadTranslations, translations } from '$lib/translations';
1+
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';
22

33
/** @type {import('@sveltejs/kit').ServerLoad} */
4-
export const load = async ({ url }) => {
4+
export const load = async ({ url, cookies, request }) => {
55
const { pathname } = url;
66

7-
const initLocale = 'en'; // get from cookie / user session etc...
7+
// Try to get the locale from cookie
8+
let locale = (cookies.get('lang') || '').toLowerCase();
89

9-
await loadTranslations(initLocale, pathname); // keep this just before the `return`
10+
// Get user preferred locale
11+
if (!locale) {
12+
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
13+
}
14+
15+
// Get defined locales
16+
const supportedLocales = locales.get().map((l) => l.toLowerCase());
17+
18+
// Use default locale if current locale is not supported
19+
if (!supportedLocales.includes(locale)) {
20+
locale = defaultLocale;
21+
}
22+
23+
await loadTranslations(locale, pathname); // keep this just before the `return`
1024

1125
return {
12-
i18n: { locale: initLocale, route: pathname },
26+
i18n: { locale, route: pathname },
1327
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
1428
};
1529
};

examples/component-scoped-csr/src/routes/+layout.svelte

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<script>
2-
import { writable } from 'svelte/store';
32
import { t, locale, locales } from '$lib/translations';
43
5-
const count = writable(2);
4+
const handleChange = ({ currentTarget }) => {
5+
const { value } = currentTarget;
6+
7+
document.cookie = `lang=${value} ;`;
8+
};
9+
10+
$: count = 2;
611
</script>
712

813
<a href="/">{$t('menu.home')}</a>
914
<a href="/about">{$t('menu.about')}</a>
1015
<br/>
1116
<br/>
12-
{$t('menu.notification', { count: $count })}<br />
13-
<button on:click="{() => {if ($count) $count -= 1;}}">–</button>
14-
<button on:click="{() => {$count += 1;}}">+</button>
17+
{$t('menu.notification', { count })}<br />
18+
<button on:click="{() => {if (count) count -= 1;}}">–</button>
19+
<button on:click="{() => {count += 1;}}">+</button>
1520
<hr />
1621
<slot />
1722
<br />
1823
<br />
1924
<br />
2025
<br />
21-
<select bind:value="{$locale}">
26+
<select bind:value="{$locale}" on:change={handleChange}>
2227
{#each $locales as value}
2328
<option value="{value}">{$t(`lang.${value}`)}</option>
2429
{/each}

examples/component-scoped-ssr/src/lib/translations/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import i18n from 'sveltekit-i18n';
22
import lang from './lang.json';
33

4+
export const defaultLocale = 'en';
5+
46
/** @type {import('sveltekit-i18n').Config} */
57
export const config = {
68
translations: {
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { loadTranslations, translations } from '$lib/translations';
1+
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';
22

33
/** @type {import('@sveltejs/kit').ServerLoad} */
4-
export const load = async ({ url }) => {
4+
export const load = async ({ url, cookies, request }) => {
55
const { pathname } = url;
66

7-
const initLocale = 'en'; // get from cookie / user session etc...
7+
// Try to get the locale from cookie
8+
let locale = (cookies.get('lang') || '').toLowerCase();
89

9-
await loadTranslations(initLocale, pathname); // keep this just before the `return`
10+
// Get user preferred locale
11+
if (!locale) {
12+
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
13+
}
14+
15+
// Get defined locales
16+
const supportedLocales = locales.get().map((l) => l.toLowerCase());
17+
18+
// Use default locale if current locale is not supported
19+
if (!supportedLocales.includes(locale)) {
20+
locale = defaultLocale;
21+
}
22+
23+
await loadTranslations(locale, pathname); // keep this just before the `return`
1024

1125
return {
12-
i18n: { locale: initLocale, route: pathname },
26+
i18n: { locale, route: pathname },
1327
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
1428
};
1529
};

examples/component-scoped-ssr/src/routes/+layout.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<script>
22
import { t, locale, locales } from '$lib/translations';
33
4+
const handleChange = ({ currentTarget }) => {
5+
const { value } = currentTarget;
6+
7+
document.cookie = `lang=${value} ;`;
8+
};
9+
410
$: count = 2;
511
</script>
612

@@ -17,7 +23,7 @@
1723
<br />
1824
<br />
1925
<br />
20-
<select bind:value="{$locale}">
26+
<select bind:value="{$locale}" on:change={handleChange}>
2127
{#each $locales as value}
2228
<option value="{value}">{$t(`lang.${value}`)}</option>
2329
{/each}

examples/fallback-locale/src/lib/translations/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import i18n from 'sveltekit-i18n';
22
import lang from './lang.json';
33

4+
export const defaultLocale = 'en';
5+
46
/** @type {import('sveltekit-i18n').Config} */
57
export const config = {
68
fallbackLocale: 'en',
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { loadTranslations, translations } from '$lib/translations';
1+
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';
22

33
/** @type {import('@sveltejs/kit').ServerLoad} */
4-
export const load = async ({ url }) => {
4+
export const load = async ({ url, cookies, request }) => {
55
const { pathname } = url;
66

7-
const initLocale = 'en'; // get from cookie / user session etc...
7+
// Try to get the locale from cookie
8+
let locale = (cookies.get('lang') || '').toLowerCase();
89

9-
await loadTranslations(initLocale, pathname); // keep this just before the `return`
10+
// Get user preferred locale
11+
if (!locale) {
12+
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
13+
}
14+
15+
// Get defined locales
16+
const supportedLocales = locales.get().map((l) => l.toLowerCase());
17+
18+
// Use default locale if current locale is not supported
19+
if (!supportedLocales.includes(locale)) {
20+
locale = defaultLocale;
21+
}
22+
23+
await loadTranslations(locale, pathname); // keep this just before the `return`
1024

1125
return {
12-
i18n: { locale: initLocale, route: pathname },
26+
i18n: { locale, route: pathname },
1327
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
1428
};
1529
};
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
<script>
2-
import { writable } from 'svelte/store';
32
import { t, locale, locales } from '$lib/translations';
43
5-
const count = writable(2);
4+
const handleChange = ({ currentTarget }) => {
5+
const { value } = currentTarget;
6+
7+
document.cookie = `lang=${value} ;`;
8+
};
9+
10+
$: count = 2;
611
</script>
712

813
<a href="/">{$t('menu.home')}</a>
914
<a href="/about">{$t('menu.about')}</a>
1015
<br/>
1116
<br/>
12-
{$t('menu.notification', { count: $count })}<br />
13-
<button on:click="{() => {if ($count) $count -= 1;}}">–</button>
14-
<button on:click="{() => {$count += 1;}}">+</button>
17+
{$t('menu.notification', { count })}<br />
18+
<button on:click="{() => {if (count) count -= 1;}}">–</button>
19+
<button on:click="{() => {count += 1;}}">+</button>
1520
<hr />
1621
<slot />
1722
<br />
1823
<br />
1924
<br />
2025
<br />
21-
<select bind:value="{$locale}">
26+
<select bind:value="{$locale}" on:change={handleChange}>
2227
{#each $locales as value}
23-
<option value="{value}">{$t(`lang.${value.toLowerCase()}`)}</option>
28+
<option value="{value}">{$t(`lang.${value}`)}</option>
2429
{/each}
2530
</select>

examples/locale-router-advanced/src/hooks.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const handle = async ({ event, resolve }) => {
1111
if (routeRegex.test(pathname)) {
1212

1313
// Get defined locales
14-
const supportedLocales = locales.get();
14+
const supportedLocales = locales.get().map((l) => l.toLowerCase());
1515

1616
// Try to get locale from `pathname`.
1717
let locale = supportedLocales.find((l) => l === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase());

0 commit comments

Comments
 (0)