Skip to content
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
11 changes: 1 addition & 10 deletions src/commands/translate/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {ok} from 'node:assert';
import {join, resolve} from 'node:path';
import {pick} from 'lodash';
import {asyncify, eachLimit} from 'async';
import liquid from '@diplodoc/transform/lib/liquid';
// @ts-ignore
import {Xliff} from '@diplodoc/translation/lib/experiment/xliff/xliff';

Expand Down Expand Up @@ -214,7 +213,7 @@ export type PipelineParameters = {
};

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

Expand All @@ -230,14 +229,6 @@ function pipeline(params: PipelineParameters) {
return join(outputRoot, targetPath);
};

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

const {schemas, ajvOptions} = await resolveSchemas({
content,
path,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/translate/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function configDefaults() {
template: {
enabled: true,
features: {
conditions: true,
substitutions: true,
conditions: 'strict',
substitutions: false,
},
scopes: {
code: false,
Expand Down
3 changes: 2 additions & 1 deletion src/core/markdown/MarkdownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type MarkdownServiceConfig = {
enabled: boolean;
features: {
substitutions: boolean;
conditions: boolean;
conditions?: boolean | 'strict';
};
scopes: {
code: boolean;
Expand Down Expand Up @@ -396,6 +396,7 @@ export class MarkdownService {
conditions: this.config.template.features.conditions,
conditionsInCode: this.config.template.scopes.code,
keepNotVar: this.config.outputFormat === 'md',
keepConditionSyntaxOnTrue: this.options.mode === 'translate',
},
options: {
disableLiquid: !this.config.template.enabled,
Expand Down
10 changes: 8 additions & 2 deletions src/core/markdown/loader/template-content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {LoaderContext} from '../loader';

import {liquidSnippet} from '@diplodoc/liquid';
import {liquidDocument, liquidSnippet} from '@diplodoc/liquid';

export function templateContent(this: LoaderContext, rawContent: string) {
const {vars, options} = this;
Expand All @@ -10,7 +10,13 @@ export function templateContent(this: LoaderContext, rawContent: string) {
return rawContent;
}

const content = liquidSnippet.call(this, rawContent, vars, this.sourcemap);
let content;

if (this.mode === 'translate') {
content = liquidDocument.call(this, rawContent, vars);
} else {
content = liquidSnippet.call(this, rawContent, vars, this.sourcemap);
}

return content;
}
153 changes: 153 additions & 0 deletions tests/e2e/__snapshots__/translation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,60 @@ exports[`Translate command > filter files on extract with extra vars option > fi
]"
`;

exports[`Translate command > remove falsy liquid conditions structures > filelist 1`] = `
"[
"index.md.skl",
"index.md.xliff"
]"
`;

exports[`Translate command > remove falsy liquid conditions structures 1`] = `
"%%%0%%%
<!-- [missed file](./missed.md) -->




#### %%%1%%%

{% if list contains "item" %}

%%%2%%%

{% endif %}

#### %%%3%%%

"
`;

exports[`Translate command > remove falsy liquid conditions structures 2`] = `
"<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="file.ext" source-language="ru-RU" target-language="es-ES" datatype="markdown">
<header>
<skeleton>
<external-file href="file.skl"></external-file>
</skeleton>
</header>
<body>
<trans-unit id="0">
<source xml:space="preserve"><g ctype="link" equiv-text="[{{text}}](./exists.md)" id="g-1" x-begin="[" x-end="](./exists.md)">existing file</g></source>
</trans-unit>
<trans-unit id="1">
<source xml:space="preserve">Standalone contains condition</source>
</trans-unit>
<trans-unit id="2">
<source xml:space="preserve">#### List</source>
</trans-unit>
<trans-unit id="3">
<source xml:space="preserve">Inline contains condition</source>
</trans-unit>
</body>
</file>
</xliff>"
`;

exports[`Translate command > removes no-translate directive and leaves content as is > filelist 1`] = `
"[
"index.md.skl",
Expand Down Expand Up @@ -2077,6 +2131,105 @@ exports[`Translate command > removes no-translate directive and leaves content a
</xliff>"
`;

exports[`Translate command > save truthy liquid conditions structures > filelist 1`] = `
"[
"index.md.skl",
"index.md.xliff"
]"
`;

exports[`Translate command > save truthy liquid conditions structures 1`] = `
"%%%0%%%
<!-- [missed file](./missed.md) -->
{% if prod == true %}%%%1%%%{% endif %}

{% if inner == true %}%%%2%%%{% endif %}

{% if prod == true %}%%%3%%%{% endif %}{% endif %}

{% if prod == true %}

{% if list contains "item" %}

%%%4%%%

{% if item == true %}%%%5%%% %%%6%%% {% endif %}

%%%7%%%

{% endif %}

{% endif %}

#### %%%8%%%

{% if list contains "item" %}

%%%9%%%

{% endif %}

#### %%%10%%%

{% if prod == true %}

%%%11%%% {% endif %}

{% endif %}
"
`;

exports[`Translate command > save truthy liquid conditions structures 2`] = `
"<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="file.ext" source-language="ru-RU" target-language="es-ES" datatype="markdown">
<header>
<skeleton>
<external-file href="file.skl"></external-file>
</skeleton>
</header>
<body>
<trans-unit id="0">
<source xml:space="preserve"><g ctype="link" equiv-text="[{{text}}](./exists.md)" id="g-1" x-begin="[" x-end="](./exists.md)">existing file</g></source>
</trans-unit>
<trans-unit id="1">
<source xml:space="preserve">Test text</source>
</trans-unit>
<trans-unit id="2">
<source xml:space="preserve">inner test text</source>
</trans-unit>
<trans-unit id="3">
<source xml:space="preserve">Test text<x ctype="liquid_Literal" equiv-text="{% if inner == true %}" id="x-1"/>inner test text</source>
</trans-unit>
<trans-unit id="4">
<source xml:space="preserve">#### List</source>
</trans-unit>
<trans-unit id="5">
<source xml:space="preserve">1.</source>
</trans-unit>
<trans-unit id="6">
<source xml:space="preserve">Item</source>
</trans-unit>
<trans-unit id="7">
<source xml:space="preserve">Some text</source>
</trans-unit>
<trans-unit id="8">
<source xml:space="preserve">Standalone contains condition</source>
</trans-unit>
<trans-unit id="9">
<source xml:space="preserve">#### List</source>
</trans-unit>
<trans-unit id="10">
<source xml:space="preserve">Inline contains condition</source>
</trans-unit>
<trans-unit id="11">
<source xml:space="preserve">#### List <x ctype="liquid_Literal" equiv-text="{% if list contains &quot;item&quot; %}" id="x-2"/> sub text</source>
</trans-unit>
</body>
</file>
</xliff>"
`;

exports[`Translate command > skip no-translate marked content > filelist 1`] = `
"[
"index.md.skl",
Expand Down
26 changes: 26 additions & 0 deletions tests/e2e/translation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,30 @@ describe('Translate command', () => {
source: 'ru-RU',
target: 'en-US',
});

let conditionVars = {prod: true, inner: true, list: ['item']};
generateMapTestTemplate(
'save truthy liquid conditions structures',
'mocks/translation/conditions',
{
subcommand: 'extract',
source: 'ru-RU',
target: 'es-ES',
additionalArgs: `--vars ${JSON.stringify(conditionVars)}`,
},
false,
);

conditionVars = {prod: false, inner: false, list: ['item']};
generateMapTestTemplate(
'remove falsy liquid conditions structures',
'mocks/translation/conditions',
{
subcommand: 'extract',
source: 'ru-RU',
target: 'es-ES',
additionalArgs: `--vars ${JSON.stringify(conditionVars)}`,
},
false,
);
});
2 changes: 1 addition & 1 deletion tests/mocks/errors/extract-filtered-link/input/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

[second missed link](./filtered2.md)
[second missed link](./filtered2.md)
[second missed link](./filtered3.md)
[second missed link](./filtered3.md)
37 changes: 37 additions & 0 deletions tests/mocks/translation/conditions/input/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[existing file](./exists.md)
<!-- [missed file](./missed.md) -->
{% if prod == true %}Test text{% endif %}

{% if inner == true %}inner test text{% endif %}

{% if prod == true %}Test text{% if inner == true %}inner test text{% endif %}{% endif %}

{% if prod == true %}

{% if list contains "item" %}

#### List

{% if item == true %}1. Item {% endif %}

Some text

{% endif %}

{% endif %}

#### Standalone contains condition

{% if list contains "item" %}

#### List

{% endif %}

#### Inline contains condition

{% if prod == true %}

#### List {% if list contains "item" %} sub text {% endif %}

{% endif %}
Loading