1.1.31 • Published 3 days ago

@jill64/svelte-i18n v1.1.31

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

@jill64/svelte-i18n

🌍 i18n toolkit for SvelteKit

Demo

Install

npm i @jill64/svelte-i18n

Quick Example

Use a function to translate from the specified locales based on the current route parameters.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'

const { locale, translate } = init({
  locales: ['en', 'ja'],
  slug: '[locale]',
  defaultLocale: 'en'
})
<!-- src/routes/[locale]/+page.svelte -->
<script>
  import { translate, locale } from '$lib/i18n'

  // src/routes/en => 'en'
  // src/routes/ja => 'ja'
  // src/routes/invalid-param => 'en' (defaultLocale)
  $: console.log($locale)
</script>

<h1>
  <!-- src/routes/en => English -->
  <!-- src/routes/ja => 日本語 -->
  {$translate({
    en: 'English',
    ja: '日本語'
  })}
</h1>

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

<!-- src/routes/+layouts.svelte -->
<script>
  import { LanguageManager } from '@jill64/svelte-i18n'
</script>

<LanguageManager />

<!-- locale = 'en' | 'ja' -->
<html lang="{locale}">
  <!-- ... -->
</html>

Attach html lang attribute

SSR uses the attach handler to add the lang attribute to html tags.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'

const { attach } = init({
  locales: ['en', 'ja'],
  defaultLocale: 'en'
})
// src/hooks.server.js
import { attach as handle } from '$lib/i18n'

To use with any handle hook, use the sequence function.

// src/hooks.server.js
import { attach } from '$lib/i18n'
import { sequence } from '@sveltejs/kit/hooks'

export const handle = sequence(attach, () => {
  // ... Your Handler Function
})

Route param matcher

Use param matcher to add type checking for route parameters.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'

const { match } = init({
  locales: ['en', 'ja'],
  slug: '[locale=locale]',
  defaultLocale: 'en'
})
// src/params/locale.js
export { match } from '$lib/i18n'

Switch Language

Quickly create links to different language versions of the current page.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'

const { altered } = init({
  locales: ['en', 'ja'],
  defaultLocale: 'en'
})
<!-- src/routes/[locale](en)/foo/bar/+page.svelte -->
<script>
  import { altered } from '$lib/i18n'
</script>

<!-- href="/ja/foo/bar" -->
<a href={$altered('ja')}> Jump to Japanese Version </a>

Locale Alternates

Create a link tag in the head element to another language based on the Locales to improve SEO.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n'

const {
  /* ... */
} = init({
  locales: ['en', 'ja'],
  defaultLocale: 'en'
})
<!-- src/routes/+layout.svelte -->
<script>
  import { LocaleAlternates } from '@jill64/svelte-i18n'
</script>

<LocaleAlternates xDefaultHref="default-language-href(optional)" />

For example, if you are in /[locale(en)]/foo/bar, create the following tag in the head element

<link rel="alternate" hreflang="ja" href="/ja/foo/bar" />
<link
  rel="alternate"
  hreflang="x-default"
  href="default-language-href(optional)"
/>

RTL

If RTL is required, a Svelte component can be created as follows

<script>
  import { translate, locale } from '$lib/i18n'
</script>

<p dir={$locale === 'ar' ? 'rtl' : 'ltr'}>
  {$translate({
    en: 'English',
    ar: 'عربي'
  })}
</p>

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.

// src/lib/i18n.js
import { init } from '@jill64/svelte-i18n/app'

const { locale, translate, attach, setting } = 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 $setting store.

<script>
  import { setting } from '$lib/i18n'

  const changeToJP = () => {
    $setting = 'ja'
  }
</script>

<button on:click={changeToJP}> Change to Japanese </button>

License

MIT

1.1.31

3 days ago

1.1.30

9 days ago

1.1.29

9 days ago

1.1.28

18 days ago

1.1.27

19 days ago

1.1.26

19 days ago

1.1.25

20 days ago

1.1.24

21 days ago

1.1.23

30 days ago

1.1.22

30 days ago

1.1.21

30 days ago

1.1.20

1 month ago

1.1.19

1 month ago

1.1.18

1 month ago

1.1.17

1 month ago

1.1.16

1 month ago

1.1.15

1 month ago

1.1.14

1 month ago

1.1.13

2 months ago

1.1.12

2 months ago

1.1.11

2 months ago

1.1.9

2 months ago

1.1.10

2 months ago

1.1.8

2 months ago

1.1.7

2 months ago

1.1.6

2 months ago

1.1.5

2 months ago

1.1.4

2 months ago

1.1.1

3 months ago

1.1.3

3 months ago

1.1.2

3 months ago

1.1.0

3 months ago

1.0.13

3 months ago

1.0.11

3 months ago

1.0.12

3 months ago

1.0.10

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago

0.3.0

5 months ago

0.2.6

6 months ago

0.2.5

6 months ago

0.2.4

6 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.2.1

6 months ago

0.2.0

6 months ago

0.1.0

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago