This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ added translateDocs function
- Loading branch information
1 parent
e6a80c5
commit db4185e
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |