Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(localize): enable whitespace preservation marker in XLIFF files
Whitespace can be relevant in extracted XLIFF translation files. Some i18n tools - e.g. CAT tool (OmegaT) - will reformat the file to collapse whitespace if there is no indication to tell it not to. This commit adds the ability to specify "format options" that are passed to the translation file serializer. The XLIFF 1.2 and 2.0 seralizers have been updated to accept `{"xml:space":"preserve"}` format option which will by added to the `<file>` element in the serialized translation file during extraction. Fixes #38679
- Loading branch information
1 parent
c90eb54
commit bed91d1
Showing
12 changed files
with
521 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/localize/src/tools/src/extract/translation_files/format_options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export type FormatOptions = Record<string, string>; | ||
export type ValidOption = [string, string[]]; | ||
export type ValidOptions = ValidOption[]; | ||
|
||
/** | ||
* Check that the given `options` are allowed based on the given `validOptions`. | ||
* @param name The name of the serializer that is receiving the options. | ||
* @param validOptions An array of valid options and their allowed values. | ||
* @param options The options to be validated. | ||
*/ | ||
export function validateOptions(name: string, validOptions: ValidOptions, options: FormatOptions) { | ||
const validOptionsMap = new Map<ValidOption[0], ValidOption[1]>(validOptions); | ||
for (const option in options) { | ||
if (!validOptionsMap.has(option)) { | ||
throw new Error( | ||
`Invalid format option for ${name}: "${option}".\n` + | ||
`Allowed options are ${JSON.stringify(Array.from(validOptionsMap.keys()))}.`); | ||
} | ||
const validOptionValues = validOptionsMap.get(option)!; | ||
const optionValue = options[option]; | ||
if (!validOptionValues.includes(optionValue)) { | ||
throw new Error( | ||
`Invalid format option value for ${name}: "${option}".\n` + | ||
`Allowed option values are ${JSON.stringify(validOptionValues)} but received "${ | ||
optionValue}".`); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Parse the given `optionString` into a collection of `FormatOptions`. | ||
* @param optionString The string to parse. | ||
*/ | ||
export function parseFormatOptions(optionString: string = '{}'): FormatOptions { | ||
return JSON.parse(optionString); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.