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.
preact-localization
Advanced tools
Simple localization for Preact.
This library is heavily inspired of preact-i18n and generally respects the same API, although it's not as complete. In particular, the following features are not available:
withText()
and intl()
wrappersFeatures include:
{{fields}}
in definition valuesThere are also a few other differences:
{ none, one, many }
)createContext
API instead of the legacy context APInpm install --save preact-localization
fr.json
:{
"news": {
"title": "Nouvelles du Monde",
"totalStories": {
"none": "Aucun article",
"one": "Un article",
"many": "{{count}} articles"
}
}
}
<IntlProvider>
:import { IntlProvider } from 'preact-localization'
import definition from './fr.json'
render(
<IntlProvider definition={definition}>
<App />
</IntlProvider>
)
<Text />
to translate string literals:import { Text } from 'preact-localization'
// Assume the "stories" prop is a list of news stories.
const App = ({ stories = [] }) => (
<div class="app">
<h1>
{/* Default fallback text example: */}
<Text id="news.title">World News</Text>
</h1>
<footer>
{/* Pluralization example: */}
<Text
id="news.totalStories"
plural={stories.length}
fields={{
count: stories.length
}}
/>
</footer>
</div>
)
That's it!
Rendering our example app with an empty definition (or without the Provider) will attempt to use any text contained within <Text>..</Text>
as fallback text.
In our example, this would mean rendering without a definition for news.title
would produce <h1>World News</h1>
.
If we provide a definition that has a title
key inside a news
object, that value will be rendered instead.
In our example, <footer>
is using <Text>
as a convenient way to do pluralization and templating. In our definition, news.totalStories
is an Object with pluralization keys. The values in that object will be selected based on an integer plural
prop passed to <Text>
.
Any definition value (including pluralization values) can contain {{field}}
placeholders. These placeholders get replaced with matched keys in an object passed as the fields
prop. In our example, the "many" plural form is such a template - it will render "5 articles"
when fields={{ count: 5 }}
.
The available forms for specifying pluralization values are as follows:
"key": { "singular": "apple", "plural": "apples" }
"key": { "none": "no apples", "one": "apple", "many": "apples" }
"key": ["apples", "apple"]
Taking <Text id="news.totalStories" ..>
from our example:
<.. plural={0}>
renders Aucun article
(no articles)<.. plural={1}>
renders Un article
(one article)<.. plural={2} fields={{ count: 2 }}>
renders 2 articles
<.. plural={3} fields={{ count: 3 }}>
renders 3 articles
In addition to <Text>
, <Localizer>
provides a ways to translate more than just display text - HTML attributes, component props, arbitrary Strings, etc.
FAQs
Preact internationalization
We found that preact-localization 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.