-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from sveltekit-i18n/2.3.0
2.3.0
- Loading branch information
Showing
117 changed files
with
4,586 additions
and
14,513 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
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,4 +1,5 @@ | ||
**/.svelte-kit | ||
**/.netlify | ||
**/package | ||
**/build | ||
/lib | ||
|
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 |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
# sveltekit-i18n | ||
`sveltekit-i18n` is a tiny library with no external dependencies, built for [Svelte](https://github.com/sveltejs/svelte) and [SvelteKit](https://github.com/sveltejs/kit). It glues [@sveltekit-i18n/base](https://github.com/sveltekit-i18n/base) and [@sveltekit-i18n/parser-default](https://github.com/sveltekit-i18n/parsers/tree/master/parser-default) together to provide you the most straightforward `sveltekit-i18n` solution. | ||
|
||
__Note this README is related to `[email protected]`. If you are looking for version `1.x` you can find it [here](https://github.com/sveltekit-i18n/lib/tree/1.x).__ | ||
|
||
## Key features | ||
|
||
✅ SvelteKit ready\ | ||
|
@@ -77,44 +75,23 @@ const config = ({ | |
export const { t, locale, locales, loading, loadTranslations } = new i18n(config); | ||
``` | ||
|
||
...load your translations in `__layout.svelte`... | ||
|
||
```svelte | ||
<script context="module"> | ||
import { locale, loadTranslations } from '$lib/translations'; | ||
export const load = async ({ url }) => { | ||
const { pathname } = url; | ||
const defaultLocale = 'en'; // get from cookie, user session, ... | ||
const initLocale = locale.get() || defaultLocale; // set default if no locale already set | ||
...load your translations in `+layout.js`... | ||
|
||
await loadTranslations(initLocale, pathname); // keep this just before the `return` | ||
return {}; | ||
} | ||
</script> | ||
``` | ||
|
||
<details><summary>+layout.ts example</summary> | ||
```javascript | ||
import { locale, loadTranslations } from '$lib/translations'; | ||
|
||
In modern SvelteKit `+layout.ts` is used instead of `__layout.svelte`. | ||
export const load = async ({ url }) => { | ||
const { pathname } = url; | ||
|
||
```ts | ||
import { loadTranslations, locale } from '$lib/translations/translations'; | ||
import type { LayoutLoad } from './$types'; | ||
const defaultLocale = 'en'; // get from cookie, user session, ... | ||
|
||
const initLocale = locale.get() || defaultLocale; // set default if no locale already set | ||
|
||
export const load: LayoutLoad = async ({ url }) => { | ||
const { pathname } = url; | ||
const defaultLocale = 'en'; // get from cookie, user session, ... | ||
const initLocale = locale.get() || defaultLocale; // set default if no locale already set | ||
await loadTranslations(initLocale, pathname); // keep this just before the `return` | ||
await loadTranslations(initLocale, pathname); // keep this just before the `return` | ||
|
||
return {}; | ||
} | ||
return {}; | ||
} | ||
``` | ||
</details> | ||
|
||
...and include your translations within pages and components. | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { locale, loadTranslations } from '$lib/translations'; | ||
|
||
/** @type {import('@sveltejs/kit').Load} */ | ||
export const load = async ({ url }) => { | ||
const { pathname } = url; | ||
|
||
const defaultLocale = 'en'; // get from cookie / user session etc... | ||
|
||
const initLocale = locale.get() || defaultLocale; | ||
|
||
await loadTranslations(initLocale, pathname); // keep this just before the `return` | ||
|
||
return {}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script> | ||
import { writable } from 'svelte/store'; | ||
import { t, locale, locales } from '$lib/translations'; | ||
const count = writable(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> | ||
<hr /> | ||
<slot /> | ||
<br /> | ||
<br /> | ||
<br /> | ||
<br /> | ||
<select bind:value="{$locale}"> | ||
{#each $locales as value} | ||
<option value="{value}">{$t(`lang.${value}`)}</option> | ||
{/each} | ||
</select> |
6 changes: 3 additions & 3 deletions
6
...ponent-scoped-csr/src/routes/index.svelte → ...ponent-scoped-csr/src/routes/+page.svelte
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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...t-load-prevention/src/routes/about.svelte → ...-scoped-csr/src/routes/about/+page.svelte
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,8 +1,6 @@ | ||
import { sveltekit } from '@sveltejs/kit/vite'; | ||
import { defineConfig } from 'vite'; | ||
|
||
/** @type {import('vite').UserConfig} */ | ||
const config = { | ||
export default defineConfig({ | ||
plugins: [sveltekit()], | ||
}; | ||
|
||
export default config; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { locale, loadTranslations } from '$lib/translations'; | ||
|
||
/** @type {import('@sveltejs/kit').Load} */ | ||
export const load = async ({ url }) => { | ||
const { pathname } = url; | ||
|
||
const defaultLocale = 'en'; // get from cookie / user session etc... | ||
|
||
const initLocale = locale.get() || defaultLocale; | ||
|
||
await loadTranslations(initLocale, pathname); // keep this just before the `return` | ||
|
||
return {}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script context="module"> | ||
import { t, locale, locales } from '$lib/translations'; | ||
</script> | ||
|
||
<script> | ||
import { writable } from 'svelte/store'; | ||
const count = writable(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> | ||
<hr /> | ||
<slot /> | ||
<br /> | ||
<br /> | ||
<br /> | ||
<br /> | ||
<select bind:value="{$locale}"> | ||
{#each $locales as value} | ||
<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { init } from '$lib/component/index.svelte'; | ||
|
||
export const load = async () => { | ||
const { loading: loadingEn, ...restEn } = init('en'); | ||
const { loading: loadingDe, ...restDe } = init('de'); | ||
await Promise.all([loadingEn.toPromise(), loadingDe.toPromise()]); | ||
|
||
return { props1: { loading: loadingEn, ...restEn }, props2: { loading: loadingDe, ...restDe } }; | ||
}; |
Oops, something went wrong.