i18n-support-server-module

i18n support server and module

i18n Related Support Modules and Server

API Docs

  • translate multiple sentence
  • get language name by language code
  • get sitemap by multiple language code

Table of Contents

Installation

npm i https://github.com/secondphantom/i18n-support-server-module

Module

type: class

Example

(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: '버스' }
}
*/
})();

Translator

Class

const controller = new Translator(options?)
await controller.init()

Constructor Parameters

interface TranslatorOptions {
translateRepo?: {
type: TranslateRepoType;
options?: TranslateRepoOptions;
launchOptions?: LaunchOptions;
};
}
type TranslateRepoType = "google_browser";

interface TranslateRepoOptions {
concurrency: number;
lockDelayMs?: number;
}

Translate

When a sentence is input, it responds with the translated content in multiple languages

Methods

translateSentence
inputs

Sentence

interface Sentence {
sentence: string;
from: string;
to: string;
}
return

TranslateReturn

interface TranslateReturn {
locale: string;
sentence: string;
}
translateMultiSentence
inputs

Array of SentenceWithKey

interface SentenceWithKey {
sentence: string;
from: string;
to: string;
key: string;
}
return

Array of TranslateReturnWithKey

interface TranslateReturnWithKey {
locale: string;
sentence: string;
key:string;
}
translateMultiLanguage
inputs

TranslateMultiLanguageDto

interface TranslateMultiLanguageDto {
sentenceAry: { sentence: string; key: string }[];
from: string;
to: string[];
}
return

Array of Array of TranslateReturnWithKey

interface TranslateReturnWithKey {
locale: string;
sentence: string;
key:string;
}
translateJsonValue
inputs

TranslateJsonValueDto

interface TranslateJsonValueDto {
json: { [key in string]: string };
from: string;
to: string[];
}
return

Return

type Return = {
[key: string]: {
[key: string]: string;
};
}

LanguageCode

Class

const controller = new LanguageCode();

Language Code

If a language code is given, it responds with the language or sitemap.xml according to the code.

Methods

getName
inputs

LanguageCodeWithOptions

interface Options {
short: boolean;
}
interface LanguageCodeWithOptions {
code: string;
options?: Options;
}
return

LanguageCodeWithName

interface LanguageCodeWithName {
code: string;
name: string;
}
getMultiName
inputs

MultiLanguageCodeWithOptions

interface Options {
short: boolean;
}
interface MultiLanguageCodeWithOptions {
codeList: string[];
options?: Options;
}
return

Array of LanguageCodeWithName

interface LanguageCodeWithName {
code: string;
name: string;
}
getMultiCodeToKeyNameValue
inputs

MultiLanguageCodeWithOptions

return

Return

type Return {
[x: string]: string;
}
getSiteMap
inputs

LanguageCodeSiteMapInputs

interface LanguageCodeSiteMapInputs {
rootUrl: string;
pages: string[];
defaultLocale: string;
supportedLocales: string[];
options?: LanguageCodeSiteMapOptions;
}
interface LanguageCodeSiteMapOptions {
trailingSlash?: boolean;
lastMod?: string;
}
return

LanguageCodeSiteMapReturn

interface LanguageCodeSiteMapReturn {
siteMap: string;
}

Server

type: class

Example

(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();
})();

Factory

i18nSupportServerFactory: (type: ServerType, options?: ServerOptions) => Promise<ExpressServer>

Request

Translate

If a sentence or JSON is given, it responds with the translation into multiple languages

URLs

/translate/sentence
/translate/sentence/multi
/translate/sentence/multi/language
/translate/sentence/multi/language/json

Language Code

If a language code is given, it responds with the language or sitemap.xml according to the code.

URLs

/language-code/name
/language-code/name/multi
/language-code/name/multi?type=keynamevalue
/language-code/sitemap

Generated using TypeDoc