@markuplint/i18n
Install
Prerequisites: Node.js (Version 12.4.0 or later)
$ npm install @markuplint/i18n
$ yarn add @markuplint/i18n
API
import { translator } from '@markuplint/i18n';
const t = translator({
locale: 'ja',
...require('@markuplint/i18n/locales/ja.json'),
});
The translator
function creates the t
function.
It is an overloading function that accepts kind of arguments below:
Translate sentence
type T = (template?: string, ...values: string[]) => string;
const message = t(
'{0} is {1:c}',
t(
'{0} of {1}',
t(
'the {0}',
'value',
),
t(
'the "{0*}" {1}',
'id',
'attribute',
),
),
'duplicated',
);
console.log(message);
Placeholder
There is a placeholder that the number is surrounded by {}
on template strings. It is replaced argument as a phrase. It translates the phrase if it matches the keyword defined in the dictionary.
Tagged templates syntax
⚠️ It is experimental.
import { taggedTemplateTranslator } from '@markuplint/i18n';
const _ = taggedTemplateTranslator(ja);
const message = _`${
//
_`${
//
_`the ${'value'}`
} of ${
//
_`the "${'id'}" ${'attribute'}`
}`
} is ${
//
'c:duplicated'
}`;
console.log(message);
Translate a phrase
type T = (phrase: string) => string;
const phrase = t('element');
console.log(phrase);
Translate listed phrases
type T = (phrases: string[]) => string;
const list = t(['element', 'attribute', 'value']);
console.log(list);
console.log(list);
It converts the character-separated list specified in each locale.
Locale | Separater | Before Char | After Char |
---|
en | , (camma + space) | " (double quote) | " (double quote) |
ja | none (empty string) | 「 (left corner bracket) | 」 (right corner bracket) |
Avoid translation
The autocomplete
is defined as オートコンプリート
in the JA dictionary.
However, It avoids translation if the number placeholder includes *
(asterisk).
It is an effective means if you want a code or a specific name.
const phrase = t('the "{0}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
const phrase = t('the "{0*}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
Another means is that it surrounds %
(percentage) to a phrase. It is effective when you use listing.
const phrase = t('the "{0}" {1}', '%autocomplete%', 'attribute');
console.log(phrase);
const list = t(['element', '%attribute%', 'value']);
console.log(list);
Contributing
$ git clone git@github.com:markuplint/markuplint.git -b main
$ yarn
$ yarn build
$ yarn test
Copyright © 2021 markuplint. Under the MIT License.