Skip to content

Commit

Permalink
add: translationsDirectory to config
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRichi3 committed May 13, 2024
1 parent c6dd6ed commit 2408b35
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const CONFIG = {
outputDirectory: config.outputDirectory,
sourceDirectory: config.sourceDirectory,
tutorialDirectory: config.tutorialDirectory,
languages: config.languages,
translationsDirectory: config.translationsDirectory,
includeLocalizedVersions: config.includeLocalizedVersions,
languages: config.languages,
};

export default CONFIG;

142 changes: 78 additions & 64 deletions src/fixes/fix_crowdin_translation.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
/**
* This should only be run after the translations have been downloaded from Crowdin
*
*
* This script will move the translated files from the Crowdin structure to the output directory
* Fix the structure of the translated files in the output directory
* Merge the paths for the translated files
* And fix the file extensions for the translated files
*/

import CONFIG from "../config";
import path from 'path'
import fs from 'fs'
import path from "path";
import fs from "fs";

/**
* Move the updated crowdin translations from translations folder to the output directory
*/
export async function moveTranslatedFilesToOutputDir() {
const translationsFolderPath = path.join(__dirname, '/../../translation')
const translationsFolderPath = path.join(__dirname, "/../../" + CONFIG.translationsDirectory);

async function copyAllFoldersAndFiles(source: string, destination: string) {
const files = await fs.promises.readdir(source)
console.log({ source, destination })
const files = await fs.promises.readdir(source);

console.log({ source, destination });

for (const file of files) {
const filePath = path.join(source, file)
const newFilePath = path.join(destination, file)
const stats = await fs.promises.stat(filePath)
const filePath = path.join(source, file);
const newFilePath = path.join(destination, file);

const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
console.log({ newFilePath })
await fs.promises.mkdir(newFilePath, { recursive: true })
await copyAllFoldersAndFiles(filePath, newFilePath)
console.log({ newFilePath });
await fs.promises.mkdir(newFilePath, { recursive: true });
await copyAllFoldersAndFiles(filePath, newFilePath);
} else {
await fs.promises.copyFile(filePath, newFilePath)
await fs.promises.copyFile(filePath, newFilePath);
}
}
}

// Find all folders that are named after the language code
for (const language of CONFIG.languages) {
const langFolderPath = path.join(translationsFolderPath, language)
const langFolderPath = path.join(translationsFolderPath, language);

// Copy the language folder to the output directory
const newLangFolderPath = path.join(__dirname, `../../${CONFIG.outputDirectory}`, language)
await copyAllFoldersAndFiles(langFolderPath, newLangFolderPath)
const newLangFolderPath = path.join(
__dirname,
`../../${CONFIG.outputDirectory}`,
language,
);
await copyAllFoldersAndFiles(langFolderPath, newLangFolderPath);
}
}

