New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ivy): i18n - support setting locales for each translation file #33381
Closed
petebacondarwin
wants to merge
2
commits into
angular:master
from
petebacondarwin:i18n-missing-target-language
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding docs 👍