Unverified Commit 001c499f authored by Tae Hyung Kim's avatar Tae Hyung Kim Committed by GitHub

[gen_l10n] Warn users when placeholder types are converted to 'num' when using...

[gen_l10n] Warn users when placeholder types are converted to 'num' when using pluralization (#108036)

* init

* asdf

* fix tests

* change warning slightly

* fix test again

* fix spacing and comment

* fix lint
parent f1df76e9
......@@ -252,9 +252,10 @@ class GenerateLocalizationsCommand extends FlutterCommand {
areResourceAttributesRequired: areResourceAttributesRequired,
untranslatedMessagesFile: untranslatedMessagesFile,
usesNullableGetter: usesNullableGetter,
logger: _logger,
)
..loadResources()
..writeOutputFiles(_logger);
..writeOutputFiles();
} on L10nException catch (e) {
throwToolExit(e.message);
}
......
......@@ -64,9 +64,10 @@ LocalizationsGenerator generateLocalizations({
areResourceAttributesRequired: options.areResourceAttributesRequired,
untranslatedMessagesFile: options.untranslatedMessagesFile?.toFilePath(),
usesNullableGetter: options.usesNullableGetter,
logger: logger,
)
..loadResources()
..writeOutputFiles(logger, isFromYaml: true);
..writeOutputFiles(isFromYaml: true);
} on L10nException catch (e) {
throwToolExit(e.message);
}
......@@ -698,6 +699,7 @@ class LocalizationsGenerator {
bool areResourceAttributesRequired = false,
String? untranslatedMessagesFile,
bool usesNullableGetter = true,
required Logger logger,
}) {
final Directory? projectDirectory = projectDirFromPath(fileSystem, projectPathString);
final Directory inputDirectory = inputDirectoryFromPath(fileSystem, inputPathString, projectDirectory);
......@@ -718,6 +720,7 @@ class LocalizationsGenerator {
untranslatedMessagesFile: _untranslatedMessagesFileFromPath(fileSystem, untranslatedMessagesFile),
inputsAndOutputsListFile: _inputsAndOutputsListFileFromPath(fileSystem, inputsAndOutputsListPath),
areResourceAttributesRequired: areResourceAttributesRequired,
logger: logger,
);
}
......@@ -739,6 +742,7 @@ class LocalizationsGenerator {
this.areResourceAttributesRequired = false,
this.untranslatedMessagesFile,
this.usesNullableGetter = true,
required this.logger,
});
final FileSystem _fs;
......@@ -859,6 +863,9 @@ class LocalizationsGenerator {
@visibleForTesting
final bool areResourceAttributesRequired;
/// Logger to be used during the execution of the script.
Logger logger;
static final RegExp _selectRE = RegExp(r'\{([\w\s,]*),\s*select\s*,\s*([\w\d]+\s*\{.*\})+\s*\}');
static bool _isNotReadable(FileStat fileStat) {
......@@ -1141,6 +1148,21 @@ class LocalizationsGenerator {
);
});
for (final Message message in messages) {
if (message.isPlural) {
if (message.placeholders.isEmpty) {
throw L10nException(
'Unable to find placeholders for the plural message: ${message.resourceId}.\n'
'Check to see if the plural message is in the proper ICU syntax format '
'and ensure that placeholders are properly specified.');
}
final Placeholder countPlaceholder = message.getCountPlaceholder();
if (countPlaceholder.type != null && countPlaceholder.type != 'num') {
logger.printWarning("Placeholders for plurals are automatically converted to type 'num' for the message: ${message.resourceId}.");
}
}
}
return classFileTemplate
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n\n')
.replaceAll('@(language)', describeLocale(locale.toString()))
......@@ -1317,7 +1339,7 @@ class LocalizationsGenerator {
|| message.placeholdersRequireFormatting;
});
void writeOutputFiles(Logger logger, { bool isFromYaml = false }) {
void writeOutputFiles({ bool isFromYaml = false }) {
// First, generate the string contents of all necessary files.
final String generatedLocalizationsFile = _generateCode();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment