Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
intl-schematic
Advanced tools
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Raiondesu/intl-schematic/main/logo/Dark%20Logo.svg"> <source media="(prefers-color-scheme: light)" srcset="https://raw.github
A tiny framework-agnostic i18n library (1kb gzip, zero-dependency) that allows to localize and format strings with infinitely expandable functionality.
See a simplified example below and don't be afraid to take a look into the sources to find out more.
// In the real world, this function would probably contain
// a dynamic import of the required translation document
const getDocument = () => ({
"hello": "Hello, World!"
});
t()
)import { createTranslator } from 'intl-schematic';
const t = createTranslator(getDocument);
// OR
const t = createTranslator(getDocument, [
// Add optional plugins here
// they will be applied to translations in corresponding order
]);
console.log(t('hello')); // `Hello, World!`
Many other i18n libraries require deep integration into a specific UI-framework.
This, however, is not the case with intl-schematic
, due to its framework-agnostic and isomorphic architecture.
The only requirement to make it behave "natively" for a certain framework,
is to simply add a reactive dependency to a closure of getLocaleDocument
function (the first argument of createTranslator
).
Here's an example in a "reactive pseudocode":
// Define a reactive variable;
// Solid
const userLocale = createSignal(new Intl.Locale('en'));
// Vue
const userLocale = ref(new Intl.Locale('en'));
const fetchDocument = () => {
// Somehow get a value from the reactive variable
const language = get(userLocale).language;
// Suppose you have a translation document in the `locales` folder
return import(`/locales/${language}.json`);
};
// Create a reactive variable for a translation document
const currentDocument = createSignal();
// Then, in a reactive context (like a UI component)
const t = createTranslator(
// If this function is invoked in a reactive context,
// the framework will most likely track this as a reactive dependency
() => get(currentDocument)
);
// useEffect/watch/computed
createEffect(() => {
// Since calling `fetchDocument`
// involves getting a value from a reactive variable,
// this is tracked as a reactive dependency and will re-run accordingly
fetchDocument()
.then(newDocument => set(currentDocument, newDocument));
});
<p>{t('some key')}</p> // Some text
// Then change the locale
set(userLocale, new Intl.Locale('es'));
// The text re-renders automatically once the new translation document is fetched
<p>{t('some key')}</p> // Algún texto
Even though something like this is fairly trivial to implement,
given a basic knowledge of any specific UI-framework,
intl-schematic
still offers some "in-house"-made integrations:
@intl-schematic/solid
- reactive adapter for solid-js
intl-schematic
and user-defined plugins@intl-schematic/vue
- Work in progressIf you want an integration for your favorite framework, feel free to contibute or create an issue!
This is by far the main strength of the library.
Translating keys relies on simple key-value lookup, and most i18n libraries add many unnecessary features on top of this primitive functionality.
intl-schematic
instead provides a way to extend both its functionality and type definitions in a comprehensive enough way,
so that anyone can pick and choose what exact features are needed for their project without any bloat whatsoever.
In other words, plugins allow to almost infinitely expand the functionality of intl-schematic
.
To find out more, see the main plugins readme.
Current list of all official plugins is as follows:
@intl-schematic/plugin-defaults
@intl-schematic/plugin-arrays
(included in defaults)
@intl-schematic/plugin-functions
@intl-schematic/plugin-locale
(included in defaults)
Intl.Locale
instance@intl-schematic/plugin-nested
@intl-schematic/plugin-processors
(included in defaults)
You might want to install the default plugin collection:
npm i @intl-schematic/plugin-defaults
These allow to use standard Intl features,
like DateTimeFormat
,
PluralRules
and DisplayNames
,
as well as cross-reference keys and many other features.
import { createTranslator } from 'intl-schematic';
import { defaultPlugins, defaultProcessors } from '@intl-schematic/plugin-defaults';
const getDocument = () => ({
price: {
// Processor name - number - means process with Intl.NumberFormat
number: {
// Intl.NumberFormat options
style: "currency",
currency: "USD",
currencyDisplay: "symbol",
trailingZeroDisplay: "stripIfInteger"
},
input: 0 // fallback
}
});
const getLocale = () => new Intl.Locale('en');
const t = createTranslator(getDocument, defaultPlugins(
getLocale,
defaultProcessors
));
console.log(t('price', 123)); // "$123"
intl-schematic
, as well as its plugins, defines a JSON-schema API designed specifically to allow type-checking JSON translation documents.
To define a JSON-schema for your translation documents, simply create a .schema.json
file with this template:
// translation.schema.json
{
"$ref": "./node_modules/intl-schematic/translation.schema.json",
"additionalProperties": {
"anyOf": [
// Definition for the default string key-value pair
{
"$ref": "./node_modules/intl-schematic/property.schema.json",
}
// Add references to more allowed types for your schema
/* for example, @intl-schematic/plugin-processors definition:
{
"$ref": "./node_modules/@intl-schematic/plugin-processors/property.schema.json"
}
*/
]
}
}
And then use it in your translation document:
// en.json
{
// Path to the schema from the example above
"$schema": "./translation.schema.json",
"key": "Translation"
}
import translation from './en.json';
const t = createTranslator(() => translation);
// Etc., see example at the start of this file
Note: the
$schema
key will be automatically excluded
from type hints fort()
for your convenience!
property.schema.json
Not all plugins have a property.schema.json
file, not all of them need to.
Here's a list of the ones that do:
@intl-schematic/plugin-arrays
(included in defaults)
@intl-schematic/plugin-processors
(included in defaults)
FAQs
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Raiondesu/intl-schematic/main/logo/Dark%20Logo.svg"> <source media="(prefers-color-scheme: light)" srcset="https://raw.github
The npm package intl-schematic receives a total of 49 weekly downloads. As such, intl-schematic popularity was classified as not popular.
We found that intl-schematic demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.