0.0.13 • Published 3 years ago

loctus v0.0.13

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Simple and easy-to-use localization for JavaScript applications.

Set up

Create localize function as follows:

const localize = createLocalizerFactory({
    en: {
        greetGuest: 'Greetings, Guest',
        greetUser: params => `Greetings, ${params.firstName}`,
        greetClient: (params, localize) => params ? localize('greetUser', params) : localize('greetGuest'),
    ka: {
        greetGuest: 'გამარჯობა სტუმარო',
        greetUser: params => `გამარჯობა ${params.firstName}`,
        greetClient: (params, localize) => params ? localize('greetUser', params) : localize('greetGuest'),
    },
})('en')

Above, createLocalizerFactory takes an object where keys are mapped to objects containing translations. Translation can either be static or dynamic.

  • Static translations are plain strings.
  • Dynamic translations take payload and localize function and return string.

createLocalizerFactory returns memoized function which in turn takes default locale and when called, returns localize function.

Usage

Considering the above set-up:

  • localize('greetGuest') returns "Greetings, Guest" - because default locale is "en".
  • localize('gretGuestt') returns "gretGuestt" - because neither there is such key as "gretGuestt" under "en" translations above, nor a fallback is specified.
  • localize('gretGuestt', null, 'Hey there') returns "Hey there" - because, although there is no such key as "gretGuestt" under "en" translations above, but "Hey there" is specified as a fallback.
  • localize('greetGuest', 'ka') returns "გამარჯობა სტუმარო" - because, although default locale is "en", a custom "ka" locale is passed to localize as a fourth argument.
  • localize('greetGuest', null, '', 'ka') returns "გამარჯობა სტუმარო" - because, although default locale is "en", a custom "ka" locale is passed to localize as a fourth argument.
  • localize('greetClient') returns "Greetings, Guest" when the underline function evaluates.
  • localize('greetClient', { firstName: 'user123' }) returns "Greetings, user123" when the underline function evaluates.
0.0.13

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

6 years ago

0.0.1

6 years ago