
Research
/Security News
npm Author Qix Compromised via Phishing Email in Major Supply Chain Attack
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
easy-translation
Advanced tools
Easy React Translations Component. Translates all text on screen using JSON translation files.
Easily add language translations to your React/Next application.
This project was built using React (version 19.0.0).
To install the library, run:
$ npm i easy-translation
The LanuageProvider must be added to the top level of your application as it sets up the required Context.
Props:
url
(Optional)
Type | Description |
---|---|
string | The url to language files directory. If not included will look at in your server "locale" directory |
supported
(Required)
Type | Description |
---|---|
Array[string] | List of supported languages |
Example:
import { LanguageProvider } from "easy-translation";
function main() {
return (
<LanguageProvider
url="<http path to language directory>"
supported={["en", "fr"]}
>
<div>...</div>
</LanguageProvider>
);
}
export default main;
function main() {
return (
<Lang>
<div>...</div>
</Lang>
);
}
export default main;
"use client";
import React from "react";
import { LanguageProvider } from "easy-translation";
function Lang({ children }: { children: React.ReactNode }) {
return (
<LanguageProvider
url="<http path to language directory>"
supported={["en", "fr"]}
>
{children}
</LanguageProvider>
);
}
export default Lang;
Language files are JSON files that are seperated into sections.
Language files must be localed in at publicly accessable url. http://someurl.com/locale
If
url
is not supplied, falls back to '/Public/Locale/'
Language file names must be named based on the supported language:
Files:
The first section for each language file MUST have the "@" section. See below for an example.
Usage:
{
"@": {
"key": "This is the text to be translated"
}
}
{
"@": {
"key": "This is the text | to translate to that is indexed"
}
}
{
"@": {
"key": "This is a token [$0] and some more tokens [$1] [$2]"
}
}
Example:
en.json
{
"@": {
"title": "This is an English language file",
"multiple": "This is a string | That can be indexed",
"tokens": "This is an indexed string | With tokens [$0] [$1]"
},
"section1": {
"title": "This is section one",
"tokens": "This is a string with tokens [$0] [$1]"
},
"section2": {
"title": "This is section two",
"subtitle": "This is section two"
}
}
This is the most common way to use the translation library.
Usage:
useTranslation("section");
Options:
section
(Optional)
Type | Default value | Description |
---|---|---|
string | undefined | All translations will use a passed "section" as default |
Usage:
translate("@text@", "section", ["tokens"])[idx];
Options:
@text@
(Required)
Type | value | Description |
---|---|---|
string | "@text@" | The translation key that must match the key in the language file |
section
(Optional)
Type | value | Description |
---|---|---|
string | "section" | Section of the language file this key exists. |
if "-" is passed in the section field. Looks in the current section.
tokens
(Optional)
Type | value | Description |
---|---|---|
Array[strings] | ["token1", "token2"] | Token strings. Language matches tokens on an index. See language file above. |
translate("@title@");
translate("@multiple@")[0];
translate("@multiple@")[1];
translate("@tokens@", "section1", ["token1", "token2"]);
Example:
import { useTranslation } from "easy-translation";
function MyComponent() {
const { translate: t } = useTranslation("section1"); // Switch default to Section1
return (
<div>
<p>{t("@title@")}</p>
<p>{t("@title@", "@")}</p>
<p>{t("@multple@", "@")[1]}</p>
<p>{t("@tokens@", "@", ["token1", "token2"])[1]}</p>
<p>{t("@title@", "section2")}</p>
</div>
);
}
export default MyComponent;
This function can be used to translate an Array of Translation Objects
Usage:
group([{ text: "@text@", section: "section", tokens: ["tokens"] }]);
Options:
object
(Required)
Type | value | Description |
---|---|---|
Array[{}] | [{text:"@text@"}] | Pass an array of Translation Objects |
import { useTranslation } from "easy-translation";
function MyComponent() {
const { group: t } = useTranslation();
const translations = t([{ text: "@title@" }, { text: "@subtitle@" }]);
return (
<div>
<p>{translations.title}</p>
<p>{translations.subtitle}</p>
</div>
);
}
export default MyComponent;
import { useTranslation } from "easy-translation";
function MyComponent() {
const { group: t } = useTranslation();
const translations = t([{ text: "@multiple@" }]);
return (
<div>
<p>{translations.multiple[0]}</p>
<p>{translations.multiple[1]}</p>
</div>
);
}
export default MyComponent;
import { useTranslation } from "easy-translation";
function MyComponent() {
const { group: t } = useTranslation("section1");
const translations = t([
{ text: "@tokens@", "-", ["token1", "token2"] }
]);
return (
<div>
<p>{translations.tokens}</p>
</div>
);
}
export default MyComponent;
Example:
import { useTranslation } from "easy-translation";
function MyComponent() {
const { group: t } = useTranslation("Section2");
const translations = t([
{ text: "@title@" },
{ text: "@subtitle@" },
{ text: "@tokens@", category: "@", tokens: ["token1", "token2"] },
]);
return (
<div>
<p>{translations.title}</p>
<p>{translations.subtitle}</p>
<p>{translations.tokens[0]}</p>
<p>{translations.tokens[1]}</p>
</div>
);
}
export default MyComponent;
The Translation Component can also be used. This component translates @text@ tokens by passing JSX
Options:
category
(Required)
Type | Default value | Description |
---|---|---|
string | undefined | The translation key that must match the key in the language file |
@text@
(Required)
Type | Default value | Description |
---|---|---|
string | "" | All translations will use a passed "section" as default |
Example:
"use client";
import { Translation } from "easy-translation";
function MyComponent() {
return (
<div>
<Translation category={"Section2"}>
<h3>@title@</h3>
<p>@subtitle@</p>
</Translation>
</div>
);
}
export default MyComponent;
FAQs
Easy React Translations Component. Translates all text on screen using JSON translation files.
The npm package easy-translation receives a total of 0 weekly downloads. As such, easy-translation popularity was classified as not popular.
We found that easy-translation 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.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.