120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
import { type Lang } from "../content/config";
|
|
|
|
export const DEFAULT_LANG = "eng" satisfies Lang;
|
|
|
|
export type TranslationRecord = { [DEFAULT_LANG]: string | ((...args: any[]) => string) } & {
|
|
[L in Exclude<Lang, typeof DEFAULT_LANG>]?: string | ((...args: any[]) => string);
|
|
};
|
|
|
|
export const UI_STRINGS: Record<string, TranslationRecord> = {
|
|
"story/return_to_stories": {
|
|
eng: "Return to stories",
|
|
tok: "o tawa e lipu ale",
|
|
},
|
|
"story/return_to_series": {
|
|
eng: (seriesName: string) => `Return to ${seriesName}`,
|
|
},
|
|
"story/go_to_description": {
|
|
eng: "Go to description",
|
|
tok: "o tawa e toki lipu",
|
|
},
|
|
"story/toggle_dark_mode": {
|
|
eng: "Toggle dark mode",
|
|
tok: "o ante e kule lipu",
|
|
},
|
|
"story/word_count": {
|
|
eng: (wordCount: string | number) => `Word count: ${wordCount}.`,
|
|
tok: "",
|
|
},
|
|
"story/publish_date": {
|
|
eng: (date: string) => date,
|
|
tok: (date: string) => `tenpo suno ${date}`,
|
|
},
|
|
"story/description": {
|
|
eng: "Description",
|
|
tok: "toki lipu",
|
|
},
|
|
"story/summary": {
|
|
eng: "Summary",
|
|
tok: "lipu tawa tenpo lili",
|
|
},
|
|
"story/reveal_summary": {
|
|
eng: "Click to reveal",
|
|
tok: "Click to reveal summary in English",
|
|
},
|
|
"story/to_top": {
|
|
eng: "To top",
|
|
tok: "tawa sewi",
|
|
},
|
|
"story/tags": {
|
|
eng: "Tags",
|
|
tok: "nimi kulupu",
|
|
},
|
|
"story/copyright_year": {
|
|
eng: (year: string | number) => `© ${year}`,
|
|
tok: (year: string | number) => `© tenpo pi sike suno ${year}`,
|
|
},
|
|
"story/licenses": {
|
|
eng: "Licenses",
|
|
tok: "lipu lawa",
|
|
},
|
|
"story/authors": {
|
|
eng: (authorsList: string[]) => {
|
|
let authorsString = `by ${authorsList[0]}`;
|
|
if (authorsList.length > 2) {
|
|
authorsString += `, ${authorsList.slice(1, authorsList.length - 1).join(", ")}, and ${authorsList[authorsList.length - 1]}`;
|
|
} else if (authorsList.length == 2) {
|
|
authorsString += ` and ${authorsList[1]}`;
|
|
}
|
|
return authorsString;
|
|
},
|
|
tok: (authorsList: string[]) => {
|
|
let authorsString = "lipu ni li tan ";
|
|
if (authorsList.length > 1) {
|
|
authorsString += `jan ni: ${authorsList.join(" en ")}`;
|
|
} else {
|
|
authorsString += authorsList[0];
|
|
}
|
|
return authorsString;
|
|
},
|
|
},
|
|
"story/commissioned_by": {
|
|
eng: (arg: string) => `Commissioned by ${arg}`,
|
|
},
|
|
"story/requested_by": {
|
|
eng: (arg: string) => `Requested by ${arg}`,
|
|
},
|
|
"characters/characters_are_copyrighted_by": {
|
|
eng: (owner: string, charactersList: string[]) => {
|
|
if (charactersList.length == 1) {
|
|
return `${charactersList[0]} is © ${owner}`;
|
|
}
|
|
if (charactersList.length == 2) {
|
|
return `${charactersList[0]} and ${charactersList[1]} are © ${owner}`;
|
|
}
|
|
return `${charactersList.slice(0, -1).join(", ")}, and ${charactersList[charactersList.length - 1]} are © ${owner}`;
|
|
},
|
|
},
|
|
"characters/all_characters_are_copyrighted_by": {
|
|
eng: (owner: string) => `All characters are © ${owner}`,
|
|
},
|
|
};
|
|
|
|
export function t(lang: Lang, stringOrSource: string | TranslationRecord, ...args: any[]): string {
|
|
if (typeof stringOrSource === "object") {
|
|
const translation = stringOrSource[lang] || stringOrSource[DEFAULT_LANG];
|
|
if (typeof translation === "function") {
|
|
return translation(...args);
|
|
}
|
|
return translation;
|
|
}
|
|
if (UI_STRINGS[stringOrSource]) {
|
|
const translation = UI_STRINGS[stringOrSource][lang] || UI_STRINGS[stringOrSource][DEFAULT_LANG];
|
|
if (typeof translation === "function") {
|
|
return translation(...args);
|
|
}
|
|
return translation;
|
|
}
|
|
console.warn(`No translation map found for "${stringOrSource}"`);
|
|
return stringOrSource;
|
|
}
|