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.
simstate-i18n
Advanced tools
simstate-i18n
is a strongly-typed React i18n library based on simstate.
npm install --save simstate-i18n
My blog ddadaal.me is created with simstate-i18n.
Try changing the language by the LanguageSelector.
This library requires setting up necessary files and folders before using the components and store.
Check out the example folder for recommended file structure.
.
├── App.tsx
└── i18n
├── cn.ts
├── en.ts
└── index.ts
i18n
(or anything you want) on your src
folder{language}.ts
, for example cn.ts
, en.ts
) under i18n
folder for each language to support with the following content:
// src/i18n/en.ts
// example: example/i18n/{en,cn}.ts
export default {
// The id of the language. Any unique string is acceptable.
id: "en",
// The name of the language
name: "English",
// The definitions of id and text template.
// Use "{}" as the placeholder for dynamically set text or React component.
definitions: {
navbar: {
home: "Home",
about: "About",
},
content: "Current time: {}. Thanks for using simstate-i18n."
}
}
index.ts
under the i18n
folder with the following content:// src/i18n/index.ts
// example: example/i18n/index.ts
// Imports
import cn from "./cn";
import { createI18nContext, I18nStoreDef, I18nStore } from "simstate-i18n";
import { useStore } from "simstate";
// Load English dynamically to support code splitting
const en = () => import("./en").then((x) => x.default);
// The actual Language type,
// might be useful when the Language object is extended and the extra properties are needed
export type Language = typeof cn;
// Create the I18nContext with cn as the default language.
export const i18nContext = createI18nContext(cn, { en });
// Destruct and export the members for easier usage
// Recommendation: rename the idAccessor to lang for shorter typing
export const { getLanguage, idAccessor: lang } = i18nContext;
// This function is shortcut to use I18nStore,
// and also specify the exact types of Language objects,
// which helps avoid type casting.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function useI18nStore() {
return useStore(I18nStore) as I18nStoreDef<Language["definitions"], Language>;
}
simstate
store with the i18nContext instance.// example: example/App.tsx
import { i18nContext } from "./i18n";
import { createI18nStore } from "simstate-i18n";
import { StoreProvider } from "simstate";
const Root = () => {
// Create global i18nStore instance.
const [i18nStore] = useState(() => createI18nStore(i18nContext));
return (
<StoreProvider stores={[i18nStore]}>
<App />
</StoreProvider>
)
}
When the configurations are completed and the global I18nStore is injected, it is possible to use the provided components and store.
<LocalizedString />
or <Localized />
component are used in place of raw texts to provide i18n capabilities to anywhere a React component can be. It shows the text of the specified id of the current language.
All LocalizedString components will be updated when the current language is changed.
Example:
// import the idAccessor (renamed to lang) from i18n folder
// which is used to access the id of a text strongly-typedly.
import { lang } from "./i18n";
import { LocalizedString } from "simstate-i18n";
// Set the id of text as accessing properties of the lang object
// If the text has placeholders {},
// set the replacements prop with the replacement elements
// that will be inserted into the placeholders in order.
<LocalizedString id={lang.content} replacements={[Date.now()]} />
// The same as above but name is shorter
<Localized id={lang.content} replacements={[Date.now()]} />
This hook is used to suffice more advanced usage.
The following example behaves the same as the LocalizedString
example above, and will also be updated when the current language is updated.
Example:
import { lang } from "./i18n";
import { useLocalized } from "simstate-i18n";
const Component = () => {
const content = useLocalized(lang.content, [Date.now()]);
return content;
}
The I18nStore instance of current provider scope can be acquired with useStore
function provided by simstate
, which can be used to control the current language as well as getting some information.
Example:
import { I18nStore } from "simstate-i18n";
import { useStore } from "simstate";
const ControlPanel = () => {
const i18nStore = useStore(I18nStore);
return (
<div>
<p>
Current language: {i18nStore.currentLanguage.name}
</p>
{
i18nStore.switchingToId && `Switching to ${i18nStore.switchingToId}`
}
<ul>
{allLanguages.map((lang) => (
<li key={lang.id}>
<a onClick={() => i18nStore.changeLanguage(lang.id)}>
{lang.name}
</a>
</li>
))}
</ul>
</div>
)
}
simstate
: A Strongly-typed React State Management Tool Favoring React Hooks and TypeScript.
Strongly Typed i18n with TypeScript (English): This article of mine talks about the problems of using raw string as the text ids, and also introduces a proxy-based text id generation method which is now replaced with another method (src/i18nContext.ts) which should have better performance.
MIT © ddadaal
FAQs
A Strongly-typed react i18n library based on simstate
The npm package simstate-i18n receives a total of 1 weekly downloads. As such, simstate-i18n popularity was classified as not popular.
We found that simstate-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.