
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
@translata/core
Advanced tools
Provides the core functionality for Translata. This includes the actual factory function createTranslator
as basic middlewares for locales, translations and placeholder patterns.
yarn add @translata/core
Create a translator function from middlewares. The order of middlewares matters. As a rule of thumb you should order them:
import { createTranslator } from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'user.greeting': 'Welcome to Translata!',
}),
);
_('user.greeting', { locale: 'en' }); // Welcome to Translata!
Injects translation strings. It takes the target locale and a map of translation strings, where the key is the translation id.
It is recommended to use "scoped" translation ids. That means, you should prefix translation ids with a namespace, to prevent conflicts of different translation sources.
The first middleware that provides a certain translation id will win and return it.
import { createTranslator, withTranslations } from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'user.greeting': 'Welcome to Translata!',
}),
withTranslations('de', {
'user.greeting': 'Willkommen bei Translata!',
}),
);
_('user.greeting', { locale: 'en' }); // Welcome to Translata!
_('user.greeting', { locale: 'de' }); // Willkommen bei Translata!
_('user.greeting', { locale: 'fr' }); // undefined
Sets a default locale when no other is given.
import { createTranslator, withDefaultLocale } from '@translata/core';
const _ = createTranslator(withDefaultLocale('en'));
_('translation_id'); // { locale: 'en' } will be present in options.
_('translation_id', { locale: 'fr' }); // { locale: 'fr' } will be present in options.
Overrides the current locale present when the translation resulted in undefined
.
import { createTranslator, withDefaultLocale } from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'user.greeting': 'Welcome to Translata!',
}),
withFallbackLocale('en'),
);
_('user.greeting', { locale: 'fr' }); // { locale: 'en' } will be used and result in "Welcome to Translata!"
_('user.greeting', { locale: 'fr', fallback: 'en' }); // Fallback can also be set in options and has priority.
When a translation is missing, the given translate callback is used to determine a fallback translation string.
import { createTranslator, withFallbackTranslation } from '@translata/core';
const _ = createTranslator(
withFallbackTranslation(
(id, options) =>
`PLEASE IMPLEMENT ME ON LOCALE "${options.locale}": ${id} `,
),
);
_('global.greeting', { locale: 'en' }); // PLEASE IMPLEMENT ME ON LOCALE "en": global.greeting
Gives translation strings the ability to contain placeholders. This middleware should always come after middlewares that find translation strings or manipulate locales.
import {
createTranslator,
withTranslations,
withDefaultLocale,
withPlaceholders,
} from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'user.greeting': 'Welcome to Translata, {{name}}!',
}),
withDefaultLocale('en')
withPlaceholders(),
);
_('user.greeting', { values: { name: 'John' } }); // Welcome to Translata, John!
Values can also be passed to the middleware function which then provide default values. Values passed to the translator function have priority:
import {
createTranslator,
withTranslations,
withDefaultLocale,
withPlaceholders,
} from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'debug.env': 'Current env is {{env}}',
}),
withDefaultLocale('en')
withPlaceholders({
env: 'development'
}),
);
_('debug.env'); // Current env is development
_('debug.env', { values: { env: 'production' } }); // Current env is production
Values can also be functions which will be invoked when translation occours:
import {
createTranslator,
withTranslations,
withDefaultLocale,
withPlaceholders,
} from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'date.now': 'Current date is {{date}}',
}),
withDefaultLocale('en')
withPlaceholders({
date: () => new Date().toDateString()
}),
);
_('date.now'); // Current date is Sat Mar 30 2019
You can even pass a context as inline option, which will then passed as argument to value callbacks:
import {
createTranslator,
withTranslations,
withDefaultLocale,
withPlaceholders,
} from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'goto.user': 'Watch the profile here: {{link}}',
}),
withDefaultLocale('en')
withPlaceholders({
link: (id: number) => `http://www.my-community.com/users/${id}`
}),
);
_('goto.user', { context: 124 }); // Watch the profile here: http://www.my-community.com/users/124
Will log missing translations with console.warn
.
import { createTranslator, withLogger } from '@translata/core';
const _ = createTranslator(withLogger());
_('global.greeting', { locale: 'en' }); // Will log: Missing translation for "global.greeting" on locale "en"
If a logger function is passed, it is called instead.
import { createTranslator, withLogger } from '@translata/core';
const _ = createTranslator(
withLogger((id, options) =>
console.error(`No translation for ${id} (locale = ${options.locale})!`),
),
);
_('global.greeting', { locale: 'en' }); // Will log: No translation for global.greeting (locale = en)!
Allows pluralization of translations. The translations string has to be separated up to three times as shown below. The resulting translation is based on the count which gets passed in the translator options:
import { createTranslator, withPluralizer } from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'pluralized.cats': 'no cats || one cat || many cats',
}),
withPluralizer(),
);
_('pluralized.cats', { locale: 'en', count: 3 }); // many cats
Since the pluralizer will pass the count as count
placeholder to the values
options of withPlaceholers
, you can even use the actual count and/or other placeholders in your string:
import {
createTranslator,
withPluralizer,
withPlaceholders,
} from '@translata/core';
const _ = createTranslator(
withTranslations('en', {
'pluralized.messages': 'no messages || one message || {{count}} messages',
}),
withPlaceholders(),
withPluralizer(),
);
_('pluralized.messages', { locale: 'en', count: 3 }); // 3 messages
_('pluralized.messages', { locale: 'en', count: 0 }); // no messages
_('pluralized.messages', { locale: 'en', count: 1 }); // one message
FAQs
Core functionality for translata: The Composable Translation Utility
The npm package @translata/core receives a total of 6 weekly downloads. As such, @translata/core popularity was classified as not popular.
We found that @translata/core 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.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.