next-translations
Version:
Translations to NextJS app
211 lines (210 loc) • 8.9 kB
JavaScript
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
//@ts-ignore
import translationsConfigUser from "../../translations.config.js";
const translationsConfig = {
defaultLocale: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.defaultLocale) || "en",
errorPagePath: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.errorPagePath) || "/404",
componentNameToReplaced: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.componentNameToReplaced) || "TComponent",
redirectForLoggedUser: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.redirectForLoggedUser) || "/",
redirectForNotLoggedUser: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.redirectForNotLoggedUser) || "/",
sitesForLoggedUser: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.sitesForLoggedUser) || [],
defaultLocaleWithMultirouting: translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.defaultLocaleWithMultirouting,
};
let pageTranslations = null;
const resolvePath = (object, path, defaultValue = undefined) => path.split(".").reduce((o, p) => { var _a; return (_a = o === null || o === void 0 ? void 0 : o[p]) !== null && _a !== void 0 ? _a : defaultValue; }, object);
const initializeTranslations = (translations) => {
pageTranslations = translations;
};
const checkTypesAndReturn = (type, value) => {
if (type === "string") {
if (typeof value === "string") {
return value;
}
else {
return undefined;
}
}
else if (type === "number") {
if (typeof value === "number") {
return value;
}
else {
return undefined;
}
}
else if (type === "array") {
if (Array.isArray(value)) {
return value;
}
else {
return undefined;
}
}
else if (type === "object") {
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
return value;
}
else {
return undefined;
}
}
else {
return value;
}
};
const generateTranslationWithType = (slug, namespace, type, disabledErrors) => {
const replacePathFromNamespace = namespace.replace(":", ".");
if (!pageTranslations) {
if (!disabledErrors) {
console.log(`next-translations - No detected translations for this page ${namespace}: ${slug}`);
}
return undefined;
}
const translationsNamespace = resolvePath(pageTranslations, replacePathFromNamespace, undefined);
const replaceSlugPathFromNamespace = slug.replace(":", ".");
const pathTranslated = resolvePath(translationsNamespace, replaceSlugPathFromNamespace, undefined);
if (pathTranslated === undefined) {
if (!disabledErrors) {
console.log(`next-translations - Fail translation ${namespace}: ${slug}`);
}
return undefined;
}
if (type) {
const validPathTranslted = checkTypesAndReturn(type, pathTranslated);
if (validPathTranslted !== undefined) {
return validPathTranslted;
}
else {
if (!disabledErrors) {
console.log(`next-translations - Fail type in translation ${namespace}: ${slug}`);
}
return undefined;
}
}
else {
return pathTranslated;
}
};
const useTranslation = (namespace = "", disabledErrors = false) => {
const replacePathFromNamespace = namespace.replace(":", ".");
const translationsNamespace = resolvePath(pageTranslations, replacePathFromNamespace, undefined);
if (!translationsNamespace) {
if (!disabledErrors) {
console.log(`next-translations - fail load namespace: ${namespace}. Probably that the given namespace is missing from the folder in your namespaces or it is spelled incorrectly. Another reason may be the translations have been cached and need to refresh the page!`);
}
return {
pageTranslations,
t: () => {
return undefined;
},
tString: () => {
return undefined;
},
tNumber: () => {
return undefined;
},
tArray: () => {
return undefined;
},
tObject: () => {
return undefined;
},
tComponent: (_slug, callback) => {
return callback({
textBefore: undefined,
textComponent: undefined,
textAfter: undefined,
});
},
};
}
const t = (slug = "") => {
return generateTranslationWithType(slug, namespace, "any", disabledErrors);
};
const tString = (slug = "") => {
return generateTranslationWithType(slug, namespace, "string", disabledErrors);
};
const tNumber = (slug = "") => {
return generateTranslationWithType(slug, namespace, "number", disabledErrors);
};
const tArray = (slug = "") => {
return generateTranslationWithType(slug, namespace, "array", disabledErrors);
};
const tObject = (slug = "") => {
return generateTranslationWithType(slug, namespace, "object", disabledErrors);
};
const tComponent = (slug = "", callback) => {
const generatedText = generateTranslationWithType(slug, namespace, "string", disabledErrors);
if (!generatedText) {
return undefined;
}
let componentStartIndex = -1;
let componentEndIndex = -1;
let componentOnlyIndex = -1;
let text = "";
let textBefore = "";
let textAfter = "";
let textComponent = undefined;
const generatedTextArray = generatedText.split(" ");
generatedTextArray === null || generatedTextArray === void 0 ? void 0 : generatedTextArray.forEach((itemText, indexText) => {
const isStartComponent = itemText === "<" + translationsConfig.componentNameToReplaced + ">";
if (isStartComponent) {
componentStartIndex = indexText;
return;
}
const isEndComponent = itemText === "</" + translationsConfig.componentNameToReplaced + ">";
if (isEndComponent) {
componentEndIndex = indexText;
return;
}
const isOnlyComponent = itemText === "<" + translationsConfig.componentNameToReplaced + "/>";
if (isOnlyComponent) {
componentOnlyIndex = indexText;
return;
}
});
generatedTextArray === null || generatedTextArray === void 0 ? void 0 : generatedTextArray.forEach((itemText, indexText) => {
if (componentOnlyIndex === -1 &&
componentStartIndex >= 0 &&
componentEndIndex >= 0) {
if (indexText < componentStartIndex) {
textBefore = `${textBefore ? textBefore + " " : ""}${itemText}`;
}
else if (indexText > componentStartIndex &&
indexText < componentEndIndex) {
textComponent = `${textComponent ? textComponent + " " : ""}${itemText}`;
}
else if (indexText > componentEndIndex) {
textAfter = `${textAfter ? textAfter + " " : ""}${itemText}`;
}
}
else if (componentOnlyIndex >= 0) {
if (indexText < componentOnlyIndex) {
textBefore = `${textBefore ? textBefore + " " : ""}${itemText}`;
}
else if (indexText > componentOnlyIndex) {
textAfter = `${textAfter ? textAfter + " " : ""}${itemText}`;
}
}
else {
text = `${text ? text + " " : ""}${itemText}`;
}
});
return callback({
textBefore,
textComponent,
textAfter,
});
};
return {
t,
tString,
tNumber,
tArray,
tObject,
tComponent,
pageTranslations,
};
};
export { initializeTranslations, pageTranslations, useTranslation };