Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-simple-i18n
Advanced tools
Define languages data first
const langData = {
enUS: {
projects: 'Projects',
cars: 'This car is %s%, that car is %s%',
nav: {
home: 'Home',
},
},
zhCN: {
projects: '项目',
cars: '这辆车是%s%,那辆车是%s%',
nav: {
home: '首页',
},
},
}
Use React hook
import React, { Component } from 'react'
import { createI18n, I18nProvider, useI18n } from 'react-simple-i18n'
const Demo = () => {
const { t, i18n } = useI18n()
return (
<div>
<p>{t('projects')}</p>
<p>{t('cars', 'BMW', 'TOYOTA')}</p>
<p>{t('nav.home')}</p>
<button onClick={() => i18n.setLang('enUS')}>English</button>
<button onClick={() => i18n.setLang('zhCN')}>中文</button>
</div>
)
}
const App = () => (
<I18nProvider i18n={createI18n(langData, { lang: 'enUS' })}>
<Demo />
</I18nProvider>
)
Creates an i18n
object for I18nProvider
data
Language data object, see usage aboveoptions
lang
Initial language to usedefaultText
Function that returns default text if i18n doesn't exist. It takes a single argument which is the i18n key string.i18n
t(key, ...args)
Get translation by language name
key
{string} key of a translation fieldargs
{[string]} strings to replace %s%
in the fieldgetLang()
Get current language
enUS
setLang(lang)
Set language by language name
lang
{string} language name, such as enUS
addLangData(langData)
Async add language data, allow adding multiple languages once
langData
{object} Language object { enUS: { key: 'value' } }
listen(handler)
Add listener to language change
handler
{func()} function which will be called when language changeunlisten(handler)
Unbind a listener of language change
handler
{func()} function which will be called when language changeMakes i18n
available to withI18n
HOC and useI18n
hook
i18n
I18n object created by createI18n
A React hook that returns an object with t
and i18n
.
MIT
FAQs
React i18n solution with context API
The npm package react-simple-i18n receives a total of 97 weekly downloads. As such, react-simple-i18n popularity was classified as not popular.
We found that react-simple-i18n demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.