UNPKG

@dreesq/serpent

Version:

An express wrapper for developing fast web applications

55 lines (39 loc) 1.54 kB
# I18n The i18n plugin deals with translation of strings. The i18n object may be accessed using `client.i18n`. Note that the i18n plugin must be enabled in the server component. Plugin is being initialized in the `setup` stage of client as following: ```js import Serpent from '@dreesq/serpent-client'; import axios from 'axios'; import sio from 'socket.io-client'; const client = new Serpent({ handler: 'http://localhost:3001/handler', actions: 'http://localhost:3001/actions', axios, sio, i18n: { store: true, // If messages should be stored in localStorage // namespaces of keys to be loaded using getTranslations, // note that these are validated on server by config load: [ 'validation' ] } }); client.setup().then(() => console.log(client.i18n.t('validation.required'))); ``` #### Available methods: ```client.i18n.t(key, params)``` - Given a translation key and a list of parameters it attempts to translate the string. ```js const msg = client.i18n.t('test', { param: '1' }); // msg will be [empty string] if key does not exist or the translated string ``` ```client.i18n.setLocale(locale)``` - Attempts to change authenticated's user locale. If not authenticated, this method would return an error. ```js const {errors, data} = await client.i18n.setLocale('en'); ``` ```client.i18n.getTranslations(keys, locale)``` - Used on client bootstrap to load the translations map inside localStorage, may be called again to overwrite translations.