i18n Related Support Modules and Server
npm i https://github.com/secondphantom/i18n-support-server-module
type: class
(async () => {
const translateController = new Translator();
await translateController.init();
const input = {
json: {
bicycle: "자전거",
car: "자동차",
bus: "버스",
},
from: "ko",
to: ["ja", "en"],
};
const result = await translateController.translateJsonValue(input);
console.log(result);
/*
{
ja: { bicycle: '自転車', car: '自動車', bus: 'バス' },
en: { bicycle: 'bicycle', car: 'automobile', bus: 'bus' },
ko: { bicycle: '자전거', car: '자동차', bus: '버스' }
}
*/
})();
const controller = new Translator(options?)
await controller.init()
interface TranslatorOptions {
translateRepo?: {
type: TranslateRepoType;
options?: TranslateRepoOptions;
launchOptions?: LaunchOptions;
};
}
type TranslateRepoType = "google_browser";
interface TranslateRepoOptions {
concurrency: number;
lockDelayMs?: number;
}
When a sentence is input, it responds with the translated content in multiple languages
translateSentence
Sentence
interface Sentence {
sentence: string;
from: string;
to: string;
}
TranslateReturn
interface TranslateReturn {
locale: string;
sentence: string;
}
translateMultiSentence
Array of SentenceWithKey
interface SentenceWithKey {
sentence: string;
from: string;
to: string;
key: string;
}
Array of TranslateReturnWithKey
interface TranslateReturnWithKey {
locale: string;
sentence: string;
key:string;
}
translateMultiLanguage
TranslateMultiLanguageDto
interface TranslateMultiLanguageDto {
sentenceAry: { sentence: string; key: string }[];
from: string;
to: string[];
}
Array of Array of TranslateReturnWithKey
interface TranslateReturnWithKey {
locale: string;
sentence: string;
key:string;
}
translateJsonValue
TranslateJsonValueDto
interface TranslateJsonValueDto {
json: { [key in string]: string };
from: string;
to: string[];
}
Return
type Return = {
[key: string]: {
[key: string]: string;
};
}
const controller = new LanguageCode();
If a language code is given, it responds with the language or sitemap.xml according to the code.
getName
LanguageCodeWithOptions
interface Options {
short: boolean;
}
interface LanguageCodeWithOptions {
code: string;
options?: Options;
}
LanguageCodeWithName
interface LanguageCodeWithName {
code: string;
name: string;
}
getMultiName
MultiLanguageCodeWithOptions
interface Options {
short: boolean;
}
interface MultiLanguageCodeWithOptions {
codeList: string[];
options?: Options;
}
Array of LanguageCodeWithName
interface LanguageCodeWithName {
code: string;
name: string;
}
getMultiCodeToKeyNameValue
Return
type Return {
[x: string]: string;
}
getSiteMap
LanguageCodeSiteMapInputs
interface LanguageCodeSiteMapInputs {
rootUrl: string;
pages: string[];
defaultLocale: string;
supportedLocales: string[];
options?: LanguageCodeSiteMapOptions;
}
interface LanguageCodeSiteMapOptions {
trailingSlash?: boolean;
lastMod?: string;
}
LanguageCodeSiteMapReturn
interface LanguageCodeSiteMapReturn {
siteMap: string;
}
type: class
(async () => {
const server = await i18nSupportServerFactory("express");
const url = "http://localhost:3000/translate/sentence/multi/language/json";
const body = {
json: {
bicycle: "자전거",
car: "자동차",
bus: "버스",
},
from: "ko",
to: ["ja", "en"],
};
const result = await fetch(url, {
body: JSON.stringify(body),
method: "POST",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
return response.json();
});
console.log(result);
/*
{
success: true,
data: {
ja: { bicycle: '自転車', car: '自動車', bus: 'バス' },
en: { bicycle: 'bicycle', car: 'automobile', bus: 'bus' },
ko: { bicycle: '자전거', car: '자동차', bus: '버스' }
}
}
*/
server.close();
})();
i18nSupportServerFactory: (type: ServerType, options?: ServerOptions) => Promise<ExpressServer>
If a sentence or JSON is given, it responds with the translation into multiple languages
/translate/sentence
POST
/translate/sentence/multi
POST
/translate/sentence/multi/language
POST
/translate/sentence/multi/language/json
POST
If a language code is given, it responds with the language or sitemap.xml according to the code.
/language-code/name
POST
/language-code/name/multi
POST
/language-code/name/multi?type=keynamevalue
POST
/language-code/sitemap
POST
Generated using TypeDoc