@hairy/react-i18-lib
v1.51.0
Published
Library for react
Readme
@hairy/react-i18-lib
Small helpers for react-i18next-based i18n.
Install
# ✨ Auto-detect
npx nypm install @hairy/react-i18-lib
# npm
npm install @hairy/react-i18-lib
# yarn
yarn add @hairy/react-i18-lib
# pnpm
pnpm add @hairy/react-i18-lib
# bun
bun install @hairy/react-i18-lib
# deno
deno install npm:@hairy/react-i18-libAPI
Trans()
Translate a string to a React node
Example:
<Trans i18nKey="hello" />
<Trans i18nKey="hello" name="John" />
<Trans i18nKey="hello" name="John" age={20} />
<Trans i18nKey="hello" name="John" age={<span>20</span>} />Directory structure
├── src/
│ └── index.ts
├── package.json
├── README.md
└── tsdown.config.tsSource
import type { ReactNode } from 'react'
import HTML from 'html-parse-stringify'
import { createElement } from 'react'
import { useTranslation } from 'react-i18next'
export interface TransProps {
i18nKey: string
[key: string]: ReactNode
}
/**
* Translate a string to a React node
*
* @example
* <Trans i18nKey="hello" />
* <Trans i18nKey="hello" name="John" />
* <Trans i18nKey="hello" name="John" age={20} />
* <Trans i18nKey="hello" name="John" age={<span>20</span>} />
*/
export function Trans({ i18nKey, ...additionalProps }: TransProps) {
const translation = useTranslation().t(i18nKey, additionalProps)
return renderNodes(HTML.parse(translation), additionalProps)
}
function renderNodes(tokens: HTML.Token[], values: Record<string, ReactNode>): ReactNode[] {
let index = 0
return tokens.map((token) => {
if (token.type === 'text')
return token.content
index++
const props = { ...token.attrs, key: index }
return token.voidElement
? (values[token.name]
? createElement('span', { key: index }, values[token.name])
: createElement(token.name, props))
: createElement(token.name, props, renderNodes(token.children, {}))
})
}Contributors
Published under the MIT license. Made by @Hairyf and community 💛
License
MIT License
Copyright (c) 2025-PRESENT Hairyf https://github.com/hairyf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
🤖 auto updated with automd
