Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
feat: ✨ added translateDocs function
Browse files Browse the repository at this point in the history
  • Loading branch information
404-Program-not-found committed Jun 14, 2023
1 parent e6a80c5 commit db4185e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Empty file removed src/i18n/en/contrib.ts
Empty file.
38 changes: 38 additions & 0 deletions src/i18n/translation-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import enDocs from "./en/docs";

// type NavDictionaryKeys = (typeof enDocs)[number]["key"];
// same as above, but includes the keys from the links array
type NavDictionaryKeys =
| (typeof enDocs)[number]["key"]
| NonNullable<(typeof enDocs)[number]["links"]>[number]["key"];

export type EntryDoc = EntryLink | EntryHeader;

type EntryLink = { link: string; type?: string; text: string; key: string };
type EntryHeader = {
header: true;
links: EntryLink[];
type: string;
text: string;
key: string;
};
/**
* @description Function to create translations based on the docs.ts file, falling back to the english version if the translation is not found.
* @param {object} translations - The translations object to use.
* @returns {object} The translated docs object.
*/
export function translateDocs(
translations: Partial<Record<NavDictionaryKeys, string>>
): EntryDoc[] {
return enDocs.map((doc) => {
const translatedDoc = structuredClone(doc) as EntryDoc;
if ("header" in translatedDoc) {
translatedDoc.links = translatedDoc.links.map((link) => ({
...link,
text: translations[link.key] || link.text,
}));
}
translatedDoc.text = translations[doc.key] || doc.text;
return translatedDoc;
}) as EntryDoc[];
}

0 comments on commit db4185e

Please sign in to comment.