Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-tiny-i18n
Advanced tools
A tiny (~500B) i18n implementation for handling translations in React
📝 A tiny (~500B) i18n implementation for handling translations in React
npm i react-tiny-i18n
or yarn add react-tiny-i18n
See a demo here: https://dericgw.github.io/react-tiny-i18n/
First, you need some translations that take the shape of an object. This could be from an API or a JSON file or even a plain ole' JavaScript object. You can also go as many levels deep as you want. For instance, you may want to have a key for each page and then under that, you can have a key for each section within that page, and so on and so forth...
<Languages />
Once you have your translations, you need to make them available to your application. In order to
do that, use the Languages
component which takes two props: langauges
and defaultLanguage
:
const languages = {
en: {
home: {
intro: 'Hi, {{firstName}}!'
}
},
fr: {
home: {
intro: 'Salut, {{firstName}}!'
}
}
};
<Languages languages={languages} defaultLanguage="en">
<Home />
</Languages>
Once you make the translations available, you want to display them. In order to do that, you can
use dot notation in order to represent the path of the translation inside of the <T>
component
or the t()
function:
Using the <T>
component:
const Home = () => (
<div>
<h4><T replacements={{ firstName: 'Debo' }}>home.intro</T></h4>
</div>
);
The child of the
<T>
component is the dot notated path. Also, the interpolated text is not wrapped in an element - only the text is returned.
Using the t()
function
const Home = () => {
// We can use this as a function and not just a React Component
const text = t('home.intro', {
firstName: 'Debo'
});
return (
<div>
<h4>{text}</h4>
</div>
);
};
The
t()
function can be used anywhere in your application.
<Switcher />
The last thing you will need is a way to switch between languages. For this, you can use the
<Switcher />
component. This is a select component that has all of the languages listed as options.
Whenever the select component changes, all of the text within the app is updated to the new language.
The
<Switcher />
component takes an number of props and passes those props to theselect
component. This allows for things such as styling, etc.
FAQs
A tiny (~500B) i18n implementation for handling translations in React
We found that react-tiny-i18n demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.