Expand All @@ -52,120 +56,129 @@ export async function moveTranslatedFilesToOutputDir() {
*/
export async function fixTranslatedFilesStructureInOutputDir() {
// Check the output dir for the localized files
const folderPath = path.join(CONFIG.outputDirectory)
const folderPath = path.join(CONFIG.outputDirectory);

for (const language of CONFIG.languages) {
const langFolderPath = path.join(folderPath, language + `/${CONFIG.outputDirectory}`)
const langFolderPath = path.join(
folderPath,
language + `/${CONFIG.outputDirectory}`,
);

// Move the folders and files from the language/<ouput folder> to the language folder
const files = await fs.promises.readdir(langFolderPath)
const files = await fs.promises.readdir(langFolderPath);

for (const file of files) {
const filePath = path.join(langFolderPath, file)
const newFilePath = path.join(path.join(folderPath, language), file)
const filePath = path.join(langFolderPath, file);
const newFilePath = path.join(
path.join(folderPath, language),
file,
);

await fs.promises.rename(filePath, newFilePath)
await fs.promises.rename(filePath, newFilePath);
}
}
}

/**
* Merge the paths for the translated files
*
* @description This is necessary because the Crowdin CLI creates a folder for each language
*
* @description This is necessary because the Crowdin CLI creates a folder for each language
* and places the translated files within that folder. This function will move the files from the language folder
* to the same level as the original files.
*/
export async function mergePathsForTranslatedFiles() {
// Go to the folders for the languages within the output dir
const folderPath = path.join(CONFIG.outputDirectory)
const folderPath = path.join(CONFIG.outputDirectory);

// Go through each language folder
for (const language of CONFIG.languages) {
const languageFolderPath = path.join(folderPath, language)
const languageFolderPath = path.join(folderPath, language);

async function recursivelyMoveSubFiles(folderPath: string) {
const files = await fs.promises.readdir(folderPath)
const files = await fs.promises.readdir(folderPath);

for (const file of files) {
const filePath = path.join(folderPath, file)
const filePath = path.join(folderPath, file);
// Remove the prefix /language/outputDir from the file path
// Example move docs/ar/chapter/index.ar.qmd to docs/chapter/index.ar.qmd
const index = filePath.indexOf(`/${language}`)
let newFilePathArr = filePath.split('')
const index = filePath.indexOf(`/${language}`);
let newFilePathArr = filePath.split("");
for (let i = 0; i < +language.length + 2; i++) {
newFilePathArr[i + index] = '/'
newFilePathArr[i + index] = "/";
}
const newFilePath = path.resolve(newFilePathArr.join(''))
const newFilePath = path.resolve(newFilePathArr.join(""));

const stats = await fs.promises.stat(filePath)
const stats = await fs.promises.stat(filePath);

if (stats.isDirectory()) {
await recursivelyMoveSubFiles(filePath)
await recursivelyMoveSubFiles(filePath);
} else {
console.log(`Moving ${filePath} to ${newFilePath}`)
fs.renameSync(filePath, newFilePath)
console.log(`Moving ${filePath} to ${newFilePath}`);
fs.renameSync(filePath, newFilePath);
}
}
}

await recursivelyMoveSubFiles(languageFolderPath)
await recursivelyMoveSubFiles(languageFolderPath);
}
}

/**
* Fix the file extensions for the translated files
*
*
* @description The generated crowdin files have this file extension pattern: <filename>.<language>.<ext>
* The problem here is that the <filename> can be <index.md> instead of 'index'.
* This will make the generated file to be <index.md>.<language>.<ext>
* This function will fix the file name to be <plain_file_name>.<language>.<ext> so it'll be <index.<language>.<ext>>
*/
export async function fixFileExtensionsForTranslatedFiles() {
// Go to the folders for the languages within the output dir
const folderPath = path.join(CONFIG.outputDirectory)
const folderPath = path.join(CONFIG.outputDirectory);

// Go through each language folder
for (const language of CONFIG.languages) {
const languageFolderPath = path.join(folderPath, language)
const languageFolderPath = path.join(folderPath, language);

async function recursivelyChangeFileExtensions(folderPath: string) {
const files = await fs.promises.readdir(folderPath)
const files = await fs.promises.readdir(folderPath);

for (const file of files) {
const filePath = path.join(folderPath, file)
const stats = await fs.promises.stat(filePath)
const filePath = path.join(folderPath, file);
const stats = await fs.promises.stat(filePath);

if (stats.isDirectory()) {
await recursivelyChangeFileExtensions(filePath)
await recursivelyChangeFileExtensions(filePath);
} else {
const fileExtension = path.extname(filePath)
const newFilePath = filePath.replace(`${fileExtension}.${language}`, `.${language}`)

console.log(`Renaming ${filePath} to ${newFilePath}`)
await fs.promises.rename(filePath, newFilePath)
const fileExtension = path.extname(filePath);
const newFilePath = filePath.replace(
`${fileExtension}.${language}`,
`.${language}`,
);

console.log(`Renaming ${filePath} to ${newFilePath}`);
await fs.promises.rename(filePath, newFilePath);
}
}
}

await recursivelyChangeFileExtensions(languageFolderPath)
await recursivelyChangeFileExtensions(languageFolderPath);
}
}

export async function deleteEmptyFoldersInOutDir() {
// Delete all the empty folders for each language in the output directory
for (const language of CONFIG.languages) {
const languageFolderPath = path.join(CONFIG.outputDirectory, language)
fs.rmdirSync(languageFolderPath, { recursive: true })
const languageFolderPath = path.join(CONFIG.outputDirectory, language);
fs.rmdirSync(languageFolderPath, { recursive: true });
}
}

async function start() {
await moveTranslatedFilesToOutputDir()
await fixTranslatedFilesStructureInOutputDir()
await fixFileExtensionsForTranslatedFiles()
await mergePathsForTranslatedFiles()
await deleteEmptyFoldersInOutDir()
await moveTranslatedFilesToOutputDir();
await fixTranslatedFilesStructureInOutputDir();
await fixFileExtensionsForTranslatedFiles();
await mergePathsForTranslatedFiles();
await deleteEmptyFoldersInOutDir();
}

if (require.main === module) {
Expand All @@ -181,7 +194,8 @@ if (require.main === module) {
process.exit(1);
}

languages.shift()
CONFIG.languages = languages
start()
}
languages.shift();
CONFIG.languages = languages;
start();
}

0 comments on commit 2408b35

Please sign in to comment.