https://github.com/jill64/svelte-i18n
🌍 i18n toolkit for SvelteKit
https://github.com/jill64/svelte-i18n
i18n language localization svelte sveltekit
Last synced: over 1 year ago
JSON representation
🌍 i18n toolkit for SvelteKit
- Host: GitHub
- URL: https://github.com/jill64/svelte-i18n
- Owner: jill64
- Created: 2023-10-31T06:00:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-18T20:17:44.000Z (over 2 years ago)
- Last Synced: 2023-12-19T17:17:52.433Z (over 2 years ago)
- Topics: i18n, language, localization, svelte, sveltekit
- Language: TypeScript
- Homepage: https://svelte-i18n.jill64.dev
- Size: 341 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @jill64/svelte-i18n
🌍 i18n toolkit for SvelteKit
## [Demo](https://svelte-i18n.jill64.dev)
## Install
```sh
npm i @jill64/svelte-i18n
```
## Quick Example
Use a function to translate from the specified locales based on the current route parameters.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'
export const i = init({
locales: ['en', 'ja'],
slug: '[locale]',
defaultLocale: 'en'
})
```
```svelte
import { i } from '$lib/i18n'
// src/routes/en => 'en'
// src/routes/ja => 'ja'
// src/routes/invalid-param => 'en' (defaultLocale)
console.log(i.locale)
{i.translate({
en: 'English',
ja: '日本語'
})}
```
## Bind html lang attribute
Each time a locale change is detected on the client side, it is reflected in the `lang` attribute of the `html`
```svelte
import { LanguageManager } from '@jill64/svelte-i18n'
```
↓
```html
```
## Attach html lang attribute
SSR uses the `attach` handler to add the lang attribute to html tags.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'
const { i } = init({
locales: ['en', 'ja'],
defaultLocale: 'en'
})
```
```js:src/hooks.server.js
// src/hooks.server.js
import { i as handle } from '$lib/i18n'
export const handle = i.attach
```
To use with any handle hook, use the `sequence` function.
```js:src/hooks.server.js
// src/hooks.server.js
import { i } from '$lib/i18n'
import { sequence } from '@sveltejs/kit/hooks'
export const handle = sequence(i.attach, () => {
// ... Your Handler Function
})
```
## Route param matcher
Use param matcher to add type checking for route parameters.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'
const i = init({
locales: ['en', 'ja'],
slug: '[locale=locale]',
defaultLocale: 'en'
})
```
```js:src/params/locale.js
// src/params/locale.js
import { i } from '$lib/i18n'
export const match = i.match
```
## Switch Language
Quickly create links to different language versions of the current page.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'
const i = init({
locales: ['en', 'ja'],
defaultLocale: 'en'
})
```
```svelte
import { i } from '$lib/i18n'
## Locale Alternates
Create a link tag in the head element to another language based on the Locales to improve SEO.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'
const {
/* ... */
} = init({
locales: ['en', 'ja'],
defaultLocale: 'en'
})
```
```svelte
import { LocaleAlternates } from '@jill64/svelte-i18n'
```
For example, if you are in `/[locale(en)]/foo/bar`, create the following tag in the `head` element
```html
```
## RTL
If RTL is required, a Svelte component can be created as follows
```svelte
import { i } from '$lib/i18n'
{i.translate({
en: 'English',
ar: 'عربي'
})}
```
## Web App Mode
Web applications may not want to include language as a parameter to keep URLs clean.
In app mode, language settings are stored using cookies and localStorage.
```js:src/lib/i18n.js
// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n/app'
// { locale, translate, attach, setting }
export const i = init({
locales: ['en', 'ja'],
defaultLocale: 'en'
})
```
The following features are not available in this mode
- route based language switching
- route matcher
- `match`
- `altered`
- `LocaleAlternates`
## Set Locale
In app mode, language settings can be changed by setting values in the `i.setting` store.
```svelte
import { i } from '$lib/i18n'
const changeToJP = () => {
i.setting = 'ja'
}
const changeToEN = () => {
i.setting = 'en'
}
Change to Japanese
Change to English
```
## License
[MIT](LICENSE)