Skip to content

dev-intellisoft/languee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DESCRIPTION

It is a very simple and scalable way to work with multi language projects.
It works with node js, react, react native, vue and we think should work with the project you are building just now ; ) !

languee is released under the MIT license. Current npm package version. Current Appveyor build status.

VIDEO TUTORIAL

I hope this 20 minutes video can save years of your life ... ;)

https://www.youtube.com/watch?v=tB6Sv4WS9WY&t=38s
INTRODUCTION

It was created by the need I had to work with multi language projects

Every time I need to type "txt_username" or "lbl_password" it was pain.

INSTALLATION

yarn add languee or npm i languee

PROJECT DIRECTORY

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

USAGE

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'
TIPS

*** 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'
CODING
Now it is time to code... To use it simply ``` console.log(t(`Hello World`)) // this will print "Olá mundo!" because my locale is pt_BR ```

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`)

PARAMETERS

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!`))
FUNCTIONS DESCRIPTION

  • 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.

* setMaps - This will map some location to another for example if you are create a project in Brazilian Portuguese or Chinese and you want to display every locale from Portuguese(Portugal, Angolan, Macau, ...) or Chinese(Taiwanese, Hong Kongers) to a default locale you can use this function.

For example:

setMaps('pt_BR', ['pt_PT', 'pt_AO', ... ])

setMaps('zh_CN', ['zh_TW', 'zh_HK', ..... ])

Any question please mail to [email protected] ;)

About

locale texts translation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published