-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98a7f7b
commit 41d23f0
Showing
15 changed files
with
124 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 19 additions & 5 deletions
24
examples/component-scoped-csr/src/routes/+layout.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 19 additions & 5 deletions
24
examples/component-scoped-ssr/src/routes/+layout.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters