Fix message type inconsistency between locales (#120129)

* init

* fix error handling

* fix issue

* lint?

* error handling tests

* lint
This commit is contained in:
Tae Hyung Kim
2023-02-13 15:05:57 -08:00
committed by GitHub
parent ddebe833b0
commit becb6bd00a
4 changed files with 158 additions and 27 deletions

View File

@@ -877,6 +877,7 @@ class LocalizationsGenerator {
_allMessages = _templateBundle.resourceIds.map((String id) => Message(
_templateBundle, _allBundles, id, areResourceAttributesRequired, useEscaping: useEscaping, logger: logger,
)).toList();
hadErrors = _allMessages.any((Message message) => message.hadErrors);
if (inputsAndOutputsListFile != null) {
_inputFileList.addAll(_allBundles.bundles.map((AppResourceBundle bundle) {
return bundle.file.absolute.path;
@@ -915,16 +916,19 @@ class LocalizationsGenerator {
final LocaleInfo locale,
) {
final Iterable<String> methods = _allMessages.map((Message message) {
LocaleInfo localeWithFallback = locale;
if (message.messages[locale] == null) {
_addUnimplementedMessage(locale, message.resourceId);
return _generateMethod(
message,
_templateArbLocale,
);
localeWithFallback = _templateArbLocale;
}
if (message.parsedMessages[localeWithFallback] == null) {
// The message exists, but parsedMessages[locale] is null due to a syntax error.
// This means that we have already set hadErrors = true while constructing the Message.
return '';
}
return _generateMethod(
message,
locale,
localeWithFallback,
);
});
@@ -953,7 +957,7 @@ class LocalizationsGenerator {
});
final Iterable<String> methods = _allMessages
.where((Message message) => message.messages[locale] != null)
.where((Message message) => message.parsedMessages[locale] != null)
.map((Message message) => _generateMethod(message, locale));
return subclassTemplate
@@ -1103,8 +1107,8 @@ class LocalizationsGenerator {
final String translationForMessage = message.messages[locale]!;
final Node node = message.parsedMessages[locale]!;
// If parse tree is only a string, then return a getter method.
if (node.children.every((Node child) => child.type == ST.string)) {
// If the placeholders list is empty, then return a getter method.
if (message.placeholders.isEmpty) {
// Use the parsed translation to handle escaping with the same behavior.
return getterTemplate
.replaceAll('@(name)', message.resourceId)

View File

@@ -349,13 +349,20 @@ class Message {
filenames[bundle.locale] = bundle.file.basename;
final String? translation = bundle.translationFor(resourceId);
messages[bundle.locale] = translation;
parsedMessages[bundle.locale] = translation == null ? null : Parser(
resourceId,
bundle.file.basename,
translation,
useEscaping: useEscaping,
logger: logger
).parse();
try {
parsedMessages[bundle.locale] = translation == null ? null : Parser(
resourceId,
bundle.file.basename,
translation,
useEscaping: useEscaping,
logger: logger
).parse();
} on L10nParserException catch (error) {
logger?.printError(error.toString());
// Treat it as an untranslated message in case we can't parse.
parsedMessages[bundle.locale] = null;
hadErrors = true;
}
}
// Infer the placeholders
_inferPlaceholders(filenames);
@@ -369,6 +376,7 @@ class Message {
final Map<String, Placeholder> placeholders;
final bool useEscaping;
final Logger? logger;
bool hadErrors = false;
bool get placeholdersRequireFormatting => placeholders.values.any((Placeholder p) => p.requiresFormatting);

View File

@@ -587,17 +587,8 @@ class Parser {
}
Node parse() {
try {
final Node syntaxTree = compress(parseIntoTree());
checkExtraRules(syntaxTree);
return syntaxTree;
} on L10nParserException catch (error) {
// For debugging purposes.
if (logger == null) {
rethrow;
}
logger?.printError(error.toString());
return Node(ST.empty, 0, value: '');
}
final Node syntaxTree = compress(parseIntoTree());
checkExtraRules(syntaxTree);
return syntaxTree;
}
}