Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ivy): i18n - support setting locales for each translation file
Previously the target locale of a translation file had to be extracted from the contents of the translation file. Therefore it was an error if the translation file did not provide a target locale. Now an array of locales can be provided via the `translationFileLocales` option that overrides any target locale extracted from the file. This allows us to support translation files that do not have a target locale specified in their contents. // FW-1644 Fixes #33323
- Loading branch information
1 parent
5607ad8
commit 4c230c8
Showing
15 changed files
with
260 additions
and
104 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
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
34 changes: 0 additions & 34 deletions
34
packages/localize/src/tools/src/translate/translation_files/translation_file_loader.ts
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
packages/localize/src/tools/src/translate/translation_files/translation_loader.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,58 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 | ||
*/ | ||
import {Diagnostics} from '../../diagnostics'; | ||
import {FileUtils} from '../../file_utils'; | ||
import {TranslationBundle} from '../translator'; | ||
import {TranslationParser} from './translation_parsers/translation_parser'; | ||
|
||
/** | ||
* Use this class to load a collection of translation files from disk. | ||
*/ | ||
export class TranslationLoader { | ||
constructor(private translationParsers: TranslationParser[], private diagnostics: Diagnostics) {} | ||
|
||
/** | ||
* Load and parse the translation files into a collection of `TranslationBundles`. | ||
* | ||
* If there is a locale provided in `translationFileLocales` then this is used rather than the | ||
* locale extracted from the file itself. | ||
* If there is neither a provided locale nor a locale parsed from the file, then an error is | ||
* thrown. | ||
* If there are both a provided locale and a locale parsed from the file, and they are not the | ||
* same, then a warning is reported . | ||
* | ||
* @param translationFilePaths An array of absolute paths to the translation files. | ||
* @param translationFileLocales An array of locales for each of the translation files. | ||
*/ | ||
loadBundles(translationFilePaths: string[], translationFileLocales: (string|undefined)[]): | ||
TranslationBundle[] { | ||
return translationFilePaths.map((filePath, index) => { | ||
const fileContents = FileUtils.readFile(filePath); | ||
for (const translationParser of this.translationParsers) { | ||
if (translationParser.canParse(filePath, fileContents)) { | ||
const providedLocale = translationFileLocales[index]; | ||
const {locale: parsedLocale, translations} = | ||
translationParser.parse(filePath, fileContents); | ||
const locale = providedLocale || parsedLocale; | ||
if (locale === undefined) { | ||
throw new Error( | ||
`The translation file "${filePath}" does not contain a target locale and no explicit locale was provided for this file.`); | ||
} | ||
if (parsedLocale !== undefined && providedLocale !== undefined && | ||
parsedLocale !== providedLocale) { | ||
this.diagnostics.warn( | ||
`The provided locale "${providedLocale}" does not match the target locale "${parsedLocale}" found in the translation file "${filePath}".`); | ||
} | ||
return {locale, translations}; | ||
} | ||
} | ||
throw new Error( | ||
`There is no "TranslationParser" that can parse this translation file: ${filePath}.`); | ||
}); | ||
} | ||
} |
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.