Localization (i18n)

The locale setting is located in Web Settings on the admin section. Currently English and Portuguese languages are supported.

Text localization can be done using the Live own i18n service.

Import

import i18n from 'live/services/i18n'

Registering translations

import locale from 'live/init/global-locale'
import i18n from 'live/services/i18n'
import pt_br from './i18n/pt_br'
import en_us from './i18n/en_us'

var localeFiles = {
    pt_br: pt_br,
    en_us: en_us
}

// register current locale file
i18n.add(localeFiles[locale])

Locale files

i18n/pt_br.js
export default {
    values: {
       book: "livro"
    }
}

Parameters, pluralization, functions

Translations can received simple parameters that will be expanded for each locale string

i18n/pt_br.js
export default {
    values: {
        'Hi %{user} welcome to our book store': 
            'Olá %{user} bem vindo à nossa loja de livros'
    }
}

The parameters can also be used for a very simple pluralization logic

i18n/en_us.js
export default {
    values: {
        '%{count} books were added to cart': [
            [1, 1, 'a book was added to cart'],
            [2, 99, '%{count} books were added to cart'],
            [100, null, '99+ books were added to cart']
        ]
    }
}

Translation values can be functions that will be rendered as React components

i18n/en_us.js
export default {
    values: {
       'book-count': function BookCount ({ count }) {
           if (count === 0) return <span style={{color: 'red'}}>There are no books</span>
           if (count === 1) return <span style={{color: 'green'}}>There is one book</span>

           return <span> There are {count} books </span>
       }
    }
}

API

type i18nString = (translation: string) => string

// passing props to function values
type i18nComponent = 
    (translation: string, props: Record<string, any>) => React.ReactNode

type i18n = i18nString | i18nComponent

Usage

function SimpleExample ()  { return <span>{i18n('book')}</span> }

function Greetings() { 
  return 
    <span>
      {i18n('Hi %{user} welcome to our book store', { user: 'John Doe' })}
    </span> 
}

function CartInfo() {
  return <span>{i18n('%{count} books were added to cart', { count: 123 })}</span>
}

function BookCounter () {
  const [ count, setCount ] = React.useState(0)

  return <div onClick={() => setCount(s => s + 1)} >{i18n('book-count', { count })}</div>
}

Last updated