New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@tamkeentech/react-i18n

Package Overview
Dependencies
Maintainers
5
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tamkeentech/react-i18n

light weight translation library for react

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
5
Weekly downloads
 
Created
Source

@tamkeentech/react-i18n

lightweight translation library for React.js

NPM JavaScript Style Guide

Installation

npm install --save @tamkeentech/react-i18n

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": "حدث خطأ ما!"
  }
}

✣ Initializing 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 ...
}

✣ Initializing 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>
  )
}

API

AttributeTypeDescription
langobjectAn object that holds the selected dictionary
initfunctionFor initializaion
addLocalefunctionAdds new dictionary. It takes two parametes: 1- locale key. 2- dictionary object
setLocalefunctionChanges the selected locale to a new one. Takes one parameter: locale key
interpolatefunctionTakes two parameters: 1- path to the targeted dictionary key. 2- an object that holds the variables
isRTLbooleanFlag for checking if the current direction is RTL
isLtrbooleanFlag for checking if the current direction is LTR

Why I18n Translator?

  • Simplicity
  • No Limitation
  • lightweight

License

MIT © omaksousa

Keywords

FAQs

Package last updated on 01 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc