Skip to content

Commit

Permalink
feat: add ability to define empty tag format (#110)
Browse files Browse the repository at this point in the history
add a new setting called `useSelfClosingTags` which allow to define how empty tag must be write while generating new XLIFF synchronized files.

its default value is `true` meaning empty tag will use self closing format (<note/>) - as it was the case prior this change.

when set to `false`, empty tag will be write using opening and closing format (<note></note>).

references :
- oozcitak/xmlbuilder-js#65
- https://github.com/oozcitak/xmlbuilder-js/wiki#converting-to-string

Closes #97
  • Loading branch information
warlof authored Nov 28, 2022
1 parent 6db79b7 commit b1ae856
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ More information: [XLIFF Sync: Time for a complete overview](https://robvanbekku
| xliffSync.ignoreLineEndingTypeChanges | `false` | Specifies whether changes in line ending type (CRLF vs. LF) should not be considered as changes to the source text of a trans-unit. |
| xliffSync.clearTranslationAfterSourceTextChange | `false` | Specifies whether translations should be cleared when the source text of a trans-unit changed. |
| xliffSync.addNeedsWorkTranslationNote | `true` | Specifies whether an XLIFF Sync note should be added to explain why a trans-unit was marked as needs-work. |
| xliffSync.useSelfClosingTags | `true` | Determine how empty XML tags must be handled while generating new XLIFF files (`<note></note>` or `<note/>`). |
| xliffSync.keepEditorOpenAfterSync | `true` | Specifies whether XLIFF files should be opened in the editor after syncing. |
| xliffSync.openExternallyAfterEvent | `[]` | Specifies after which event translation files should be opened automatically with the default XLIFF editor. Options: "Check", "ProblemDetected", "Sync" |
| xliffSync.developerNoteDesignation | `Developer` | Specifies the name that is used to designate a developer note. |
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@
"description": "Specifies whether an XLIFF Sync note should be added to explain why a trans-unit was marked as needs-work.",
"scope": "resource"
},
"xliffSync.useSelfClosingTags": {
"type": "boolean",
"default": true,
"description": "Determine how empty XML tags must be handled while generating new XLIFF files (<note></note> or <note/>).",
"scope": "resource"
},
"xliffSync.keepEditorOpenAfterSync": {
"type": "boolean",
"default": true,
Expand All @@ -233,8 +239,7 @@
"Open automatically after syncing with the base translation file."
]
},
"default": [
],
"default": [],
"uniqueItems": true,
"description": "Specifies after which event translation files should be opened automatically with the default XLIFF editor",
"scope": "resource"
Expand Down
4 changes: 3 additions & 1 deletion src/features/tools/xlf/xlf-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class XlfDocument {
private missingTranslation: string;
private needsWorkTranslationSubstate: string;
private addNeedsWorkTranslationNote: boolean;
private useSelfClosingTags: boolean;

private idUnitMap: { [id: string]: XmlNode } | undefined;
private xliffGeneratorNoteSourceUnitMap: { [key: string]: XmlNode } | undefined;
Expand All @@ -196,6 +197,7 @@ export class XlfDocument {
}
this.needsWorkTranslationSubstate = xliffWorkspaceConfiguration['needsWorkTranslationSubstate'];
this.addNeedsWorkTranslationNote = xliffWorkspaceConfiguration['addNeedsWorkTranslationNote'];
this.useSelfClosingTags = xliffWorkspaceConfiguration['useSelfClosingTags'];
}

public static async loadFromUri(sourceUri: Uri, resourceUri: Uri | undefined): Promise<XlfDocument> {
Expand Down Expand Up @@ -259,7 +261,7 @@ export class XlfDocument {
let retVal: string | undefined;

if (this.valid) {
retVal = XmlBuilder.create(this.root)!;
retVal = XmlBuilder.create(this.root, false, ! this.useSelfClosingTags)!;

const rootIdx = retVal.indexOf('<xliff ');

Expand Down
4 changes: 2 additions & 2 deletions src/features/tools/xml-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { create, XMLElementOrXMLNode } from 'xmlbuilder';
import { XmlNode } from './xml-node';

export class XmlBuilder {
public static create(root: XmlNode | undefined, headless: boolean = false): string | undefined {
public static create(root: XmlNode | undefined, headless: boolean = false, allowEmpty: boolean = false): string | undefined {
if (!root) {
return undefined;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ export class XmlBuilder {
}

// TODO: pretty as an option ?
return outputNode.end();
return outputNode.end({ allowEmpty: allowEmpty });
}

private static appendNode(dest: XMLElementOrXMLNode, source: XmlNode): XMLElementOrXMLNode {
Expand Down

0 comments on commit b1ae856

Please sign in to comment.