Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added null safety support #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bin/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ I18nOption _generateI18Option(List<String> args) {
return i18nOption;
}

ArgParser _generateArgParser(I18nOption i18nOption) {
ArgParser _generateArgParser(I18nOption? i18nOption) {
var parser = new ArgParser();

parser.addOption('source-dir',
defaultsTo: 'res/string',
callback: (String x) => i18nOption.sourceDir = x,
callback: (String? x) => i18nOption!.sourceDir = x,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (I18nOption == null) this will throw an error

help: 'A source folder contains all string json files');

parser.addOption('output-dir',
defaultsTo: 'lib/generated',
callback: (String x) => i18nOption.outputDir = x,
callback: (String? x) => i18nOption!.outputDir = x,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (I18nOption == null) this will throw an error

help: 'A output folder stores all generated files');

parser.addOption('template-locale',
defaultsTo: 'en',
callback: (String x) => i18nOption.templateLocale = x,
callback: (String? x) => i18nOption!.templateLocale = x,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (I18nOption == null) this will throw an error

help:
'Use string_{template-locale}.json as a template for default value when a locale does not exist. If string_en does not exist, this script will use the first string json file as a template');

Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
compileSdkVersion 28

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
26 changes: 13 additions & 13 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kingwu.genlangexample">
package="com.kingwu.genlangexample">

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
Expand All @@ -13,27 +13,27 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="gen_lang_example"
android:icon="@mipmap/ic_launcher">
android:label="gen_lang_example"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<meta-data
android:name="flutterEmbedding"
android:value="2"/>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package com.kingwu.genlangexample

import android.os.Bundle

import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
}
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
13 changes: 13 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/home/vader/Android/flutter"
export "FLUTTER_APPLICATION_PATH=/home/vader/Linos/Project/gen_lang/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=false"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.packages"
25 changes: 18 additions & 7 deletions example/lib/generated/i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ class S {
static const GeneratedLocalizationsDelegate delegate = GeneratedLocalizationsDelegate();

static S of(BuildContext context) {
return Localizations.of<S>(context, S);
final localization = Localizations.of<S>(context, S);

assert(() {
if (localization == null) {
throw FlutterError(
'S requested with a context that does not include S.');
}
return true;
}());

return localization!;
}

static Future<S> load(Locale locale) {
Expand Down Expand Up @@ -73,13 +83,14 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
return const <Locale>[
Locale("en", ""),
Locale("ja", ""),
Locale("es", ""),
Locale("zh", "TW"),

];
}

LocaleListResolutionCallback listResolution({Locale fallback}) {
return (List<Locale> locales, Iterable<Locale> supported) {
LocaleListResolutionCallback listResolution({Locale? fallback}) {
return (List<Locale>? locales, Iterable<Locale> supported) {
if (locales == null || locales.isEmpty) {
return fallback ?? supported.first;
} else {
Expand All @@ -88,13 +99,13 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
};
}

LocaleResolutionCallback resolution({Locale fallback}) {
return (Locale locale, Iterable<Locale> supported) {
LocaleResolutionCallback resolution({Locale? fallback}) {
return (Locale? locale, Iterable<Locale> supported) {
return _resolve(locale, fallback, supported);
};
}

Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported) {
Locale _resolve(Locale? locale, Locale? fallback, Iterable<Locale> supported) {
if (locale == null || !isSupported(locale)) {
return fallback ?? supported.first;
}
Expand All @@ -116,7 +127,7 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
}

@override
bool isSupported(Locale locale) =>
bool isSupported(Locale? locale) =>
locale != null && supportedLocales.contains(locale);

@override
Expand Down
61 changes: 40 additions & 21 deletions example/lib/generated/messages_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ class $ja extends MessageLookupByLibrary {
};
}

final _$zh_TW = $zh_TW();

class $zh_TW extends MessageLookupByLibrary {
get localeName => 'zh_TW';

final messages = {
"genderMessage" : (targetGender, name) => "${Intl.genderLogic(targetGender, male: "你好 ${name},他是男。", female: "你好 ${name},她是女。", other: "你好 ${name},他/她是男/女。")}",
"locale" : MessageLookupByLibrary.simpleMessage("中文"),
"messageWithParams" : (yourName) => "你好 ${yourName},歡迎你。",
"pluralMessage" : (howMany, interviewerName) => "${Intl.pluralLogic(howMany, zero: null, one: "你好 ${interviewerName},我沒有工作經驗。", two:null, few:null, many:null, other: "你好 ${interviewerName},我有${howMany}年工作經驗。")}",
"simpleMessage" : MessageLookupByLibrary.simpleMessage("這是簡單消息"),
"specialCharactersMessage" : MessageLookupByLibrary.simpleMessage("Special Characters Nice Developer's \"Message\"\n Next Line"),

};
}

final _$en = $en();

class $en extends MessageLookupByLibrary {
Expand All @@ -55,24 +39,59 @@ class $en extends MessageLookupByLibrary {
};
}

final _$es = $es();

class $es extends MessageLookupByLibrary {
get localeName => 'es';

final messages = {
"genderMessage" : (targetGender, name) => "${Intl.genderLogic(targetGender, male: "Hola ${name}, él es u chico.", female: "Hola ${name}, ella es una chica.", other: "Hola ${name}, el/ella es chico/chica.")}",
"locale" : MessageLookupByLibrary.simpleMessage("Spanish"),
"messageWithParams" : (yourName) => "Hola ${yourName}, bienvenido!",
"pluralMessage" : (howMany, interviewerName) => "${Intl.pluralLogic(howMany, zero: null, one: "Hola ${interviewerName}, tengo un año de experiencia laboral.", two:null, few:null, many:null, other: "Hola ${interviewerName}, Tengo ${howMany} años de experiencia laboral.")}",
"simpleMessage" : MessageLookupByLibrary.simpleMessage("Este es un mensaje sencillo"),
"specialCharactersMessage" : MessageLookupByLibrary.simpleMessage("Caracteres Especiales Buenos Desarrolladores \"Mensaje\"\n Siguiente Línea"),

};
}

final _$zh_TW = $zh_TW();

class $zh_TW extends MessageLookupByLibrary {
get localeName => 'zh_TW';

final messages = {
"genderMessage" : (targetGender, name) => "${Intl.genderLogic(targetGender, male: "你好 ${name},他是男。", female: "你好 ${name},她是女。", other: "你好 ${name},他/她是男/女。")}",
"locale" : MessageLookupByLibrary.simpleMessage("中文"),
"messageWithParams" : (yourName) => "你好 ${yourName},歡迎你。",
"pluralMessage" : (howMany, interviewerName) => "${Intl.pluralLogic(howMany, zero: null, one: "你好 ${interviewerName},我沒有工作經驗。", two:null, few:null, many:null, other: "你好 ${interviewerName},我有${howMany}年工作經驗。")}",
"simpleMessage" : MessageLookupByLibrary.simpleMessage("這是簡單消息"),
"specialCharactersMessage" : MessageLookupByLibrary.simpleMessage("Special Characters Nice Developer's \"Message\"\n Next Line"),

};
}



typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
"ja": () => Future.value(null),
"zh_TW": () => Future.value(null),
"en": () => Future.value(null),
"es": () => Future.value(null),
"zh_TW": () => Future.value(null),

};

MessageLookupByLibrary _findExact(localeName) {
MessageLookupByLibrary? _findExact(localeName) {
switch (localeName) {
case "ja":
return _$ja;
case "zh_TW":
return _$zh_TW;
case "en":
return _$en;
case "es":
return _$es;
case "zh_TW":
return _$zh_TW;

default:
return null;
Expand Down Expand Up @@ -105,7 +124,7 @@ bool _messagesExistFor(String locale) {
}
}

MessageLookupByLibrary _findGeneratedMessagesFor(locale) {
MessageLookupByLibrary? _findGeneratedMessagesFor(locale) {
var actualLocale = Intl.verifiedLocale(locale, _messagesExistFor,
onFailure: (_) => null);
if (actualLocale == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:gen_lang_example/generated/i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:gen_lang_example/generated/i18n.dart';

void main() => runApp(MyApp());

Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ description: Demonstrates how to use the gen_lang plugin.
publish_to: 'none'

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
cupertino_icons: ^1.0.2
flutter_localizations:
sdk: flutter

Expand Down
11 changes: 0 additions & 11 deletions example/res/string/_zh_CN.json

This file was deleted.

11 changes: 0 additions & 11 deletions example/res/string/string_.json

This file was deleted.

11 changes: 11 additions & 0 deletions example/res/string/string_es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"genderMessageFemale":"Hola ${name}, ella es una chica.",
"genderMessageGOther":"Hola ${name}, el/ella es chico/chica.",
"genderMessageMale":"Hola ${name}, él es u chico.",
"locale":"Spanish",
"messageWithParams":"Hola ${yourName}, bienvenido!",
"pluralMessageOne":"Hola ${interviewerName}, tengo un año de experiencia laboral.",
"pluralMessagePOther":"Hola ${interviewerName}, Tengo ${howMany} años de experiencia laboral.",
"simpleMessage":"Este es un mensaje sencillo",
"specialCharactersMessage":"Caracteres Especiales Buenos Desarrolladores \"Mensaje\"\n Siguiente Línea"
}
11 changes: 0 additions & 11 deletions example/res/string/xxxxx_zh_CN.json

This file was deleted.

2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data.startsWith('Running on:'),
widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
Loading