You can manage contants more easily using ji-dictionary
.
typescript friendly.
npm install ji-dictionary
or
pnpm add ji-dictionary
or
yarn add ji-dictionary
Dictionary
receives an object follwing the interface.
{
[key: TDictKey] : {
[language: TLanguage] : [word: TWord]
}
}
import { Dictionary } from 'ji-dictionary';
const dictionary = new Dictionary({
apple: {
en: 'APPLE',
ko: '사과',
},
melon: {
en: 'MELON',
ko: '메론',
},
} as const);
In this case,
TLanguage
is infered as"en" | "ko"
.TWord
is infered as"APPLE" | "MELON" | "메론" | "사과"
.TDictKey
is infered as"apple" | "melon"
There is no limit to the number of languages.
It returns input data.
dictionary.data.apple.en // returns "APPLE"
getTranslator returns Translator
.
Translator
can translate word to another language's word.
It is based on Map
object.
dictionary.getTranslator('en-ko').translate('APPLE'); // returns "사과"
dictionary.getTranslator('en-ko').translate('사과'); // returns "APPLE"
Either using 'en-ko' or 'ko-en' gives the same result. You can use the anyone.
dictionary.getTranslator('ko-en').translate('APPLE'); // returns "사과"
dictionary.getTranslator('ko-en').translate('사과'); // returns "APPLE"