Coder Social home page Coder Social logo

react-localize-alias's Introduction

Library for localization of React applications with support for templates and HTML

Installation

yarn add react-localize-alias
or
npm i react-localize-alias

Simple

import {
  initLocalize,
  Translate,
  setActiveLanguage,
  getTranslate,
} from 'react-localize-alias'

const translations = {
  en: {
    hello: 'Hellow bro',
    'auth.title': 'Welcome to <b>site</b>',
    apple_count: 'Apple: ${count}',
  },
  ru: {
    hello: 'Привет, друг',
    'auth.title': 'Добро пожаловать на <b>сайт</b>',
    apple_count: 'Яблок: ${count}',
  },
}

export const App = () => {

  useEffect(() => {
   initLocalize({
         languages: [
           { code: 'en', name: 'English' },
           { code: 'ru', name: 'Russian' },
         ],
         translations,
         options: {
           defaultLanguage: 'en',
           // if the alias for the current language is not found. this function will be called
           onMissing: data => console.log(data),
           // this function will be called whenever the alias is rendered. here you can change the aliases that were set in the components
           onAlias: (alias) => alias,
         },
       })
  }, [])

  console.log(getTranslate('hello'))

  return (
    <>
      <button type="button" onClick={() => setActiveLanguage('en')}>
        set EN
      </button>
      <button type="button" onClick={() => setActiveLanguage('ru')}>
        set RU
      </button>

      <Translate id="hello" />

      <Translate
        id="apple_count"
        data={{ count: 123 }}
      />

      <Translate id="not_found_alias" /> 

    </>
  )
}

Documentation

Init Localize

import { initLocalize } from 'react-localize-alias'

const translations = {
    en: {
        hello: 'hi',
        auth.title: 'welcome to site',
        auth_button: 'next ${numebr}'
        },
    ru: {
        hellow: 'привет',
        auth.title: 'спс за регу',
        auth_button: 'далее ${number}'
    }
}

initLocalize({
    languages: [
        { code: 'en', name: 'English' },
        { code: 'ru', name: 'Руский' },
    ],
    translations,
    options: {
      defaultLanguage: 'en',
      onMissing: data => console.log(data),
    },
})

Using

import { Translate, getTranslate } from 'react-localize-alias'

<Translate id='auth.title' />
getTranslate('auth.title')

// get castom value 
<Translate id='auth_button' data={{ numebr: 5 }}/> // -> next 5
getTranslate('auth_button', { data: { number: 5 } } })

Change language

import { setActiveLanguage } from 'react-localize-alias'

const App = () => {
    const handlerLanguage = () => {
        setActiveLanguage('en')
    }

    return <div onClick={handlerLanguage}>set en</div>  
}

react-localize-alias's People

Contributors

bulhiczar avatar

Stargazers

shadowplay1 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.