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 { ...@@ -63,9 +63,8 @@ Future<void> main(List<String> arguments) async {
exit(0); exit(0);
} }
final String flutterRoot = Platform.environment['FLUTTER_ROOT']; await precacheLanguageAndRegionTags();
final String flutterBin = Platform.isWindows ? 'flutter.bat' : 'flutter';
final String flutterPath = flutterRoot == null ? flutterBin : path.join(flutterRoot, 'bin', flutterBin);
final String arbPathString = results['arb-dir'] as String; final String arbPathString = results['arb-dir'] as String;
final String outputFileString = results['output-localization-file'] as String; final String outputFileString = results['output-localization-file'] as String;
final String templateArbFileName = results['template-arb-file'] as String; final String templateArbFileName = results['template-arb-file'] as String;
...@@ -74,6 +73,7 @@ Future<void> main(List<String> arguments) async { ...@@ -74,6 +73,7 @@ Future<void> main(List<String> arguments) async {
const local.LocalFileSystem fs = local.LocalFileSystem(); const local.LocalFileSystem fs = local.LocalFileSystem();
final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs); final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs);
try { try {
localizationsGenerator localizationsGenerator
..initialize( ..initialize(
...@@ -83,9 +83,8 @@ Future<void> main(List<String> arguments) async { ...@@ -83,9 +83,8 @@ Future<void> main(List<String> arguments) async {
classNameString: classNameString, classNameString: classNameString,
preferredSupportedLocaleString: preferredSupportedLocaleString, preferredSupportedLocaleString: preferredSupportedLocaleString,
) )
..parseArbFiles() ..loadResources()
..generateClassMethods() ..writeOutputFile();
..generateOutputFile();
} on FileSystemException catch (e) { } on FileSystemException catch (e) {
exitWithError(e.message); exitWithError(e.message);
} on FormatException catch (e) { } on FormatException catch (e) {
...@@ -93,24 +92,4 @@ Future<void> main(List<String> arguments) async { ...@@ -93,24 +92,4 @@ Future<void> main(List<String> arguments) async {
} on L10nException catch (e) { } on L10nException catch (e) {
exitWithError(e.message); 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 @@ ...@@ -2,80 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
const String getterMethodTemplate = ''' const String fileTemplate = '''
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 = '''
import 'dart:async'; import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.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 /// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(className).of(context)`. /// by `@(class).of(context)`.
/// ///
/// Applications need to include `@(className).delegate()` in their app's /// Applications need to include `@(class).delegate()` in their app\'s
/// localizationDelegates list, and the locales they support in the app's /// localizationDelegates list, and the locales they support in the app\'s
/// supportedLocales list. For example: /// supportedLocales list. For example:
/// ///
/// ``` /// ```
/// import '@(importFile)'; /// import '@(importFile)';
/// ///
/// return MaterialApp( /// return MaterialApp(
/// localizationsDelegates: @(className).localizationsDelegates, /// localizationsDelegates: @(class).localizationsDelegates,
/// supportedLocales: @(className).supportedLocales, /// supportedLocales: @(class).supportedLocales,
/// home: MyApplicationHome(), /// home: MyApplicationHome(),
/// ); /// );
/// ``` /// ```
...@@ -113,26 +62,18 @@ import 'messages_all.dart'; ...@@ -113,26 +62,18 @@ import 'messages_all.dart';
/// Select and expand the newly-created Localizations item then, for each /// Select and expand the newly-created Localizations item then, for each
/// locale your application supports, add a new item and select the locale /// 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 /// 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. /// property.
abstract class @(class) {
// ignore_for_file: unnecessary_brace_in_string_interps @(class)(String locale) : assert(locale != null), _localeName = intl.Intl.canonicalizedLocale(locale.toString());
class @(className) {
@(className)(Locale locale) : _localeName = Intl.canonicalizedLocale(locale.toString());
final String _localeName; final String _localeName;
static Future<@(className)> load(Locale locale) { static @(class) of(BuildContext context) {
return initializeMessages(locale.toString()) return Localizations.of<@(class)>(context, @(class));
.then<@(className)>((_) => @(className)(locale));
}
static @(className) of(BuildContext context) {
return Localizations.of<@(className)>(context, @(className));
} }
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 /// A list of this localizations delegate along with the default localizations
/// delegates. /// delegates.
...@@ -152,21 +93,104 @@ class @(className) { ...@@ -152,21 +93,104 @@ class @(className) {
]; ];
/// A list of this localizations delegate's supported locales. /// 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)> { class _@(class)Delegate extends LocalizationsDelegate<@(class)> {
const _@(className)Delegate(); const _@(class)Delegate();
@override @override
Future<@(className)> load(Locale locale) => @(className).load(locale); Future<@(class)> load(Locale locale) {
return SynchronousFuture<@(class)>(@(lookupName)(locale));
}
@override @override
bool isSupported(Locale locale) => <String>[@(supportedLanguageCodes)].contains(locale.languageCode); bool isSupported(Locale locale) => <String>[@(supportedLanguageCodes)].contains(locale.languageCode);
@override @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