
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
@drewjbartlett/i17n
Advanced tools
[](https://github.com/drewjbartlett/i17n/actions/workflows/core.yml)
A lightweight (1.4kB) internationalization library tailored for startups and small-to-medium sized projects, offering a simple and efficient solution for managing translations in a single language without the overhead of a bloated library.
{ "key": "value" }
translations t('key')
{ "key": "child": "value" }
translations t('key.child')
t('Hello, {name}!', { name: 'Drew' })
t('tree', { count: 1 }) // "tree"
, t('tree', { count: 10 }) // "trees"
i17n is intended to inform that this is not a full-fledged i18n library. There is only support for a single set of translations or language at a time. If your project is smaller or a startup with no need for multiple languages yet, but may need them in the future, the i18n familiar API of i17n is exactly what you need.
Using internationalization is not just for languages. It helps create a consistent UX for your application and separates the concerns of text and UIs. Imagine your application sometimes says "Add Item" and other times says "Create Item" and even worse, says "New Item" in other places. This can be confusing to some users and leads to an inconsistent user experience. i17n allows you to have a single definition { "addItem": "Add {item}" }
that you can reference all over your application, t('addItem', { item: 'model.name' }).
npm i @drewjbartlett/i17n --save
{
"helloWorld": "Hello World!",
"welcomeUser": "Welcome, {user}!",
"global": {
"edit": "Edit",
"save": "Save",
"addNew": "Add New {item}",
},
"item": {
"name__one": "Item",
"name__many": "Items",
},
}
// or translations.ts
export const lang = {
helloWorld: 'Hello World!,
welcomeUser: 'Welcome, {user}!',
user: {
name: 'User',
},
global: {
edit: 'Edit',
save: 'Save',
addNew: 'Add New {item}',
},
item: {
name__one: 'Item',
name__many: 'Items',
},
};
// i17n.ts
import { createI7n } from '@drewjbartlett/i17n'
import Translations from 'path/to/translations';
export const i17n = createI17n({
translations: Translations,
});
// another-file.ts
import { i17n } from 'path/to/i17n'
i17n.t('helloWorld'); // "Hello World"
Optionally you can export your i17n's API to fit your convention and needs.
const $t = i18n.t;
export { $t }
// another-file.ts
import { $t } from 'path/to/i17n'
$t('helloWorld')
Value | Description |
---|---|
translations | The core { key: value } translations. |
loggingEnabled | When enabled warning logs will write to the console for missing keys. |
cache | Optionally pass a prebuilt cache of the resolved { key: value } pairs. |
{
"topLevel": "Top Level",
"namespace": {
"anotherNamespace": {
"tooDeep": "Deeply Namespaced Value"
}
},
"withAnInterpolation": "Hey, {name}!",
"withCounts": {
"mouse__one": "Mouse",
"mouse__many": "Mice",
}
}
When calling t()
, the each level of the keys are denoted by a .
. For example:
i18n.t('topLevel') // "Top Level"
i18n.t('namespace.anotherNamespace.tooDeep') // "Deeply Namespaced Value"
Tip: Don't nest too deeply or the first time a key is resolved it will have to iterate n
times to resolve it. After the first time it's cached.
When interpolating values, you simply pass put a value in your translation wrapped in {}
.
i18n.t('withAnInterpolation', { name: 'Drew' }) // "Hey, Drew!"
i18n.t('withAnInterpolation', { name: 'Robin' }) // "Hey, Robin!"
Keeping with the example above, there are times when a translation should resolve based on a given count. To do this simply provide a key of the same name and postfix it with __one
or __many
.
i18n.t('withCounts.mouse', { count: 1 }) // "Mouse"
i18n.t('withCounts.mouse', { count: 10 }) // "Mice"
There may be times where a core i17n instance is shipped. Imagine your global translations are included.
export const i17n = createI17n({
translations: {
global: {
edit: "Edit",
save: "Save",
addNew: "Add New {item}",
},
},
});
However, other parts of your application may want to extend and include their own translations that don't ship as core translations. This is especially useful when lazy loading translations in Single Page Apps.
// my-app-1.ts
import { i17n } from 'path/to/i17n'
i17n.extend({
myApp1: {
someKey: '...',
anotherKey: '...',
}
});
// now both the original and extended translations may be used
i17n.t('myApp1.someKey')
i17n.t('global.edit')
i17n can be used in any application but to show how easy it is to use with a library like Vue, here are some examples.
// i17n.ts
import { createI7n } from '@drewjbartlett/i17n'
import Translations from 'path/to/translations';
const i17n = createI17n({
translations: Translations,
});
const I18nPlugin = {
install(app) {
app.config.globalProperties.$t = i18n.t;
},
};
const app = createApp(MainApp);
app.use(I18nPlugin);
<template>
<h1>{{ $t('welcomeUser', { user: myUser }) }}</h1>
</template>
import Vue from 'vue';
const I18nPlugin = {
install: (Vue) => {
Vue.prototype.$t = i18n.t;
},
};
Vue.use(I18nPlugin)
<template>
<h1>{{ $t('welcomeUser', { user: myUser }) }}</h1>
</template>
FAQs
[](https://github.com/drewjbartlett/i17n/actions/workflows/core.yml) [](https://www.npmjs
The npm package @drewjbartlett/i17n receives a total of 2 weekly downloads. As such, @drewjbartlett/i17n popularity was classified as not popular.
We found that @drewjbartlett/i17n 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.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.