Unverified Commit 13e1336e authored by Hans Muller's avatar Hans Muller Committed by GitHub

Generate message lookup in gen_l10n (#50733)

parent 9b3754d5
......@@ -63,9 +63,8 @@ Future<void> main(List<String> arguments) async {
exit(0);
}
final String flutterRoot = Platform.environment['FLUTTER_ROOT'];
final String flutterBin = Platform.isWindows ? 'flutter.bat' : 'flutter';
final String flutterPath = flutterRoot == null ? flutterBin : path.join(flutterRoot, 'bin', flutterBin);
await precacheLanguageAndRegionTags();
final String arbPathString = results['arb-dir'] as String;
final String outputFileString = results['output-localization-file'] as String;
final String templateArbFileName = results['template-arb-file'] as String;
......@@ -74,6 +73,7 @@ Future<void> main(List<String> arguments) async {
const local.LocalFileSystem fs = local.LocalFileSystem();
final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs);
try {
localizationsGenerator
..initialize(
......@@ -83,9 +83,8 @@ Future<void> main(List<String> arguments) async {
classNameString: classNameString,
preferredSupportedLocaleString: preferredSupportedLocaleString,
)
..parseArbFiles()
..generateClassMethods()
..generateOutputFile();
..loadResources()
..writeOutputFile();
} on FileSystemException catch (e) {
exitWithError(e.message);
} on FormatException catch (e) {
......@@ -93,24 +92,4 @@ Future<void> main(List<String> arguments) async {
} on L10nException catch (e) {
exitWithError(e.message);
}
final ProcessResult pubGetResult = await Process.run(flutterPath, <String>['pub', 'get']);
if (pubGetResult.exitCode != 0) {
stderr.write(pubGetResult.stderr);
exit(1);
}
final ProcessResult generateFromArbResult = await Process.run(flutterPath, <String>[
'pub',
'run',
'intl_translation:generate_from_arb',
'--output-dir=${localizationsGenerator.l10nDirectory.path}',
'--no-use-deferred-loading',
localizationsGenerator.outputFile.path,
...localizationsGenerator.arbPathStrings,
]);
if (generateFromArbResult.exitCode != 0) {
stderr.write(generateFromArbResult.stderr);
exit(1);
}
}
This diff is collapsed.
......@@ -2,80 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const String getterMethodTemplate = '''
String get @(methodName) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
''';
const String simpleMethodTemplate = '''
String @(methodName)(@(methodParameters)) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
''';
const String formatMethodTemplate = '''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
String @(methodName)(@(innerMethodParameters)) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
return @(methodName)(@(innerMethodArgs));
}
''';
const String pluralMethodTemplate = '''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
return Intl.plural(
@(intlMethodArgs)
);
}
''';
const String pluralFormatMethodTemplate = '''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
String @(methodName)(@(innerMethodParameters)) {
return Intl.plural(
@(intlMethodArgs)
);
}
return @(methodName)(@(innerMethodArgs));
}
''';
const String defaultFileTemplate = '''
const String fileTemplate = '''
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';
import 'package:intl/intl.dart' as intl;
import 'messages_all.dart';
// ignore_for_file: unnecessary_brace_in_string_interps
/// Callers can lookup localized strings with an instance of @(className) returned
/// by `@(className).of(context)`.
/// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(class).of(context)`.
///
/// Applications need to include `@(className).delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's
/// Applications need to include `@(class).delegate()` in their app\'s
/// localizationDelegates list, and the locales they support in the app\'s
/// supportedLocales list. For example:
///
/// ```
/// import '@(importFile)';
///
/// return MaterialApp(
/// localizationsDelegates: @(className).localizationsDelegates,
/// supportedLocales: @(className).supportedLocales,
/// localizationsDelegates: @(class).localizationsDelegates,
/// supportedLocales: @(class).supportedLocales,
/// home: MyApplicationHome(),
/// );
/// ```
......@@ -113,26 +62,18 @@ import 'messages_all.dart';
/// Select and expand the newly-created Localizations item then, for each
/// locale your application supports, add a new item and select the locale
/// you wish to add from the pop-up menu in the Value field. This list should
/// be consistent with the languages listed in the @(className).supportedLocales
/// be consistent with the languages listed in the @(class).supportedLocales
/// property.
// ignore_for_file: unnecessary_brace_in_string_interps
class @(className) {
@(className)(Locale locale) : _localeName = Intl.canonicalizedLocale(locale.toString());
abstract class @(class) {
@(class)(String locale) : assert(locale != null), _localeName = intl.Intl.canonicalizedLocale(locale.toString());
final String _localeName;
static Future<@(className)> load(Locale locale) {
return initializeMessages(locale.toString())
.then<@(className)>((_) => @(className)(locale));
}
static @(className) of(BuildContext context) {
return Localizations.of<@(className)>(context, @(className));
static @(class) of(BuildContext context) {
return Localizations.of<@(class)>(context, @(class));
}
static const LocalizationsDelegate<@(className)> delegate = _@(className)Delegate();
static const LocalizationsDelegate<@(class)> delegate = _@(class)Delegate();
/// A list of this localizations delegate along with the default localizations
/// delegates.
......@@ -152,21 +93,104 @@ class @(className) {
];
/// A list of this localizations delegate's supported locales.
@(supportedLocales)
static const List<Locale> supportedLocales = <Locale>[
@(supportedLocales)
];
@(classMethods)
}
@(methods)}
class _@(className)Delegate extends LocalizationsDelegate<@(className)> {
const _@(className)Delegate();
class _@(class)Delegate extends LocalizationsDelegate<@(class)> {
const _@(class)Delegate();
@override
Future<@(className)> load(Locale locale) => @(className).load(locale);
Future<@(class)> load(Locale locale) {
return SynchronousFuture<@(class)>(@(lookupName)(locale));
}
@override
bool isSupported(Locale locale) => <String>[@(supportedLanguageCodes)].contains(locale.languageCode);
@override
bool shouldReload(_@(className)Delegate old) => false;
bool shouldReload(_@(class)Delegate old) => false;
}
@(allMessagesClasses)
@(class) @(lookupName)(Locale locale) {
switch(locale.languageCode) {
@(lookupBody)
}
assert(false, '@(class).delegate failed to load unsupported locale "\$locale"');
return null;
}
''';
const String numberFormatTemplate = '''
final intl.NumberFormat @(placeholder)NumberFormat = intl.NumberFormat.@(format)(
locale: _localeName,
@(parameters)
);
final String @(placeholder)String = @(placeholder)NumberFormat.format(@(placeholder));
''';
const String dateFormatTemplate = '''
final intl.DateFormat @(placeholder)DateFormat = intl.DateFormat.@(format)(_localeName);
final String @(placeholder)String = @(placeholder)DateFormat.format(@(placeholder));
''';
const String getterTemplate = '''
@override
String get @(name) => @(message);''';
const String methodTemplate = '''
@override
String @(name)(@(parameters)) {
return @(message);
}''';
const String formatMethodTemplate = '''
@override
String @(name)(@(parameters)) {
@(dateFormatting)
@(numberFormatting)
return @(message);
}''';
const String pluralMethodTemplate = '''
@override
String @(name)(@(parameters)) {
@(dateFormatting)
@(numberFormatting)
return intl.Intl.pluralLogic(
@(count),
locale: _localeName,
@(pluralLogicArgs),
);
}''';
const String classTemplate = '''
/// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseClass) {
@(class)([String locale = '@(localeName)']) : super(locale);
@(methods)
}''';
const String baseClassGetterTemplate = '''
// @(comment)
String get @(name);
''';
const String baseClassMethodTemplate = '''
// @(comment)
String @(name)(@(parameters));
''';
const String switchClauseTemplate = '''case '@(case)': return @(class)();''';
const String countryCodeSwitchTemplate = '''case '@(languageCode)': {
switch (locale.countryCode) {
@(switchClauses)
}
return @(class)();
}''';
This diff is collapsed.
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