Created
January 26, 2024 11:40
-
-
Save Almasx/88c37546023280f8697114b5c7ca4156 to your computer and use it in GitHub Desktop.
Auto generate astro files for internalization based on translations
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
const fs = require("fs"); | |
const path = require("path"); | |
// Base directory for i18n files | |
const i18nDir = path.join(__dirname, "../i18n"); | |
// Target base directory for language-specific pages | |
const pagesBaseDir = path.join(__dirname, "../../pages"); | |
// Languages to generate pages for | |
const languages = ["en", "ru", "kz"]; | |
const defaultLang = "ru"; | |
// Read filenames from the i18n directory, ignoring 'base.ts' | |
const filenames = fs.readdirSync(i18nDir).filter((file) => file !== "base.ts"); | |
// Iterate over each language | |
languages.forEach((lang) => { | |
if (lang === defaultLang) return; | |
// Destination directory for the current language | |
const langDir = path.join(pagesBaseDir, lang); | |
// Ensure the language directory exists | |
if (!fs.existsSync(langDir)) { | |
fs.mkdirSync(langDir, { recursive: true }); | |
} | |
// Iterate over each filename and copy it to the destination with an .astro extension | |
filenames.forEach((filename) => { | |
const sourcePath = path.join( | |
pagesBaseDir, | |
filename.replace(/\.[^/.]+$/, "") + ".astro" | |
); | |
const destPath = path.join( | |
langDir, | |
filename.replace(/\.[^/.]+$/, "") + ".astro" | |
); | |
fs.copyFileSync(sourcePath, destPath); | |
console.log(`Created ${destPath}`); | |
}); | |
}); | |
console.log("Internationalization files generated."); |
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
{ | |
..., | |
"scripts": { | |
..., | |
"sync:i18n": "node ./src/lib/cli/generate.cjs", | |
"build": "astro check && node ./src/lib/cli/generate.cjs && astro build", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment