![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
laravel-like-translations
Advanced tools
Laravel-inspired translation/internationalization library
Javascript internalisation helper with nested translations and dot notation access.
Heavily inspired by Laravel's internationalisation system
npm: npm i laravel-like-translations
yarn: yarn add laravel-like-translations
export interface TranslationShape {
key1: string,
keys: {
a: string,
b: string,
},
}
import { TranslationShape } from "./types";
const En: TranslationShape = {
key1: "Key 1",
keys: {
a: "A",
b: "B",
},
};
export default En;
import makeTranslator from "laravel-like-translations";
import { TranslationShape } from "./types";
import En from "./lang/en";
const translator = makeTranslator<TranslationShape>({
translations: {
en: En,
},
// Optional fallback locale
fallbackLocale: 'en',
});
export default translator;
import translator from "./helpers/translator";
// ...
const translated = translator("key1", "en");
Third argument allows you to engage pluralisation and placeholder replacement.
Number is used in :count
placeholder:
"I have one apple|I have :count apples"
translate(..., ..., 4) // "I have 4 apples"
translate(..., ..., 1) // "I have one apple"
String is used in :default
placeholder:
"Your username is :default"
translate(..., ..., "cool_nickname") // "Your username is cool_nickname"
You can also pass an object, where keys correspond to names of placeholders:
translate(..., ..., {
name: "john"
});
"My name is :name" => "My name is john"
"My name is :Name" => "My name is John"
"My name is :NAME" => "My name is JOHN"
import makeTranslator from "laravel-like-translations";
import { Paths, TranslationReplacements } from "laravel-like-translations/lib/types";
import translator from "./helpers/translator";
import { TranslationShape } from "./types";
function useTranslate() {
// Implement your locale extractor, for example:
const locale = useSelector(state => state.app.locale);
// Create HOC for your translation helper
function translate(key: Paths<TranslationShape>, replacements?: TranslationReplacements) {
// HOC inserts extracted locale for you
return translator(key, locale, replacements);
}
// Memoize your HOC if you need to
return React.useCallback(translate);
}
export default useTranslate;
import useTranslate from "./hooks/useTranslate";
function YourComponent() {
const __ = useTranslate();
return (
<p>
{__("key1")}
</p>
);
}
FAQs
Laravel-inspired translation/internationalization library
The npm package laravel-like-translations receives a total of 1 weekly downloads. As such, laravel-like-translations popularity was classified as not popular.
We found that laravel-like-translations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.