# svelte-i18n > Internationalization for Svelte ## 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. ```js import { locale, dictionary } 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') }) ``` --- ### The dictionary The `dictionary` store defines the dictionary of messages of all locales. ```js 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. ```html ``` `svelte-i18n` uses `formatjs` behind the scenes, which means it supports the [ICU message format](http://userguide.icu-project.org/formatparse/messages) for interpolation, pluralization and much more. ```html
{$_('greeting.message', { name: 'John' })} {$_('photos', { n: 0 })} {$_('photos', { n: 12 })}
``` ### 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. ```html
{$_('greeting.ask')}
``` #### `_.upper` Transforms a localized message into uppercase. ```html
{$_.upper('greeting.ask')}
``` #### `_.lower` Transforms a localized message into lowercase. ```html
{$_.lower('greeting.ask')}
``` #### `_.capital` Capitalize a localized message. ```html
{$_.capital('greeting.ask')}
``` #### `_.title` Transform the message into title case. ```html
{$_.capital('greeting.ask')}
``` #### `_.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](http://userguide.icu-project.org/formatparse/messages) documentation for all available. formats ```html
{$_.time(new Date(2019, 3, 24, 23, 45))}
{$_.time(new Date(2019, 3, 24, 23, 45), 'medium')}
``` #### `_.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](http://userguide.icu-project.org/formatparse/messages) documentation for all available. formats ```html
{$_.date(new Date(2019, 3, 24, 23, 45))}
{$_.date(new Date(2019, 3, 24, 23, 45), 'medium')}
``` #### `_.number` `function(number: Number, locale?: string)` Formats a number with the specified locale ```html
{$_.number(100000000)}
{$_.number(100000000, 'pt')}
```