2.2.1 • Published 8 months ago

@jill64/svelte-i18n v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months 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'

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

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

<h1>
  <!-- src/routes/en => English -->
  <!-- src/routes/ja => 日本語 -->
  {i.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 { i } = init({
  locales: ['en', 'ja'],
  defaultLocale: 'en'
})
// 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.

// 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.

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

const i = init({
  locales: ['en', 'ja'],
  slug: '[locale=locale]',
  defaultLocale: 'en'
})
// 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.

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

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

<!-- href="/ja/foo/bar" -->
<a href={i.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 { i } from '$lib/i18n'
</script>

<p dir={i.locale === 'ar' ? 'rtl' : 'ltr'}>
  {i.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'

// { 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.

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

  const changeToJP = () => {
    i.setting = 'ja'
  }
  const changeToEN = () => {
    i.setting = 'en'
  }
</script>

<button onclick={changeToJP}> Change to Japanese </button>
<button onclick={changeToEN}> Change to English </button>

License

MIT

2.2.1

8 months ago

2.2.0

10 months ago

2.0.0

11 months ago

2.1.1

10 months ago

1.1.56

11 months ago

1.1.55

11 months ago

1.1.54

11 months ago

2.1.0

10 months ago

1.1.53

1 year ago

1.1.52

1 year ago

1.1.51

1 year ago

1.1.50

1 year ago

1.1.34

1 year ago

1.1.38

1 year ago

1.1.37

1 year ago

1.1.36

1 year ago

1.1.35

1 year ago

1.1.39

1 year ago

1.1.41

1 year ago

1.1.40

1 year ago

1.1.45

1 year ago

1.1.44

1 year ago

1.1.43

1 year ago

1.1.42

1 year ago

1.1.49

1 year ago

1.1.48

1 year ago

1.1.47

1 year ago

1.1.46

1 year ago

1.1.33

2 years ago

1.1.32

2 years ago

1.1.31

2 years ago

1.1.30

2 years ago

1.1.29

2 years ago

1.1.28

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.9

2 years ago

1.1.10

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.1

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.13

2 years ago

1.0.11

2 years ago

1.0.12

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.0

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago