Skip to content

Commit

Permalink
feat: Add content filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed May 31, 2024
1 parent 9a5873c commit ce2d387
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
22 changes: 19 additions & 3 deletions src/commands/translate/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {mkdir} from 'node:fs/promises';
import {pick} from 'lodash';
import {gray} from 'chalk';
import {asyncify, eachLimit} from 'async';
import liquid from '@diplodoc/transform/lib/liquid';
import {BaseProgram} from '~/program/base';
import {Command, defined} from '~/config';
import {YFM_CONFIG_FILENAME} from '~/constants';
Expand All @@ -21,6 +22,7 @@ import {
resolveSchemas,
resolveSource,
resolveTargets,
resolveVars,
} from '../utils';

const MAX_CONCURRENCY = 50;
Expand All @@ -31,6 +33,7 @@ export type ExtractArgs = ProgramArgs & {
target?: string | string[];
include?: string[];
exclude?: string[];
vars?: Record<string, any>;
};

export type ExtractConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'> & {
Expand All @@ -40,6 +43,7 @@ export type ExtractConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'> &
include: string[];
exclude: string[];
files: string[];
vars: Record<string, any>;
};

class ExtractLogger extends Logger {
Expand Down Expand Up @@ -68,6 +72,7 @@ export class Extract
options.files,
options.include,
options.exclude,
options.vars,
options.config(YFM_CONFIG_FILENAME),
];

Expand Down Expand Up @@ -96,6 +101,7 @@ export class Extract
source.language,
['.md', '.yaml'],
);
const vars = resolveVars(config, args);

return Object.assign(config, {
input,
Expand All @@ -107,12 +113,13 @@ export class Extract
files,
include,
exclude,
vars,
});
});
}

async action() {
const {input, output, files, source, target: targets} = this.config;
const {input, output, files, source, target: targets, vars} = this.config;

this.logger.setup(this.config);

Expand All @@ -125,6 +132,7 @@ export class Extract
target,
input,
output,
vars,
});

await eachLimit(
Expand Down Expand Up @@ -157,10 +165,11 @@ export type PipelineParameters = {
output: string;
source: ExtractOptions['source'];
target: ExtractOptions['target'];
vars: Record<string, any>;
};

function pipeline(params: PipelineParameters) {
const {input, output, source, target} = params;
const {input, output, source, target, vars} = params;
const inputRoot = resolve(input);
const outputRoot = resolve(output);

Expand All @@ -180,7 +189,14 @@ function pipeline(params: PipelineParameters) {
return;
}

const content = await loadFile(inputPath);
let content = await loadFile(inputPath);
if (Object.keys(vars).length && typeof content === 'string') {
content = liquid(content, vars, inputPath, {
conditions: 'strict',
substitutions: false,
cycles: false,
});
}

await mkdir(dirname(xliffPath), {recursive: true});

Expand Down
14 changes: 14 additions & 0 deletions src/commands/translate/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ const exclude = option({
parser: toArray,
});

const vars = option({
flags: '-v, --vars <json>',
desc: `
Pass list of variables directly to translation.
Variables should be passed in JSON format.
Translation command ignores any presets.yaml.
Example:
{{PROGRAM}} -i ./ -o ./build -v '{"name":"test"}'
`,
parser: (value) => JSON.parse(value),
});

const dryRun = option({
flags: '--dry-run',
desc: 'Do not execute target translation provider, but only calculate required quota.',
Expand All @@ -105,6 +118,7 @@ export const options = {
files,
include,
exclude,
vars,
dryRun,
useSource,
};
7 changes: 6 additions & 1 deletion src/commands/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Extract} from './commands/extract';
import {Compose} from './commands/compose';
import {Extension as YandexTranslation} from './providers/yandex';

import {resolveFiles, resolveSource, resolveTargets} from './utils';
import {resolveFiles, resolveSource, resolveTargets, resolveVars} from './utils';

type Parent = IProgram & {
translate: Translate;
Expand Down Expand Up @@ -42,6 +42,7 @@ export type TranslateArgs = ProgramArgs & {
target?: string | string[];
include?: string[];
exclude?: string[];
vars?: Record<string, any>;
};

export type TranslateConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'> & {
Expand All @@ -52,6 +53,7 @@ export type TranslateConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'>
include: string[];
exclude: string[];
files: string[];
vars: Record<string, any>;
dryRun: boolean;
};

Expand Down Expand Up @@ -101,6 +103,7 @@ export class Translate
options.files,
options.include,
options.exclude,
options.vars,
options.dryRun,
options.config(YFM_CONFIG_FILENAME),
];
Expand Down Expand Up @@ -141,6 +144,7 @@ export class Translate
source.language,
['.md', '.yaml'],
);
const vars = resolveVars(config, args);

return Object.assign(config, {
input,
Expand All @@ -152,6 +156,7 @@ export class Translate
files,
include,
exclude,
vars,
provider: defined('provider', args, config),
dryRun: defined('dryRun', args, config) || false,
});
Expand Down
17 changes: 14 additions & 3 deletions src/commands/translate/providers/yandex/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import axios, {AxiosError, AxiosResponse} from 'axios';
import {LogLevel, Logger} from '~/logger';
import {TranslateError, compose, dumpFile, extract, loadFile, resolveSchemas} from '../../utils';
import {AuthError, Defer, LimitExceed, RequestError, bytes} from './utils';
import liquid from '@diplodoc/transform/lib/liquid';

const REQUESTS_LIMIT = 15;
const BYTES_LIMIT = 10000;
Expand All @@ -27,7 +28,7 @@ export class Provider {
}

async translate(files: string[], config: TranslateConfig & YandexTranslationConfig) {
const {input, output, auth, folder, source, target: targets, dryRun} = config;
const {input, output, auth, folder, source, target: targets, vars, dryRun} = config;

try {
for (const target of targets) {
Expand All @@ -39,6 +40,7 @@ export class Provider {
targetLanguage: target.language,
// yandexCloudTranslateGlossaryPairs,
folderId: folder,
vars,
dryRun,
};

Expand Down Expand Up @@ -90,6 +92,7 @@ type TranslatorParams = {
output: string;
sourceLanguage: string;
targetLanguage: string;
vars: Record<string, any>;
// yandexCloudTranslateGlossaryPairs: YandexCloudTranslateGlossaryPair[];
};

Expand Down Expand Up @@ -233,7 +236,7 @@ function requester(params: RequesterParams, cache: Cache) {
}

function translator(params: TranslatorParams, split: Split) {
const {input, output, sourceLanguage, targetLanguage} = params;
const {input, output, sourceLanguage, targetLanguage, vars} = params;
const inputRoot = resolve(input);
const outputRoot = resolve(output);

Expand All @@ -245,7 +248,15 @@ function translator(params: TranslatorParams, split: Split) {

const inputPath = join(inputRoot, path);
const outputPath = join(outputRoot, path.replace(sourceLanguage, targetLanguage));
const content = await loadFile(inputPath);

let content = await loadFile(inputPath);
if (Object.keys(vars).length && typeof content === 'string') {
content = liquid(content, vars, inputPath, {
conditions: 'strict',
substitutions: false,
cycles: false,
});
}

await mkdir(dirname(outputPath), {recursive: true});

Expand Down
8 changes: 8 additions & 0 deletions src/commands/translate/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ok} from 'node:assert';
import {dirname, isAbsolute, relative, resolve} from 'node:path';
import {readFileSync} from 'node:fs';
import glob from 'glob';
import {merge} from 'lodash';
import {filter} from 'minimatch';
import {defined} from '~/config';

Expand Down Expand Up @@ -145,3 +146,10 @@ export function resolveFiles(

return result;
}

export function resolveVars(
config: {vars?: Record<string, any>},
args: {vars?: Record<string, any>},
) {
return merge(config.vars || {}, args.vars);
}
2 changes: 1 addition & 1 deletion src/commands/translate/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type {Locale} from './config';
export {dumpFile, loadFile, resolveSchemas} from './fs';
export {extract, compose} from './translate';
export {resolveSource, resolveTargets, resolveFiles} from './config';
export {resolveSource, resolveTargets, resolveFiles, resolveVars} from './config';
export {TranslateError, ExtractError, ComposeError} from './errors';

0 comments on commit ce2d387

Please sign in to comment.