Talkr is a super small i18n provider for React applications. It supports Typescript, has 0 dependencies, and is very easy to use.
Talkr is a super small i18n provider for React applications. It supports Typescript, has 0 dependencies, and is very easy to use.
__dynamicValue__.
countparameter to Talkr's translation function. Talkr will then chose the right word or sentence between
zero,
one,
two,
fewand
many.
🤓: Some languages have more complex plural rules, that may require these five options to offer a perfect user experience. For instance, Arabic handle
zero,one,two,numbers between 3 and 10andnumbers over 10as separate entities. If a language doesn't need all these subtleties - like english - you can only writezero,oneandmanyin the JSON file.
{ "hello": "hello", "feedback": { "error": "The connection failed", "success": "The connection succedeed" }, "user": { "describe": { "simple": "You are __name__", "complex": "You are __name__ and you like __hobby__" } }, "message-count": { "zero": "you don't have new messages", "one": "you have 1 message", "many": "you have __count__ messages" } }
languagesand your
defaultLanguage.
detectBrowserLanguage(see #Props).
import * as React from "react"; import ReactDOM from "react-dom"; import { Talkr } from "talkr"; import App from "./app"; import en from "./i18n/en.json"; import fr from "./i18n/fr.json";ReactDOM.render( , document.getElementById("root") );
T.
.between each key. Based on the JSON example above, we could print the sentence
The connection succedeedby simply writing
T("feedback.success")
import React from "react"; import { T } from "talkr";export default function MyComponent() { return ( <>
{T("hello")}
{T("feedback.success")}> ); }
__dynamicValue__)
import React from "react"; import { T } from "talkr";export default function MyComponent() { return ( <>
{T("user.describe.complex", { name: "joe", hobby: "coding" })}
> ); }
countproperty to the object
zero,
oneand
manyvalues to your JSON files.
import React, { useState } from "react"; import { T } from "talkr";export default function MyComponent() { const [count, setCount] = useState(0); return ( <>
{T("message-count", { count })}
setCount(count + 1)}>+1 > ); }
useLocale()
defaultLanguagesent to the provider.
import React, { useState } from "react"; import { T, useLocale } from "talkr";export default function MyComponent() { const { setLocale, locale } = useLocale(); return ( <>
{T("hello")}
{locale}
setLocale("fr")}>speak french > ); }
You can pass these props to Talkr's provider | |Type |Role | |----------------|-------------------------------|-----------------------------| |languages |
object|object containing all your json files. Typical format:
{en: {...}, fr: {...}}| |defaultLanguage |
string|default language of your app (a similar key must be included in the
languageprop) | |detectBrowserLanguage |
boolean|if
true, Talkr will automatically use browser language and override the
defaultLanguage. If the browser language is not included in your available translations, it will switch back to
defaultLanguage.|
🤓: The auto-detect language feature will always return a simple key such as 'fr' instead of 'fr_FR'. Keep things simple and always declare your languages with 2 letters.
You can access these props from Talkr's hook
useLocale()| |Type |Role | |----------------|-------------------------------|-----------------------------| |locale |
string|returns the current locale | |setLocale |
(locale: string) => void|function to update the locale
string|returns the App's default language| |languages |
object|returns all your JSON translations|
Listen to Noir Empire (dark rock & trip-hop):
https://www.facebook.com/Noir-Empire-101478985118273
https://www.instagram.com/noir_empire/
https://soundcloud.com/noir-empire
https://www.youtube.com/channel/UCwUU5Qa-AWKiNTGmyfh1LdQ
"The Way She Rocks" - Out November 13th, 2020.
DoneDeal0
Mouth logo made by emilegraphics from the Noun Project