@tamkeentech/react-i18n-translator

light weight translation library for react

Usage no npm install needed!

<script type="module">
  import tamkeentechReactI18nTranslator from 'https://cdn.skypack.dev/@tamkeentech/react-i18n-translator';
</script>

README

react-i18n-translator

lightweight translation library for React.js

NPM JavaScript Style Guide

Installation

npm install --save react-i18n-translator

Getting Started

✣ Prepare Your Dictionary Files in .js or .json Format

lang/en.json

{
  "welcome": "Welcome {{name}}",
  "submit": "Submit",
  "errors": {
    "default": "Something went wrong!"
  }
}

lang/es.json

{
  "welcome": "Hola {{name}}",
  "submit": "enviar",
  "errors": {
    "default": "Algo salió mal!"
  }
}

lang/ar.json

{
  "welcome": "مرحبًا {{name}}",
  "submit": "ارسال",
  "errors": {
    "default": "حدث خطأ ما!"
  }
}

✣ Initilizing in Functional Component

import React, { useCallback, useState } from 'react'
import { lang, setLocale, init } from 'react-i18n-translator'
import arDictionary from './lang/ar.json'
import enDictionary from './lang/en.json'
import esDictionary from './lang/es.json'

init({
  resourses: [
    { locale: 'en', dictionary: enDictionary },
    { locale: 'es', dictionary: esDictionary },
    {
      locale: 'ar',
      dictionary: arDictionary,
      options: { isRTL: true }
    }
  ],
  defaultLocale: 'ar'
})

const App = () => {
 ...
  return ...
}

✣ Initilizing in Class Component

import React, { useCallback, useState } from 'react'
import { lang, setLocale, init } from 'react-i18n-translator'
import arDictionary from './lang/ar.json'
import enDictionary from './lang/en.json'
import esDictionary from './lang/es.json'

class App extends React.Component{
  constructor(props){
    super(props);
    init({
      resourses: [
        { locale: 'en', dictionary: enDictionary },
        { locale: 'es', dictionary: esDictionary },
        {
          locale: 'ar',
          dictionary: arDictionary,
          options: { isRTL: true }
        }
      ],
      defaultLocale: 'ar'
    })
  }

  render(){
    ...
    return ...
  }
}

Usage and Examples

✣ Basic Usage

import React from 'react'
import { lang } from 'react-i18n-translator'

const Button = () => <button>{lang.submit}</button>
const ErrorMsg = () => <button>{lang.errors.default}</button>

✣ Interpolation

import React from 'react'
import { interpolate } from 'react-i18n-translator'

const Hello = () => <h1>{interpolate('welcome', { name: "Omar" })}</h1>
const ErrorMsg = () => <h1>{interpolate('errors.default')}</h1>

✣ Usage with Memoized Functional Component

import React, { memo } from 'react'
import { lang, useSyncLang } from 'react-i18n-translator'

const Hello = () => {
  useSyncLang()
  return <h1>{lang.welcome}</h1>
}

export default memo(Hello)

✣ Usage with PureComponent

import React, { memo } from 'react'
import { lang, withSyncLang } from 'react-i18n-translator'

class Hello extends React.PureComponent {
  render() {
    return <h1>{lang.welcome}</h1>
  }
}

export default withSyncLang(Hello)

✣ Changing Language

import React, { useState } from 'react'
import { lang, setLocale} from 'react-i18n-translator'

const App = () => {
  const changeLanguage = useState()[1];
  const changeLang = (locale) => {
    setLocale(locale)
    changeLanguage(locale)
  }
  return (
    <div>
      {lang.welcome}
    </div>
  )
}

✣ Why I18n Translator?

  • Simplicity
  • No Limitation
  • lightweight

License

MIT © omaksousa