Skip to content

added nested directory support for i18n files #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.idea/

# Runtime data
pids
*.pid
Expand Down Expand Up @@ -66,4 +68,4 @@ typings/

# dotenv environment variables file
.env
.env.test
.env.test
72 changes: 44 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
evaluateFilePath,
FileType,
DirectoryStructure,
TranslatableFile,
TranslatableFile, ensureDirectoryExists,
} from './util/file-system';
import { matcherMap } from './matchers';

Expand Down Expand Up @@ -102,6 +102,10 @@ commander
'-o, --overwrite',
'overwrite existing translations instead of skipping them',
)
.option(
'-r, --recursive',
'recursively load translations from subdirectories',
)
.parse(process.argv);

const translate = async (
Expand All @@ -122,6 +126,7 @@ const translate = async (
appName?: string,
context?: string,
overwrite: boolean = false,
recursive: boolean = false,
) => {
const workingDir = path.resolve(process.cwd(), inputDir);
const resolvedCacheDir = path.resolve(process.cwd(), cacheDir);
Expand Down Expand Up @@ -158,6 +163,7 @@ const translate = async (
exclude,
fileType,
withArrays,
recursive
);

if (templateFiles.length === 0) {
Expand All @@ -176,7 +182,7 @@ const translate = async (

console.log(`🏭 Loading source files...`);
for (const file of templateFiles) {
console.log(chalk`├── ${String(file.name)} (${file.type})`);
console.log(chalk`├── ${String(file.relativePath)} (${file.type})`);
}
console.log(chalk`└── {green.bold Done}`);
console.log();
Expand Down Expand Up @@ -208,9 +214,9 @@ const translate = async (
);

if (inconsistentKeys.length > 0) {
inconsistentFiles.push(file.name);
inconsistentFiles.push(file.relativePath);
console.log(
chalk`├── {yellow.bold ${file.name} contains} {red.bold ${String(
chalk`├── {yellow.bold ${file.relativePath} contains} {red.bold ${String(
inconsistentKeys.length,
)}} {yellow.bold inconsistent key(s)}`,
);
Expand All @@ -231,6 +237,10 @@ const translate = async (
fixSourceInconsistencies(
templateFilePath,
evaluateFilePath(resolvedCacheDir, dirStructure, sourceLang),
exclude,
fileType,
withArrays,
recursive,
);
console.log(chalk`└── {green.bold Fixed all inconsistencies.}`);
} else {
Expand All @@ -252,9 +262,9 @@ const translate = async (
);

if (invalidKeys.length > 0) {
invalidFiles.push(file.name);
invalidFiles.push(file.relativePath);
console.log(
chalk`├── {yellow.bold ${file.name} contains} {red.bold ${String(
chalk`├── {yellow.bold ${file.relativePath} contains} {red.bold ${String(
invalidKeys.length,
)}} {yellow.bold invalid key(s)}`,
);
Expand Down Expand Up @@ -318,29 +328,30 @@ const translate = async (
exclude,
fileType,
withArrays,
recursive
);

if (deleteUnusedStrings) {
const templateFileNames = templateFiles.map((t) => t.name);
const templateFileNames = templateFiles.map((t) => t.relativePath);
const deletableFiles = existingFiles.filter(
(f) => !templateFileNames.includes(f.name),
(f) => !templateFileNames.includes(f.relativePath),
);

for (const file of deletableFiles) {
console.log(
chalk`├── {red.bold ${file.name} is no longer used and will be deleted.}`,
chalk`├── {red.bold ${file.relativePath} is no longer used and will be deleted.}`,
);

fs.unlinkSync(
path.resolve(
evaluateFilePath(workingDir, dirStructure, language),
file.name,
file.relativePath,
),
);

const cacheFile = path.resolve(
evaluateFilePath(workingDir, dirStructure, language),
file.name,
file.relativePath,
);
if (fs.existsSync(cacheFile)) {
fs.unlinkSync(cacheFile);
Expand All @@ -349,12 +360,12 @@ const translate = async (
}

for (const templateFile of templateFiles) {
process.stdout.write(`├── Translating ${templateFile.name}`);
process.stdout.write(`├── Translating ${templateFile.relativePath}`);

const [addedTranslations, removedTranslations] =
await translateContent(
templateFile,
existingFiles.find((f) => f.name === templateFile.name),
existingFiles.find((f) => f.relativePath === templateFile.relativePath),
);

totalAddedTranslations += addedTranslations;
Expand All @@ -364,14 +375,14 @@ const translate = async (

case 'ngx-translate':
const sourceFile = templateFiles.find(
(f) => f.name === `${sourceLang}.json`,
(f) => f.relativePath === `${sourceLang}.json`,
);
if (!sourceFile) {
throw new Error('Could not find source file. This is a bug.');
}
const [addedTranslations, removedTranslations] = await translateContent(
sourceFile,
templateFiles.find((f) => f.name === `${language}.json`),
templateFiles.find((f) => f.relativePath === `${language}.json`),
);

totalAddedTranslations += addedTranslations;
Expand Down Expand Up @@ -441,6 +452,7 @@ translate(
commander.appName,
commander.context,
commander.overwrite,
commander.recursive
).catch((e: Error) => {
console.log();
console.log(chalk.bgRed('An error has occurred:'));
Expand All @@ -460,15 +472,15 @@ function createTranslator(
dirStructure: DirectoryStructure,
deleteUnusedStrings: boolean,
withArrays: boolean,
overwrite,
overwrite: boolean,
) {
return async (
sourceFile: TranslatableFile,
destinationFile: TranslatableFile | undefined,
) => {
const cachePath = path.resolve(
evaluateFilePath(cacheDir, dirStructure, sourceLang),
sourceFile ? sourceFile.name : '',
sourceFile ? sourceFile.relativePath : '',
);
let cacheDiff: string[] = [];
if (fs.existsSync(cachePath) && !fs.statSync(cachePath).isDirectory()) {
Expand Down Expand Up @@ -535,11 +547,14 @@ function createTranslator(
2,
) + `\n`;

const writePath = path.resolve(
evaluateFilePath(workingDir, dirStructure, targetLang),
destinationFile?.relativePath ?? sourceFile.relativePath,
)
ensureDirectoryExists(writePath);

fs.writeFileSync(
path.resolve(
evaluateFilePath(workingDir, dirStructure, targetLang),
destinationFile?.name ?? sourceFile.name,
),
writePath,
newContent,
);

Expand All @@ -548,14 +563,15 @@ function createTranslator(
dirStructure,
targetLang,
);
if (!fs.existsSync(languageCachePath)) {
fs.mkdirSync(languageCachePath);
}

const langCacheFilePath = path.resolve(
languageCachePath,
destinationFile?.relativePath ?? sourceFile.relativePath,
);
ensureDirectoryExists(langCacheFilePath)

fs.writeFileSync(
path.resolve(
languageCachePath,
destinationFile?.name ?? sourceFile.name,
),
langCacheFilePath,
JSON.stringify(translatedFile, null, 2) + '\n',
);
}
Expand Down
39 changes: 31 additions & 8 deletions src/util/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type FileType = 'key-based' | 'natural' | 'auto';
export type DirectoryStructure = 'default' | 'ngx-translate';

export interface TranslatableFile {
name: string;
relativePath: string;
originalContent: string;
type: FileType;
content: object;
Expand Down Expand Up @@ -44,16 +44,17 @@ export const detectFileType = (json: any): FileType => {

export const loadTranslations = (
directory: string,
exclude?: string,
exclude: string | undefined,
fileType: FileType = 'auto',
withArrays = false,
recursive = false
) =>
globSync(`${directory}/*.json`, { ignore: exclude }).map((f) => {
globSync(`${directory}${recursive ? '/**' : ''}/*.json`, { ignore: exclude }).map((f) => {
const json = require(path.resolve(directory, f));
const type = fileType === 'auto' ? detectFileType(json) : fileType;

return {
name: path.basename(f),
relativePath: recursive ? path.relative(directory, f) : path.basename(f),
originalContent: json,
type,
content:
Expand All @@ -65,25 +66,47 @@ export const loadTranslations = (
} as TranslatableFile;
});

export const ensureDirectoryExists = (filePath: string) => {
const dirname = path.dirname(filePath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive:true });
}
}

export const fixSourceInconsistencies = (
directory: string,
cacheDir: string,
exclude: string | undefined,
fileType: FileType = 'auto',
withArrays = false,
recursive = false
) => {
const files = loadTranslations(directory).filter((f) => f.type === 'natural');
const files = loadTranslations(
directory,
exclude,
fileType,
withArrays,
recursive
).filter((f) => f.type === 'natural');

for (const file of files) {
const fixedContent = Object.keys(file.content).reduce(
(acc, cur) => ({ ...acc, [cur]: cur }),
{} as { [k: string]: string },
);

const outPath = path.resolve(directory, file.relativePath);
const cachePath = path.resolve(cacheDir, file.relativePath);
ensureDirectoryExists(outPath)
ensureDirectoryExists(cachePath)

fs.writeFileSync(
path.resolve(directory, file.name),
JSON.stringify(fixedContent, null, 2) + '\n',
outPath,
JSON.stringify(fixedContent, null, 2) + '\n'
);

fs.writeFileSync(
path.resolve(cacheDir, file.name),
cachePath,
JSON.stringify(fixedContent, null, 2) + '\n',
);
}
Expand Down