Unverified Commit 94ca7b4a authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[gen-l10n] NNBD generated code (#70415)

* NNBD gen-l10n generated code
parent d8167b90
...@@ -455,7 +455,7 @@ class _DefaultPub implements Pub { ...@@ -455,7 +455,7 @@ class _DefaultPub implements Pub {
if (!generateSyntheticPackage) { if (!generateSyntheticPackage) {
return; return;
} }
final Package flutterGen = Package('flutter_gen', generatedDirectory.uri, languageVersion: LanguageVersion(2, 8)); final Package flutterGen = Package('flutter_gen', generatedDirectory.uri, languageVersion: LanguageVersion(2, 12));
if (packageConfig.packages.any((Package package) => package.name == 'flutter_gen')) { if (packageConfig.packages.any((Package package) => package.name == 'flutter_gen')) {
return; return;
} }
......
...@@ -47,7 +47,7 @@ import 'package:intl/intl.dart' as intl; ...@@ -47,7 +47,7 @@ import 'package:intl/intl.dart' as intl;
/// # Internationalization support. /// # Internationalization support.
/// flutter_localizations: /// flutter_localizations:
/// sdk: flutter /// sdk: flutter
/// intl: 0.17.0-nullsafety.2 /// intl: ^0.17.0-nullsafety.2
/// ///
/// # rest of dependencies /// # rest of dependencies
/// ``` /// ```
...@@ -72,12 +72,12 @@ import 'package:intl/intl.dart' as intl; ...@@ -72,12 +72,12 @@ import 'package:intl/intl.dart' as intl;
/// be consistent with the languages listed in the @(class).supportedLocales /// be consistent with the languages listed in the @(class).supportedLocales
/// property. /// property.
abstract class @(class) { abstract class @(class) {
@(class)(String locale) : assert(locale != null), localeName = intl.Intl.canonicalizedLocale(locale.toString()); @(class)(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString());
// ignore: unused_field // ignore: unused_field
final String localeName; final String localeName;
static @(class) of(BuildContext context) { static @(class)? of(BuildContext context) {
return Localizations.of<@(class)>(context, @(class)); return Localizations.of<@(class)>(context, @(class));
} }
...@@ -222,15 +222,25 @@ const String loadBodyDeferredLoadingTemplate = '''return @(lookupName)(locale);' ...@@ -222,15 +222,25 @@ const String loadBodyDeferredLoadingTemplate = '''return @(lookupName)(locale);'
const String lookupFunctionTemplate = r''' const String lookupFunctionTemplate = r'''
@(class) @(lookupName)(Locale locale) { @(class) @(lookupName)(Locale locale) {
@(lookupBody) @(lookupBody)
assert(false, '@(class).delegate failed to load unsupported locale "$locale"');
return null; throw FlutterError(
'@(class).delegate failed to load unsupported locale "$locale". This is likely '
'an issue with the localizations generation tool. Please file an issue '
'on GitHub with a reproducible sample app and the gen-l10n configuration '
'that was used.'
);
}'''; }''';
const String lookupFunctionDeferredLoadingTemplate = r''' const String lookupFunctionDeferredLoadingTemplate = r'''
Future<@(class)> @(lookupName)(Locale locale) { Future<@(class)> @(lookupName)(Locale locale) {
@(lookupBody) @(lookupBody)
assert(false, '@(class).delegate failed to load unsupported locale "$locale"');
return null; throw FlutterError(
'@(class).delegate failed to load unsupported locale "$locale". This is likely '
'an issue with the localizations generation tool. Please file an issue '
'on GitHub with a reproducible sample app and the gen-l10n configuration '
'that was used.'
);
}'''; }''';
const String lookupBodyTemplate = ''' const String lookupBodyTemplate = '''
......
...@@ -35,7 +35,7 @@ class GenL10nProject extends Project { ...@@ -35,7 +35,7 @@ class GenL10nProject extends Project {
final String pubspec = ''' final String pubspec = '''
name: test name: test
environment: environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0" sdk: ">=2.12.0-0 <3.0.0"
dependencies: dependencies:
flutter: flutter:
...@@ -47,17 +47,22 @@ dependencies: ...@@ -47,17 +47,22 @@ dependencies:
@override @override
final String main = r''' final String main = r'''
// @dart = 2.8
// generated localizations do not support null safety.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart'; import 'l10n/app_localizations.dart';
class LocaleBuilder extends StatelessWidget { class LocaleBuilder extends StatelessWidget {
const LocaleBuilder({ Key key, this.locale, this.test, this.callback }) : super(key: key); const LocaleBuilder({
final Locale locale; Key? key,
final String test; this.locale,
this.test,
required this.callback,
}) : super(key: key);
final Locale? locale;
final String? test;
final void Function (BuildContext context) callback; final void Function (BuildContext context) callback;
@override build(BuildContext context) { @override build(BuildContext context) {
return Localizations.override( return Localizations.override(
locale: locale, locale: locale,
...@@ -71,9 +76,15 @@ class LocaleBuilder extends StatelessWidget { ...@@ -71,9 +76,15 @@ class LocaleBuilder extends StatelessWidget {
} }
class ResultBuilder extends StatelessWidget { class ResultBuilder extends StatelessWidget {
const ResultBuilder({ Key key, this.test, this.callback }) : super(key: key); const ResultBuilder({
final String test; Key? key,
this.test,
required this.callback,
}) : super(key: key);
final String? test;
final void Function (BuildContext context) callback; final void Function (BuildContext context) callback;
@override build(BuildContext context) { @override build(BuildContext context) {
return Builder( return Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
...@@ -102,8 +113,8 @@ class Home extends StatelessWidget { ...@@ -102,8 +113,8 @@ class Home extends StatelessWidget {
int n = 0; int n = 0;
for (Locale locale in AppLocalizations.supportedLocales) { for (Locale locale in AppLocalizations.supportedLocales) {
String languageCode = locale.languageCode; String languageCode = locale.languageCode;
String countryCode = locale.countryCode; String? countryCode = locale.countryCode;
String scriptCode = locale.scriptCode; String? scriptCode = locale.scriptCode;
results.add('supportedLocales[$n]: languageCode: $languageCode, countryCode: $countryCode, scriptCode: $scriptCode'); results.add('supportedLocales[$n]: languageCode: $languageCode, countryCode: $countryCode, scriptCode: $scriptCode');
n += 1; n += 1;
} }
...@@ -114,8 +125,8 @@ class Home extends StatelessWidget { ...@@ -114,8 +125,8 @@ class Home extends StatelessWidget {
test: 'countryCode - en_CA', test: 'countryCode - en_CA',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- countryCode (en_CA) tests ---'); results.add('--- countryCode (en_CA) tests ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
results.add(AppLocalizations.of(context).hello("CA fallback World")); results.add(AppLocalizations.of(context)!.hello("CA fallback World"));
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -123,8 +134,8 @@ class Home extends StatelessWidget { ...@@ -123,8 +134,8 @@ class Home extends StatelessWidget {
test: 'countryCode - en_GB', test: 'countryCode - en_GB',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- countryCode (en_GB) tests ---'); results.add('--- countryCode (en_GB) tests ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
results.add(AppLocalizations.of(context).hello("GB fallback World")); results.add(AppLocalizations.of(context)!.hello("GB fallback World"));
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -132,17 +143,17 @@ class Home extends StatelessWidget { ...@@ -132,17 +143,17 @@ class Home extends StatelessWidget {
test: 'zh', test: 'zh',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- zh ---'); results.add('--- zh ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
results.add(AppLocalizations.of(context).helloWorlds(0)); results.add(AppLocalizations.of(context)!.helloWorlds(0));
results.add(AppLocalizations.of(context).helloWorlds(1)); results.add(AppLocalizations.of(context)!.helloWorlds(1));
results.add(AppLocalizations.of(context).helloWorlds(2)); results.add(AppLocalizations.of(context)!.helloWorlds(2));
// Should use the fallback language, in this case, // Should use the fallback language, in this case,
// "Hello 世界" should be displayed. // "Hello 世界" should be displayed.
results.add(AppLocalizations.of(context).hello("世界")); results.add(AppLocalizations.of(context)!.hello("世界"));
// helloCost is tested in 'zh' because 'es' currency format contains a // helloCost is tested in 'zh' because 'es' currency format contains a
// non-breaking space character (U+00A0), which if removed, // non-breaking space character (U+00A0), which if removed,
// makes it hard to decipher why the test is failing. // makes it hard to decipher why the test is failing.
results.add(AppLocalizations.of(context).helloCost("价钱", 123)); results.add(AppLocalizations.of(context)!.helloCost("价钱", 123));
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -150,7 +161,7 @@ class Home extends StatelessWidget { ...@@ -150,7 +161,7 @@ class Home extends StatelessWidget {
test: 'zh', test: 'zh',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- scriptCode: zh_Hans ---'); results.add('--- scriptCode: zh_Hans ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -158,7 +169,7 @@ class Home extends StatelessWidget { ...@@ -158,7 +169,7 @@ class Home extends StatelessWidget {
test: 'scriptCode - zh_Hant', test: 'scriptCode - zh_Hant',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- scriptCode - zh_Hant ---'); results.add('--- scriptCode - zh_Hant ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -166,7 +177,7 @@ class Home extends StatelessWidget { ...@@ -166,7 +177,7 @@ class Home extends StatelessWidget {
test: 'scriptCode - zh_TW_Hant', test: 'scriptCode - zh_TW_Hant',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- scriptCode - zh_Hant_TW ---'); results.add('--- scriptCode - zh_Hant_TW ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context)!.helloWorld);
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -174,7 +185,7 @@ class Home extends StatelessWidget { ...@@ -174,7 +185,7 @@ class Home extends StatelessWidget {
test: 'General formatting', test: 'General formatting',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- General formatting tests ---'); results.add('--- General formatting tests ---');
final AppLocalizations localizations = AppLocalizations.of(context); final AppLocalizations localizations = AppLocalizations.of(context)!;
results.addAll(<String>[ results.addAll(<String>[
'${localizations.helloWorld}', '${localizations.helloWorld}',
'${localizations.helloNewlineWorld}', '${localizations.helloNewlineWorld}',
...@@ -213,7 +224,7 @@ class Home extends StatelessWidget { ...@@ -213,7 +224,7 @@ class Home extends StatelessWidget {
test: '--- es ---', test: '--- es ---',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- es ---'); results.add('--- es ---');
final AppLocalizations localizations = AppLocalizations.of(context); final AppLocalizations localizations = AppLocalizations.of(context)!;
results.addAll(<String>[ results.addAll(<String>[
'${localizations.helloWorld}', '${localizations.helloWorld}',
'${localizations.helloNewlineWorld}', '${localizations.helloNewlineWorld}',
...@@ -254,7 +265,7 @@ class Home extends StatelessWidget { ...@@ -254,7 +265,7 @@ class Home extends StatelessWidget {
test: 'countryCode - es_419', test: 'countryCode - es_419',
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- es_419 ---'); results.add('--- es_419 ---');
final AppLocalizations localizations = AppLocalizations.of(context); final AppLocalizations localizations = AppLocalizations.of(context)!;
results.addAll([ results.addAll([
'${localizations.helloWorld}', '${localizations.helloWorld}',
'${localizations.helloWorlds(0)}', '${localizations.helloWorlds(0)}',
......
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