svelte-i18n
Internationalization for Svelte
Usage
On the store
import { i18n } from 'svelte-i18n'
import { Store } from 'svelte/store'
let store = new Store()
store = i18n(store, {
dictionary: {
'pt-BR': {
message: 'Mensagem',
greeting: 'Olá {name}, como vai?',
greetingIndex: 'Olá {0}, como vai?',
meter: 'metros | metro | metros',
book: 'livro | livros',
messages: {
alert: 'Alerta',
error: 'Erro',
},
},
'en-US': {
message: 'Message',
greeting: 'Hello {name}, how are you?',
greetingIndex: 'Hello {0}, how are you?',
meter: 'meters | meter | meters',
book: 'book | books',
messages: {
alert: 'Alert',
error: 'Error',
},
},
},
})
store.i18n.extendDictionary({
'pt-BR': {
messages: {
warn: 'Aviso',
success: 'Sucesso',
},
},
'en-US': {
messages: {
warn: 'Warn',
success: 'Success',
},
},
})
store.i18n.setLocale('en-US')
On templates
Basic usage
<div>
{$_('message')}: {$_('messages.success')}
</div>
Current locale
The current locale is available via this.store.get().locale
.
Interpolation
<div>
{$_('greeting', { name: 'John' })}
{$_('greetingIndex', ['John'])}
</div>
Pluralization
<div>
0 {$_.plural('meter', 0)}
1 {$_.plural('meter', 1)}
100 {$_.plural('meter', 100)}
0 {$_.plural('book', 0)}
1 {$_.plural('book', 1)}
10 {$_.plural('book', 10)}
</div>
Utilities
<div>
{$_.upper('message')}
{$_.lower('message')}
{$_.capital('message')}
{$_.title('greeting', { name: 'John' })}
</div>