2.2.0 • Published 1 year ago

react-multi-lang v2.2.0

Weekly downloads
448
License
MIT
Repository
github
Last release
1 year ago

react-multi-lang

React Multi-language component.

Works with React and React Native

Installation

npm i -S react-multi-lang

Usage

Hook

See the example folder for better understanding

import React from 'react'

// Translation Hook
import { setTranslations, setDefaultLanguage, useTranslation } from 'react-multi-lang'
import pt from 'pt.json'
import en from 'en.json'

// Do this two lines only when setting up the application
setTranslations({pt, en})
setDefaultLanguage('en')

const App: React.FC = () => {
  const t = useTranslation()
  return (
    <div>
      {t('home.Title')}
      {t('Hello', {name: 'João'})}
    </div>
  )
}

export default App

Higher order component

import React from 'react'

// Translation Higher Order Component
import { setTranslations, setDefaultLanguage, withTranslation } from 'react-multi-lang'
import pt from 'pt.json'
import en from 'en.json'
import type { T } from 'react-multi-lang'

// Do this two lines only when setting up the application
setTranslations({pt, en})
setDefaultLanguage('en')

type Props = {
  t: T
}

class SomeComponent extends React.Component<Props> {
  render () {
    const { t } = this.props
    return (
      <div>
        {t('home.Title')}
        {t('Hello', {name: 'João'})}
      </div>
    )
  }
}

export default withTranslation(SomeComponent)

Auto update functions

useTranslation(basePath)

React hook that returns the t function

ParamsTypeDescriptionRequired
basePathstringtranslation base path used to identify all the next requested translationsno

withTranslation(component, basePath)

HOC that injects the translation function into the component

ParamsTypeDescriptionRequired
componentReact ComponentReact component that requires the translation functionyes
basePathstringtranslation base path used to identify all the next requested translationsno

Translation Method

t(path, params)

Returns the translation for the requested path

ParamsTypeDescriptionRequired
pathstringtranslation path that identifies the textyes
paramsobject{'param': 'value', ...} each parameter will be set on the string in its correct locationno

Exported Methods

setDefaultTranslations(translations)

Sets the translations

ParamsTypeDescriptionRequired
translationsobject{'key': 'translations', ...}yes

setTranslations(translations)

Same as setDefaultTranslations, but this will update all components using translations

ParamsTypeDescriptionRequired
translationsobject{'key': 'translations', ...}yes

setDefaultLanguage(key)

Sets the default application language

ParamsTypeDescriptionRequired
keystringtranslation key, in this example 'en' or 'pt'yes

setLanguage(key)

Same as setDefaultLanguage, but this will update all components using translations

ParamsTypeDescriptionRequired
keystringtranslation key, in this example 'en' or 'pt'yes

getLanguage()

Returns the current selected language

t(key, params)

t(path, params)

Returns the translation for the requested path

ParamsTypeDescriptionRequired
pathstringtranslation path that identifies the textyes
paramsobject{'param': 'value', ...} each parameter will be set on the string in its correct locationno
2.2.0

1 year ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago