1.3.1 • Published 4 years ago

languee v1.3.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

yarn add languee or npm i languee

After the installation, you need to create a folder called "locales" in the root of your project and inside it locales folders.

For example:

  • en_US for United State English
  • pt_BR for Brazilian Portuguese
  • zh_CN for Chinese

Once all file structure set, you can create *.json files for every locale.

For example:

//hello.json
{
    "Hello World":"Olá mundo!",
    "Hello {name}!":"Oi {name}!"
}

Once you get here you you might have a file structure like this:

.
├── index.js
└── locales
    ├── en_US
    │   └── hello.json
    ├── pt_BR
    │   └── hello.json
    └── zh_CN
        └── hello.json

if you use mobile and/or web project you need assemble you locale files by run yarn run languee or npm run languee

First of all you need to instantiate languee You can do that by typing ...

const { t } = require('languee')

you can also import it like this ...

import { t } from 'languee'

*** If you are using it for web or mobile application you may need assemble locales folder by execute the follow command yarn run languee or npm run languee

Please react and react native programmers don't forget the tip

* If you are using React Native

const { t } = require('languee/native')

you can also import it like that

import { t } from 'languee/native'

You can also change computer locale to another locale by simply calling "setLocale" function.

For example:

const { t, setLocale } = require('languee')
//now I can simply
setLocale(`zh_CN`)

console.log(t(`Hello World`)) //this should print 你好世界!

You can also change the file bears all the translations by calling setFile function

For example: setFile(`hello`)

Passing parameter

console.log(t(`Hello {name}!`, { name:"Wellington" })) // this should print "Hello Wellington!"
console.log(t(`Hello World`, {}, `zh_CN`)) // this should print "你好世界!" 

Full example:

const { t, setLocale, setFile } = require('languee')

setLocale(`pt_BR`)
setFile(`hello`)

console.log(t(`Hello World`))

console.log(t(`Hello World`, {}, `zh_CN`))

console.log(t(`Hello {name}!`, { name:"Wellington" }))

console.log(t(`Hello my friend!`))
  • t - This is the function you are going to use everywhere it can take till three arguments the first will be text you want to display:

    For example: t('Hello!')// will print "Hello!"

    The second argument of "t" function will be a object the can be converted to binds strings to the function:

    For example: t('Hello, {name}!', { name:"Wellington" }) // will print "Hello, Wellington!

    The third one is the locale that override the chose locale.

    For example:

    t('Hello, {name}!', { name:"Wellington" }, 'zh_CN')//will print "你好, Wellington!"

    * Donit worry if if you locales folder was not correctly set "t" function will return exactly the text you typed.

  • setLocale - It will overried any previously set locale and use it system wide.

  • setDirectory - This function will change the directory from "locales" to another directory you want.

  • setFile - The default locale *.json file which bears the translations is "main.json" but if you want to set another file you can use this function.

Any question please mail to wellington@programer.com.br ;)

1.3.1

4 years ago

1.3.0

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago