Internationalization library for Svelte
Go to file
Christian Kaisermann dcf36b575a chore: 🤖 add test:ci script
2019-11-25 11:35:05 -03:00
.github chore: 🤖 add github templates 2019-11-19 21:10:41 -03:00
example feat: 🎸 add custom formats support 2019-11-22 13:51:18 -03:00
src feat: 🎸 add custom formats support 2019-11-22 13:51:18 -03:00
test Release v1.1.2 2019-11-25 11:27:33 -03:00
.babelrc Namespace i18n store methods with "i18n" property 2018-08-08 20:17:08 -03:00
.editorconfig I'm sorry Typescript, it's not you... 2018-08-07 18:53:07 -03:00
.eslintignore I'm sorry Typescript, it's not you... 2018-08-07 18:53:07 -03:00
.eslintrc Simplify linting config 2019-11-11 21:11:57 -03:00
.gitignore Make tests work again. New utility structure 2018-08-08 02:17:09 -03:00
.prettierrc I'm sorry Typescript, it's not you... 2018-08-07 18:53:07 -03:00
LICENSE 👶 Initial commit 2018-07-25 22:50:55 -03:00
package-lock.json feat: 🎸 add custom formats support 2019-11-22 13:51:18 -03:00
package.json chore: 🤖 add test:ci script 2019-11-25 11:35:05 -03:00
README.md Release v1.1.2 2019-11-25 11:27:33 -03:00
rollup.config.js Add es bundle to dist again 2019-06-19 17:34:18 -03:00
yarn.lock Release v1.1.2 2019-11-25 11:27:33 -03:00

svelte-i18n

Internationalization for Svelte.

Note: the v2 version was unpublished and will be released again in the following week after I rewrite all of its tests. For now, use the v1.1.2.

Usage

svelte-i18n utilizes svelte stores for keeping track of the current locale, dictionary of messages and the main format function. This way, we keep everything neat, in sync and easy to use on your svelte files.


Locale

The locale store defines what is the current locale.

import { locale, dictionary, getClientLocale } from 'svelte-i18n'

// Set the current locale to en-US
locale.set('en-US')

// This is a store, so we can subscribe to its changes
locale.subscribe(() => {
  console.log('locale change')
})

// svelte-i18n exports a method to help getting the current client locale
locale.set(
  getClientLocale({
    // the fallback locale, if didn't find any
    fallback: 'en-US',
    // set to 'true' to check the 'window.navigator.language'
    navigator: true,
    // set the key name to look for a locale on 'window.location.search'
    // 'example.com?locale=en-US'
    search: 'lang',
    // set the key name to look for a locale on 'window.location.hash'
    // 'example.com#locale=en-US'
    hash: 'locale',
  }),
)

If a locale with the format xx-YY is not found, svelte-i18n looks for the locale xx as well.


The dictionary

The dictionary store defines the dictionary of messages of all locales.

import { locale, dictionary } from 'svelte-i18n'

// Define a locale dictionary
dictionary.set({
  pt: {
    message: 'Mensagem',
    'switch.lang': 'Trocar idioma',
    greeting: {
      ask: 'Por favor, digite seu nome',
      message: 'Olá {name}, como vai?',
    },
    photos:
      'Você {n, plural, =0 {não tem fotos.} =1 {tem uma foto.} other {tem # fotos.}}',
    cats: 'Tenho {n, number} {n,plural,=0{gatos}one{gato}other{gatos}}',
  },
  en: {
    message: 'Message',
    'switch.lang': 'Switch language',
    greeting: {
      ask: 'Please type your name',
      message: 'Hello {name}, how are you?',
    },
    photos:
      'You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}',
    cats: 'I have {n, number} {n,plural,one{cat}other{cats}}',
  },
})

// It's also possible to merge the current dictionary
// with other objets
dictionary.update(dict => {
  dict.fr = {
    // ...french messages
  }
  return dict
})

Each language message dictionary can be as deep as you want. Messages can also be looked up by a string represetation of it's path on the dictionary (i.e greeting.message).


Formatting

The _/format store is the actual formatter method. To use it, it's simple as any other svelte store.

<script>
  // locale is en
  import { _ } from 'svelte-i18n'
</script>

<input placeholder="{$_('greeting.ask')}" />

svelte-i18n uses formatjs behind the scenes, which means it supports the ICU message format for interpolation, pluralization and much more.

<div>
  {$_('greeting.message', { name: 'John' })}
  <!-- Hello John, how are you? -->

  {$_('photos', { n: 0 })}
  <!-- You have no photos. -->

  {$_('photos', { n: 12 })}
  <!-- You have 12 photos. -->
</div>

Formatting methods

_ / format

function(messageId: string, locale:? string): string

function(messageId: string, interpolations?: object, locale:? string): string

Main formatting method that formats a localized message by its id.

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_('greeting.ask')}</div>
<!-- Please type your name -->

_.upper

Transforms a localized message into uppercase.

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.upper('greeting.ask')}</div>
<!-- PLEASE TYPE YOUR NAME -->

_.lower

Transforms a localized message into lowercase.

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.lower('greeting.ask')}</div>
<!-- PLEASE TYPE YOUR NAME -->

_.capital

Capitalize a localized message.

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.capital('greeting.ask')}</div>
<!-- Please type your name -->

_.title

Transform the message into title case.

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.capital('greeting.ask')}</div>
<!-- Please Type Your Name -->

_.time

function(time: Date, format?: string, locale?: string)

Formats a date object into a time string with the specified format (short, medium, long, full). Please refer to the ICU message format documentation for all available. formats

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.time(new Date(2019, 3, 24, 23, 45))}</div>
<!-- 11:45 PM -->

<div>{$_.time(new Date(2019, 3, 24, 23, 45), 'medium')}</div>
<!-- 11:45:00 PM -->

_.date

function(date: Date, format?: string, locale?: string)

Formats a date object into a string with the specified format (short, medium, long, full). Please refer to the ICU message format documentation for all available. formats

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.date(new Date(2019, 3, 24, 23, 45))}</div>
<!-- 4/24/19 -->

<div>{$_.date(new Date(2019, 3, 24, 23, 45), 'medium')}</div>
<!-- Apr 24, 2019 -->

_.number

function(number: Number, locale?: string)

Formats a number with the specified locale

<script>
  import { _ } from 'svelte-i18n'
</script>

<div>{$_.number(100000000)}</div>
<!-- 100,000,000 -->

<div>{$_.number(100000000, 'pt')}</div>
<!-- 100.000.000 -->