Skip to content

Commit

Permalink
Add lang cookies to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jarda-svoboda committed Jul 13, 2023
1 parent 98a7f7b commit 41d23f0
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 38 deletions.
2 changes: 2 additions & 0 deletions examples/component-scoped-csr/src/lib/translations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import i18n from 'sveltekit-i18n';
import lang from './lang.json';

export const defaultLocale = 'en';

/** @type {import('sveltekit-i18n').Config} */
export const config = {
translations: {
Expand Down
24 changes: 19 additions & 5 deletions examples/component-scoped-csr/src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { loadTranslations, translations } from '$lib/translations';
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';

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

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

await loadTranslations(initLocale, pathname); // keep this just before the `return`
// Get user preferred locale
if (!locale) {
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
}

// Get defined locales
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Use default locale if current locale is not supported
if (!supportedLocales.includes(locale)) {
locale = defaultLocale;
}

await loadTranslations(locale, pathname); // keep this just before the `return`

return {
i18n: { locale: initLocale, route: pathname },
i18n: { locale, route: pathname },
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
};
};
17 changes: 11 additions & 6 deletions examples/component-scoped-csr/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<script>
import { writable } from 'svelte/store';
import { t, locale, locales } from '$lib/translations';
const count = writable(2);
const handleChange = ({ currentTarget }) => {
const { value } = currentTarget;
document.cookie = `lang=${value} ;`;
};
$: count = 2;
</script>

<a href="/">{$t('menu.home')}</a>
<a href="/about">{$t('menu.about')}</a>
<br/>
<br/>
{$t('menu.notification', { count: $count })}<br />
<button on:click="{() => {if ($count) $count -= 1;}}">–</button>
<button on:click="{() => {$count += 1;}}">+</button>
{$t('menu.notification', { count })}<br />
<button on:click="{() => {if (count) count -= 1;}}">–</button>
<button on:click="{() => {count += 1;}}">+</button>
<hr />
<slot />
<br />
<br />
<br />
<br />
<select bind:value="{$locale}">
<select bind:value="{$locale}" on:change={handleChange}>
{#each $locales as value}
<option value="{value}">{$t(`lang.${value}`)}</option>
{/each}
Expand Down
2 changes: 2 additions & 0 deletions examples/component-scoped-ssr/src/lib/translations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import i18n from 'sveltekit-i18n';
import lang from './lang.json';

export const defaultLocale = 'en';

/** @type {import('sveltekit-i18n').Config} */
export const config = {
translations: {
Expand Down
24 changes: 19 additions & 5 deletions examples/component-scoped-ssr/src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { loadTranslations, translations } from '$lib/translations';
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';

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

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

await loadTranslations(initLocale, pathname); // keep this just before the `return`
// Get user preferred locale
if (!locale) {
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
}

// Get defined locales
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Use default locale if current locale is not supported
if (!supportedLocales.includes(locale)) {
locale = defaultLocale;
}

await loadTranslations(locale, pathname); // keep this just before the `return`

return {
i18n: { locale: initLocale, route: pathname },
i18n: { locale, route: pathname },
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
};
};
8 changes: 7 additions & 1 deletion examples/component-scoped-ssr/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<script>
import { t, locale, locales } from '$lib/translations';
const handleChange = ({ currentTarget }) => {
const { value } = currentTarget;
document.cookie = `lang=${value} ;`;
};
$: count = 2;
</script>

Expand All @@ -17,7 +23,7 @@
<br />
<br />
<br />
<select bind:value="{$locale}">
<select bind:value="{$locale}" on:change={handleChange}>
{#each $locales as value}
<option value="{value}">{$t(`lang.${value}`)}</option>
{/each}
Expand Down
2 changes: 2 additions & 0 deletions examples/fallback-locale/src/lib/translations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import i18n from 'sveltekit-i18n';
import lang from './lang.json';

export const defaultLocale = 'en';

/** @type {import('sveltekit-i18n').Config} */
export const config = {
fallbackLocale: 'en',
Expand Down
24 changes: 19 additions & 5 deletions examples/fallback-locale/src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { loadTranslations, translations } from '$lib/translations';
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';

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

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

await loadTranslations(initLocale, pathname); // keep this just before the `return`
// Get user preferred locale
if (!locale) {
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
}

// Get defined locales
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Use default locale if current locale is not supported
if (!supportedLocales.includes(locale)) {
locale = defaultLocale;
}

await loadTranslations(locale, pathname); // keep this just before the `return`

return {
i18n: { locale: initLocale, route: pathname },
i18n: { locale, route: pathname },
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
};
};
19 changes: 12 additions & 7 deletions examples/fallback-locale/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<script>
import { writable } from 'svelte/store';
import { t, locale, locales } from '$lib/translations';
const count = writable(2);
const handleChange = ({ currentTarget }) => {
const { value } = currentTarget;
document.cookie = `lang=${value} ;`;
};
$: count = 2;
</script>

<a href="/">{$t('menu.home')}</a>
<a href="/about">{$t('menu.about')}</a>
<br/>
<br/>
{$t('menu.notification', { count: $count })}<br />
<button on:click="{() => {if ($count) $count -= 1;}}">–</button>
<button on:click="{() => {$count += 1;}}">+</button>
{$t('menu.notification', { count })}<br />
<button on:click="{() => {if (count) count -= 1;}}">–</button>
<button on:click="{() => {count += 1;}}">+</button>
<hr />
<slot />
<br />
<br />
<br />
<br />
<select bind:value="{$locale}">
<select bind:value="{$locale}" on:change={handleChange}>
{#each $locales as value}
<option value="{value}">{$t(`lang.${value.toLowerCase()}`)}</option>
<option value="{value}">{$t(`lang.${value}`)}</option>
{/each}
</select>
2 changes: 1 addition & 1 deletion examples/locale-router-advanced/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const handle = async ({ event, resolve }) => {
if (routeRegex.test(pathname)) {

// Get defined locales
const supportedLocales = locales.get();
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Try to get locale from `pathname`.
let locale = supportedLocales.find((l) => l === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase());
Expand Down
2 changes: 1 addition & 1 deletion examples/locale-router-static/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const handle = async ({ event, resolve }) => {
const { pathname } = url;

// Get defined locales
const supportedLocales = locales.get();
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Try to get locale from `pathname`.
let locale = supportedLocales.find((l) => l === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase());
Expand Down
2 changes: 1 addition & 1 deletion examples/locale-router/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const handle = async ({ event, resolve }) => {
const { pathname } = url;

// Get defined locales
const supportedLocales = locales.get();
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Try to get locale from `pathname`.
let locale = supportedLocales.find((l) => l === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase());
Expand Down
2 changes: 2 additions & 0 deletions examples/multi-page/src/lib/translations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import i18n from 'sveltekit-i18n';
import { dev } from '$app/environment';
import lang from './lang.json';

export const defaultLocale = 'en';

/** @type {import('sveltekit-i18n').Config} */
export const config = {
log: {
Expand Down
24 changes: 19 additions & 5 deletions examples/multi-page/src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { loadTranslations, translations } from '$lib/translations';
import { locales, loadTranslations, translations, defaultLocale } from '$lib/translations';

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

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

await loadTranslations(initLocale, pathname); // keep this just before the `return`
// Get user preferred locale
if (!locale) {
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=-|_|,|;)/)}`.toLowerCase();
}

// Get defined locales
const supportedLocales = locales.get().map((l) => l.toLowerCase());

// Use default locale if current locale is not supported
if (!supportedLocales.includes(locale)) {
locale = defaultLocale;
}

await loadTranslations(locale, pathname); // keep this just before the `return`

return {
i18n: { locale: initLocale, route: pathname },
i18n: { locale, route: pathname },
translations: translations.get(), // `translations` on server contain all translations loaded by different clients
};
};
8 changes: 7 additions & 1 deletion examples/multi-page/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<script>
import { t, locale, locales } from '$lib/translations';
const handleChange = ({ currentTarget }) => {
const { value } = currentTarget;
document.cookie = `lang=${value} ;`;
};
$: count = 2;
</script>

Expand All @@ -17,7 +23,7 @@
<br />
<br />
<br />
<select bind:value="{$locale}">
<select bind:value="{$locale}" on:change={handleChange}>
{#each $locales as value}
<option value="{value}">{$t(`lang.${value}`)}</option>
{/each}
Expand Down

0 comments on commit 41d23f0

Please sign in to comment.