Skip to content

Commit

Permalink
feat: New Setting, SetExactMatchToState (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwikman authored Nov 4, 2024
1 parent 624fac2 commit 6a765a0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Added support for System Actions on pages of type PromptDialog.
- Added support for namespaces in AL objects.
- Added a new setting, `NAB.AutoAcceptSuggestions`. If enabled, the `NAB: Refresh XLF files from g.xlf` function will automatically accept the first suggestion, if any. This feature only works if `UseExternalTranslationTool` is disabled. Disabled by default. Thanks to [dirkmass](https://github.com/dirkmass) for suggesting this in [issue 470](https://github.com/jwikman/nab-al-tools/issues/470).
- Added a new setting, `NAB.SetExactMatchToState`. Specifies that when working with the target state attributes (`NAB.UseExternalTranslationTool: true`) and this is set to a target state value, when executing the `NAB: Refresh XLF files from g.xlf` function and an exact match is found, the target state will be set to the configured state and the state-qualifier will be set to `exact-match`.
- Fixes:
- Added support for captions in the `layout` of a `rendering` section in reports. Thanks to [lv-janpieter](https://github.com/lv-janpieter) for bringing us the attention to this in [issue 441](https://github.com/jwikman/nab-al-tools/issues/441).
- Removed a couple of semicolons from the test snippets (see [this post on X](https://twitter.com/arthrvdv/status/1727729430737342791) for more information)
Expand Down
1 change: 1 addition & 0 deletions extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ This extension contributes the following settings:
- `NAB.MatchTranslation`: If enabled, the `NAB: Refresh XLF files from g.xlf` function tries to match sources in the translated xlf file to reuse translations. A found match of "source" is then prefixed with `[NAB: SUGGESTION]` for manual review. If several matches are found, all matches are added as targets and you need delete the ones you do not want. Use `NAB: Find next untranslated text` (Ctrl+Alt+U) or `NAB: Find multiple targets in XLF files` to review all matches. This feature only works if "UseExternalTranslationTool" is disabled. Activated by default.
- `NAB.MatchBaseAppTranslation`: If enabled, the `NAB: Refresh XLF files from g.xlf` function tries to match sources in the translated xlf file with translations from the BaseApplication. A found match of `source` is then prefixed with [NAB: SUGGESTION] for manual review. If several matches are found, all matches are added and you need delete the ones you do not want. Use `NAB: Find next untranslated text` (Ctrl+Alt+U) or `NAB: Find multiple targets in XLF files` to review all matches. This feature only works if `UseExternalTranslationTool` is disabled. Disabled by default.
- `NAB.AutoAcceptSuggestions`: If enabled, the `NAB: Refresh XLF files from g.xlf` function will automatically accept the first suggestion, if any. This feature only works if `UseExternalTranslationTool` is disabled. Disabled by default.
- `NAB.SetExactMatchToState`: Specifies that when working with the target state attributes (`NAB.UseExternalTranslationTool: true`) and this is set to a target state value, when executing the `NAB: Refresh XLF files from g.xlf` function and an exact match is found, the target state will be set to the configured state and the state-qualifier will be set to `exact-match`.
- `NAB.TranslationSuggestionPaths`: Supply any relative or absolute paths that contains xlf files that should be used when matching translations. The `NAB: Refresh XLF files from g.xlf` function will try to match any untranslated targets with targets in the xlf files in the provided folders that has matching target language.
- `NAB.ShowXlfHighlights`: If enabled, all translation tags ([NAB: NOT TRANSLATED], [NAB: REVIEW] and [NAB: SUGGESTION]) will be highlighted ([Request 75](https://github.com/jwikman/nab-al-tools/issues/75)). Some common issues when writing targets manually is highlighted. Details found in [issue 71](https://github.com/jwikman/nab-al-tools/issues/71). Uses the style specified in `NAB.XlfHighlightsDecoration`.
- `NAB.XlfHighlightsDecoration`: Specifies the style that should be used to highlight inside xlf files.
Expand Down
32 changes: 31 additions & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,39 @@
"NAB.UseExternalTranslationTool": {
"type": "boolean",
"default": false,
"description": "Modifies the state-attribute of the translation unit when running 'NAB: Refresh XLF files from g.xlf' instead of inserting a searchable string. Useful when working with external translation software.",
"description": "Modifies the state attribute of the translation unit target when running 'NAB: Refresh XLF files from g.xlf', instead of inserting a searchable string. Useful when working with external translation tools.",
"scope": "resource"
},
"NAB.SetExactMatchToState": {
"type": "string",
"default": "translated",
"description": "Specifies that when working with the target state attributes (\"NAB.UseExternalTranslationTool\": true) and this is set to a target state value, when executing the \"NAB: Refresh XLF files from g.xlf\" function and an exact match is found, the target state will be set to the configured state and the state-qualifier will be set to \"exact-match\".",
"scope": "resource",
"enum": [
"translated",
"signed-off",
"final",
"new",
"needs-review-translation",
"needs-translation",
"needs-adaptation",
"needs-l10n",
"needs-review-adaptation",
"needs-review-l10n"
],
"enumDescriptions": [
"Sets the Target State to 'translated'",
"Sets the Target State to 'signed-off'",
"Sets the Target State to 'final'",
"Sets the Target State to 'new'",
"Sets the Target State to 'needs-review-translation'",
"Sets the Target State to 'needs-translation'",
"Sets the Target State to 'needs-adaptation'",
"Sets the Target State to 'needs-l10n'",
"Sets the Target State to 'needs-review-adaptation'",
"Sets the Target State to 'needs-review-l10n'"
]
},
"NAB.DetectInvalidTargets": {
"type": "boolean",
"default": true,
Expand Down
2 changes: 2 additions & 0 deletions extension/src/Settings/LanguageFunctionsSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Settings } from "./Settings";
export class LanguageFunctionsSettings {
translationMode: TranslationMode;
useExternalTranslationTool: boolean;
setExactMatchToState: TargetState;
searchOnlyXlfFiles: boolean;
detectInvalidValuesEnabled: boolean;
translationSuggestionPaths: string[];
Expand All @@ -25,6 +26,7 @@ export class LanguageFunctionsSettings {
constructor(settings: Settings) {
this.translationMode = this.getTranslationMode(settings);
this.useExternalTranslationTool = settings.useExternalTranslationTool;
this.setExactMatchToState = settings.setExactMatchToState;
this.searchOnlyXlfFiles = settings.searchOnlyXlfFiles;
this.detectInvalidValuesEnabled = settings.detectInvalidTargets;
this.translationSuggestionPaths = settings.translationSuggestionPaths;
Expand Down
6 changes: 5 additions & 1 deletion extension/src/Settings/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as path from "path";
import { ISearchReplaceBeforeSaveXliff } from "../Xliff/XLIFFDocument";
import {
ISearchReplaceBeforeSaveXliff,
TargetState,
} from "../Xliff/XLIFFDocument";

// When a new setting is added:
// 1. Add the setting in package.json, with default value
Expand Down Expand Up @@ -29,6 +32,7 @@ export class Settings {
},
};
public useExternalTranslationTool = false;
public setExactMatchToState: TargetState = TargetState.translated;
public detectInvalidTargets = true;
public useDTS = false;
public dtsProjectId = "";
Expand Down
1 change: 1 addition & 0 deletions extension/src/Settings/SettingsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const settingsMap = new Map<string, keyof Settings>([
["NAB.ShowXlfHighlights", "showXlfHighlights"],
["NAB.XlfHighlightsDecoration", "xlfHighlightsDecoration"],
["NAB.UseExternalTranslationTool", "useExternalTranslationTool"],
["NAB.SetExactMatchToState", "setExactMatchToState"],
["NAB.DetectInvalidTargets", "detectInvalidTargets"],
["NAB.UseDTS", "useDTS"],
["NAB.DTS ProjectId", "dtsProjectId"],
Expand Down
5 changes: 4 additions & 1 deletion extension/src/XliffFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,10 @@ export function matchTranslationsFromTranslationMap(
} else {
const match = matchMap.get(transUnit.source);
if (match !== undefined) {
const newTarget = new Target(match[0], TargetState.translated);
const newTargetState = languageFunctionsSettings.setExactMatchToState
? languageFunctionsSettings.setExactMatchToState
: TargetState.translated;
const newTarget = new Target(match[0], newTargetState);
newTarget.stateQualifier = StateQualifier.exactMatch;
transUnit.removeCustomNote(CustomNoteType.refreshXlfHint);
transUnit.targets = [];
Expand Down
68 changes: 64 additions & 4 deletions extension/src/test/LanguageFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LanguageFunctionsSettings } from "../Settings/LanguageFunctionsSettings
import {
CustomNoteType,
Note,
StateQualifier,
TargetState,
TranslationToken,
Xliff,
Expand Down Expand Up @@ -470,7 +471,66 @@ suite("Language Functions Tests", function () {
assert.strictEqual(
xlfDoc.transunit[0].target.textContent,
"Tillstånd",
"Unexpected textContent 0"
"Unexpected textContent"
);
assert.strictEqual(
xlfDoc.transunit[0].target.state,
TargetState.translated,
"Unexpected state"
);
assert.strictEqual(
xlfDoc.transunit[0].target.stateQualifier,
StateQualifier.exactMatch,
"Unexpected stateQualifier"
);
});

test("matchTranslationsFromTranslationMap(): TranslationMode.external, TargetStateFinal", async function () {
/*
* Test with Xlf that has multiple matching sources
* - Assert all matching sources gets suggestion in target.
* - Assert that the target state is set to Final.
*/
const languageFunctionsSettings = new LanguageFunctionsSettings(
SettingsLoader.getSettings()
);
languageFunctionsSettings.translationMode = TranslationMode.external;
languageFunctionsSettings.setExactMatchToState = TargetState.final;
const xlfDoc: Xliff = Xliff.fromString(
ALObjectTestLibrary.getXlfWithContextBasedMultipleMatchesInBaseApp()
);
const matchMap: Map<string, string[]> = new Map<string, string[]>();
matchMap.set("State", ["Tillstånd", "Status", "Delstat"]);
const matchResult = XliffFunctions.matchTranslationsFromTranslationMap(
xlfDoc,
matchMap,
languageFunctionsSettings
);

assert.strictEqual(
matchResult,
xlfDoc.transunit.length,
"Number of matched translations should equal 1"
);
assert.strictEqual(
xlfDoc.transunit[0].targets.length,
1,
"Expected 1 targets."
);
assert.strictEqual(
xlfDoc.transunit[0].target.textContent,
"Tillstånd",
"Unexpected textContent"
);
assert.strictEqual(
xlfDoc.transunit[0].target.state,
TargetState.final,
"Unexpected state"
);
assert.strictEqual(
xlfDoc.transunit[0].target.stateQualifier,
StateQualifier.exactMatch,
"Unexpected stateQualifier"
);
});

Expand Down Expand Up @@ -975,12 +1035,12 @@ suite("Language Functions Tests", function () {
assert.strictEqual(
langFuncSettings.exactMatchState,
"test" as TargetState,
"Expeted (keep) as Targetstate"
"Expected (keep) as Targetstate"
);
assert.strictEqual(
langFuncSettings.translationMode,
TranslationMode.dts,
"Expected tranlation mode to be set to DTS"
"Expected translation mode to be set to DTS"
);
});

Expand All @@ -993,7 +1053,7 @@ suite("Language Functions Tests", function () {
assert.strictEqual(
langFuncSettings.translationMode,
TranslationMode.external,
"Expected tranlation mode to be set to DTS"
"Expected translation mode to be set to DTS"
);
});

Expand Down

0 comments on commit 6a765a0

Please sign in to comment.