From 29e68501a540399f32d65824a4dc53b3d981ae1d Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 4 Mar 2021 20:02:18 +0100 Subject: [PATCH 1/3] #32 wrote the full implmentation for the nullsafe release --- CHANGELOG.md | 6 +++ example/ios/Runner.xcodeproj/project.pbxproj | 19 --------- .../contents.xcworkspacedata | 2 +- example/lib/app.dart | 4 +- example/lib/di/injector.dart | 19 --------- example/lib/di/injector.g.dart | 19 --------- example/lib/main.dart | 2 - example/lib/repository/locale_repository.dart | 9 ++-- example/lib/screen/home_screen.dart | 6 +-- example/lib/util/locale/localization.dart | 28 +++++-------- .../util/locale/localization_delegate.dart | 15 ++++--- .../lib/util/locale/localization_keys.dart | 2 + .../viewmodel/locale/locale_viewmodel.dart | 2 +- example/pubspec.yaml | 14 ++----- lib/src/case_util.dart | 2 +- lib/src/locale_gen_params.dart | 41 +++++++++---------- lib/src/locale_gen_writer.dart | 20 +++------ lib/src/translation_writer.dart | 8 ++-- pubspec.yaml | 11 +++-- 19 files changed, 77 insertions(+), 152 deletions(-) delete mode 100644 example/lib/di/injector.dart delete mode 100644 example/lib/di/injector.g.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a6dd0..870ec91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [3.0.0] - 2021-03-07 +### Added +-#32 Nullsafety implementation. Support for flutter 2.0 & dart 2.12.0 +### Removed +-#32 nullsafe flag is removed because from this version we are targeting 2.12 which will use nullsafety by default + ## [2.1.0] - 2021-02-09 ### -Added option to generate null safety compatible code diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index caa2931..e619b06 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -149,7 +149,6 @@ 97C146EC1CF9000F007C117D /* Resources */, 9705A1C41CF9048500538489 /* Embed Frameworks */, 3B06AD1E1E4923F5004D2608 /* Thin Binary */, - F48304C09168E6E040649494 /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -258,24 +257,6 @@ shellPath = /bin/sh; shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; }; - F48304C09168E6E040649494 /* [CP] Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", - "${PODS_ROOT}/../Flutter/Flutter.framework", - ); - name = "[CP] Embed Pods Frameworks"; - outputPaths = ( - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework", - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 1d526a1..919434a 100644 --- a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/example/lib/app.dart b/example/lib/app.dart index ad1e7d4..fbfdec0 100644 --- a/example/lib/app.dart +++ b/example/lib/app.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; -import 'package:kiwi/kiwi.dart' as kiwi; +import 'package:locale_gen_example/repository/locale_repository.dart'; import 'package:locale_gen_example/screen/home_screen.dart'; import 'package:locale_gen_example/util/locale/localization_delegate.dart'; import 'package:locale_gen_example/viewmodel/locale/locale_viewmodel.dart'; @@ -26,7 +26,7 @@ class MyApp extends StatelessWidget { home: HomeScreen(), ), ), - create: (context) => kiwi.Container().resolve()..init(), + create: (context) => LocaleViewModel(LocaleRepository())..init(), ); } } diff --git a/example/lib/di/injector.dart b/example/lib/di/injector.dart deleted file mode 100644 index d67b1ff..0000000 --- a/example/lib/di/injector.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:kiwi/kiwi.dart'; -import 'package:locale_gen_example/repository/locale_repository.dart'; -import 'package:locale_gen_example/viewmodel/locale/locale_viewmodel.dart'; - -part 'injector.g.dart'; - -abstract class Injector { - @Register.singleton(LocaleRepository) - void registerCommonDependencies(); - - @Register.factory(LocaleViewModel) - void registerViewModelFactories(); -} - -void setupDependencyTree() { - _$Injector() - ..registerCommonDependencies() - ..registerViewModelFactories(); -} diff --git a/example/lib/di/injector.g.dart b/example/lib/di/injector.g.dart deleted file mode 100644 index f6a2c95..0000000 --- a/example/lib/di/injector.g.dart +++ /dev/null @@ -1,19 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'injector.dart'; - -// ************************************************************************** -// InjectorGenerator -// ************************************************************************** - -class _$Injector extends Injector { - void registerCommonDependencies() { - final Container container = Container(); - container.registerSingleton((c) => LocaleRepository()); - } - - void registerViewModelFactories() { - final Container container = Container(); - container.registerFactory((c) => LocaleViewModel(c())); - } -} diff --git a/example/lib/main.dart b/example/lib/main.dart index bdfd7d5..43c6808 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; import 'package:locale_gen_example/app.dart'; -import 'package:locale_gen_example/di/injector.dart' as kiwi; Future main() async { - kiwi.setupDependencyTree(); runApp(MyApp()); } diff --git a/example/lib/repository/locale_repository.dart b/example/lib/repository/locale_repository.dart index c0e0d34..d220f3a 100644 --- a/example/lib/repository/locale_repository.dart +++ b/example/lib/repository/locale_repository.dart @@ -3,10 +3,13 @@ import 'package:shared_preferences/shared_preferences.dart'; class LocaleRepository { static const STORE_LOCALE = 'locale'; + static LocaleRepository? _instance; - LocaleRepository(); + LocaleRepository._(); - Future setCustomLocale(Locale locale) async { + factory LocaleRepository() => _instance ??= LocaleRepository._(); + + Future setCustomLocale(Locale? locale) async { final prefs = await SharedPreferences.getInstance(); if (locale == null) { print('Reset custom locale. Use system language'); @@ -17,7 +20,7 @@ class LocaleRepository { } //can be null - Future getCustomLocale() async { + Future getCustomLocale() async { final prefs = await SharedPreferences.getInstance(); final localeCode = prefs.getString(STORE_LOCALE); if (localeCode == null || localeCode.isEmpty) return null; diff --git a/example/lib/screen/home_screen.dart b/example/lib/screen/home_screen.dart index 95dd8c4..3f4b491 100644 --- a/example/lib/screen/home_screen.dart +++ b/example/lib/screen/home_screen.dart @@ -14,17 +14,17 @@ class HomeScreen extends StatelessWidget { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - FlatButton( + MaterialButton( child: const Text('System Language (Not translated)'), onPressed: Provider.of(context) .onSwitchToSystemLanguage, ), - FlatButton( + MaterialButton( child: const Text('English (Not translated)'), onPressed: Provider.of(context).onSwitchToEnglish, ), - FlatButton( + MaterialButton( child: const Text('Nederlands (Not translated)'), onPressed: Provider.of(context).onSwitchToDutch, ), diff --git a/example/lib/util/locale/localization.dart b/example/lib/util/locale/localization.dart index cf535f2..305079a 100644 --- a/example/lib/util/locale/localization.dart +++ b/example/lib/util/locale/localization.dart @@ -10,32 +10,26 @@ import 'package:locale_gen_example/util/locale/localization_keys.dart'; class Localization { Map _localisedValues = Map(); - static Localization of(BuildContext context) => - Localizations.of(context, Localization); + static Localization of(BuildContext context) => Localizations.of(context, Localization)!; - static Future load(Locale locale, - {bool showLocalizationKeys = false}) async { + static Future load(Locale locale, {bool showLocalizationKeys = false}) async { final localizations = Localization(); if (showLocalizationKeys) { return localizations; } - final jsonContent = await rootBundle - .loadString('assets/locale/${locale.languageCode}.json'); + final jsonContent = await rootBundle.loadString('assets/locale/${locale.languageCode}.json'); // ignore: avoid_as - localizations._localisedValues = - json.decode(jsonContent) as Map; + localizations._localisedValues = json.decode(jsonContent) as Map; return localizations; } - String _t(String key, {List args}) { + String _t(String key, {List? args}) { try { // ignore: avoid_as var value = _localisedValues[key] as String; if (value == null) return '$key'; if (args == null || args.isEmpty) return value; - args - .asMap() - .forEach((index, arg) => value = _replaceWith(value, arg, index + 1)); + args.asMap().forEach((index, arg) => value = _replaceWith(value, arg, index + 1)); return value; } catch (e) { return '⚠$key⚠'; @@ -58,12 +52,10 @@ class Localization { String testArg2(num arg1) => _t(LocalizationKeys.testArg2, args: [arg1]); - String testArg3(String arg1, num arg2) => - _t(LocalizationKeys.testArg3, args: [arg1, arg2]); + String testArg3(String arg1, num arg2) => _t(LocalizationKeys.testArg3, args: [arg1, arg2]); - String testArg4(String arg1, num arg2) => - _t(LocalizationKeys.testArg4, args: [arg1, arg2]); + String testArg4(String arg1, num arg2) => _t(LocalizationKeys.testArg4, args: [arg1, arg2]); + + String getTranslation(String key, {List? args}) => _t(key, args: args ?? []); - String getTranslation(String key, {List args}) => - _t(key, args: args ?? List()); } diff --git a/example/lib/util/locale/localization_delegate.dart b/example/lib/util/locale/localization_delegate.dart index 4f11cdc..847110d 100644 --- a/example/lib/util/locale/localization_delegate.dart +++ b/example/lib/util/locale/localization_delegate.dart @@ -18,28 +18,27 @@ class LocalizationDelegate extends LocalizationsDelegate { Locale('nl'), ]; - Locale newLocale; - Locale activeLocale; + Locale? newLocale; + Locale? activeLocale; bool showLocalizationKeys; LocalizationDelegate({this.newLocale, this.showLocalizationKeys = false}) { if (newLocale != null) { activeLocale = newLocale; } - showLocalizationKeys ??= false; } @override - bool isSupported(Locale locale) => - supportedLanguages.contains(locale.languageCode); + bool isSupported(Locale locale) => supportedLanguages.contains(locale.languageCode); @override Future load(Locale locale) async { - activeLocale = newLocale ?? locale; - return Localization.load(activeLocale, - showLocalizationKeys: showLocalizationKeys); + final newActiveLocale = newLocale ?? locale; + activeLocale = newActiveLocale; + return Localization.load(newActiveLocale, showLocalizationKeys: showLocalizationKeys); } @override bool shouldReload(LocalizationsDelegate old) => true; + } diff --git a/example/lib/util/locale/localization_keys.dart b/example/lib/util/locale/localization_keys.dart index 5205cfc..77c9eca 100644 --- a/example/lib/util/locale/localization_keys.dart +++ b/example/lib/util/locale/localization_keys.dart @@ -2,6 +2,7 @@ //THIS FILE IS AUTO GENERATED. DO NOT EDIT// //============================================================// class LocalizationKeys { + static const test = 'test'; static const testArg1 = 'test_arg1'; @@ -11,4 +12,5 @@ class LocalizationKeys { static const testArg3 = 'test_arg3'; static const testArg4 = 'test_arg4'; + } diff --git a/example/lib/viewmodel/locale/locale_viewmodel.dart b/example/lib/viewmodel/locale/locale_viewmodel.dart index 99e17b6..9307c48 100644 --- a/example/lib/viewmodel/locale/locale_viewmodel.dart +++ b/example/lib/viewmodel/locale/locale_viewmodel.dart @@ -32,7 +32,7 @@ class LocaleViewModel with ChangeNotifier { await _onUpdateLocaleClicked(null); } - Future _onUpdateLocaleClicked(Locale locale) async { + Future _onUpdateLocaleClicked(Locale? locale) async { await _localeRepository.setCustomLocale(locale); localeDelegate = LocalizationDelegate(newLocale: locale); notifyListeners(); diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 414d83e..7c33f7b 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,27 +3,19 @@ description: A project used to demo the Todo Reporter version: 1.0.0+1 environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter - provider: ^3.0.0+1 - kiwi: ^0.1.0 - shared_preferences: ^0.5.3+1 + provider: ^5.0.0 + shared_preferences: ^2.0.3 dev_dependencies: flutter_test: sdk: flutter - build_runner: ^1.3.1 - built_value_generator: ^6.4.0 - kiwi_generator: - git: - url: https://github.com/simolus3/kiwi.git - ref: 6ead6e68b535bf935df37b29f7d25b283337225e - path: kiwi_generator locale_gen: path: ../ diff --git a/lib/src/case_util.dart b/lib/src/case_util.dart index be7a972..85d6c07 100644 --- a/lib/src/case_util.dart +++ b/lib/src/case_util.dart @@ -14,7 +14,7 @@ class CaseUtil { static List _groupIntoWords(String text) { final sb = StringBuffer(); - final words = List(); + final words = []; final isAllCaps = !text.contains(RegExp('[a-z]')); for (var i = 0; i < text.length; i++) { diff --git a/lib/src/locale_gen_params.dart b/lib/src/locale_gen_params.dart index 4b77df9..597c725 100644 --- a/lib/src/locale_gen_params.dart +++ b/lib/src/locale_gen_params.dart @@ -10,32 +10,30 @@ final defaultLocaleAssetsDir = join('assets', 'locale'); class LocaleGenParams { final String programName; - String outputDir = defaultOutputDir; + String? outputDir = defaultOutputDir; String assetsDir = defaultAssetsDir; String localeAssetsDir = defaultLocaleAssetsDir; - bool nullSafe = false; - String projectName; - String defaultLanguage; - List languages; + late String projectName; + late String defaultLanguage; + late List languages; LocaleGenParams(this.programName) { final pubspecYaml = File(join(Directory.current.path, 'pubspec.yaml')); if (!pubspecYaml.existsSync()) { - throw Exception( - 'This program should be run from the root of a flutter/dart project'); + throw Exception('This program should be run from the root of a flutter/dart project'); } final pubspecContent = pubspecYaml.readAsStringSync(); final doc = loadYaml(pubspecContent); - projectName = doc['name']; + final projectName = doc['name']; if (projectName == null || projectName.isEmpty) { - throw Exception( - 'Could not parse the pubspec.yaml, project name not found'); + throw Exception('Could not parse the pubspec.yaml, project name not found'); } + this.projectName = projectName; final config = doc[programName]; if (config == null) { languages = ['en']; @@ -47,23 +45,21 @@ class LocaleGenParams { @mustCallSuper void configure(YamlMap config) { - final YamlList yamlList = config['languages']; + final YamlList? yamlList = config['languages']; if (yamlList == null || yamlList.isEmpty) { - throw Exception( - "At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" + throw Exception("At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" '$programName\n' " languages: ['en']"); } - languages = yamlList.map((item) => item.toString()).toList(); - if (languages == null || languages.isEmpty) { - throw Exception( - "At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" + final languages = yamlList.map((item) => item.toString()).toList(); + if (languages.isEmpty) { + throw Exception("At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" '$programName\n' " languages: ['en']"); } - defaultLanguage = config['default_language']; + var defaultLanguage = config['default_language']; if (defaultLanguage == null) { if (languages.contains('en')) { defaultLanguage = 'en'; @@ -78,17 +74,20 @@ class LocaleGenParams { outputDir ??= defaultOutputDir; - assetsDir = config['assets_path']; + var assetsDir = config['assets_path']; assetsDir ??= defaultAssetsDir; if (!assetsDir.endsWith('/')) { assetsDir += '/'; } - localeAssetsDir = config['locale_assets_path']; + var localeAssetsDir = config['locale_assets_path']; localeAssetsDir ??= defaultLocaleAssetsDir; if (!localeAssetsDir.endsWith('/')) { localeAssetsDir += '/'; } - nullSafe = config['nullsafety'] == true; + this.localeAssetsDir = localeAssetsDir; + this.assetsDir = assetsDir; + this.languages = languages; + this.defaultLanguage = defaultLanguage; } } diff --git a/lib/src/locale_gen_writer.dart b/lib/src/locale_gen_writer.dart index 3a6c62b..b3e4ad7 100644 --- a/lib/src/locale_gen_writer.dart +++ b/lib/src/locale_gen_writer.dart @@ -58,8 +58,6 @@ class LocaleGenWriter { static void _createLocalizationFile( LocaleGenParams params, Map translations) { - final nullableInfix = params.nullSafe ? '?' : ''; - final forceNonNullPostfix = params.nullSafe ? '!' : ''; final sb = StringBuffer() ..writeln("import 'dart:convert';") ..writeln() @@ -77,7 +75,7 @@ class LocaleGenWriter { ..writeln(' Map _localisedValues = Map();') ..writeln() ..writeln( - ' static Localization of(BuildContext context) => Localizations.of(context, Localization)$forceNonNullPostfix;') + ' static Localization of(BuildContext context) => Localizations.of(context, Localization)!;') ..writeln() ..writeln( ' static Future load(Locale locale, {bool showLocalizationKeys = false}) async {') @@ -93,7 +91,7 @@ class LocaleGenWriter { ..writeln(' return localizations;') ..writeln(' }') ..writeln() - ..writeln(' String _t(String key, {List$nullableInfix args}) {') + ..writeln(' String _t(String key, {List? args}) {') ..writeln(' try {') ..writeln(' // ignore: avoid_as') ..writeln(' var value = _localisedValues[key] as String;') @@ -121,7 +119,7 @@ class LocaleGenWriter { TranslationWriter.buildTranslationFunction(sb, key, value)); sb ..writeln( - ' String getTranslation(String key, {List$nullableInfix args}) => _t(key, args: args ?? []);') + ' String getTranslation(String key, {List? args}) => _t(key, args: args ?? []);') ..writeln() ..writeln('}'); @@ -137,8 +135,6 @@ class LocaleGenWriter { } static void _createLocalizationDelegateFile(LocaleGenParams params) { - final nullableFieldInfix = params.nullSafe ? '?' : ''; - final sb = StringBuffer() ..writeln("import 'dart:async';") ..writeln() @@ -166,19 +162,15 @@ class LocaleGenWriter { sb ..writeln(' ];') ..writeln() - ..writeln(' Locale$nullableFieldInfix newLocale;') - ..writeln(' Locale$nullableFieldInfix activeLocale;') + ..writeln(' Locale? newLocale;') + ..writeln(' Locale? activeLocale;') ..writeln(' bool showLocalizationKeys;') ..writeln() ..writeln( ' LocalizationDelegate({this.newLocale, this.showLocalizationKeys = false}) {') ..writeln(' if (newLocale != null) {') ..writeln(' activeLocale = newLocale;') - ..writeln(' }'); - if (!params.nullSafe) { - sb.writeln(' showLocalizationKeys ??= false;'); - } - sb + ..writeln(' }') ..writeln(' }') ..writeln() ..writeln(' @override') diff --git a/lib/src/translation_writer.dart b/lib/src/translation_writer.dart index 94f51b4..c945011 100644 --- a/lib/src/translation_writer.dart +++ b/lib/src/translation_writer.dart @@ -6,13 +6,13 @@ class TranslationWriter { static const REGEX_TYPE_GROUP_INDEX = 2; static void buildTranslationFunction( - StringBuffer sb, String key, String value) { + StringBuffer sb, String key, String? value) { if (value == null || value.isEmpty) { _buildDefaultFunction(sb, key, value); return; } final allMatched = formatRegex.allMatches(value); - if (allMatched == null || allMatched.isEmpty) { + if (allMatched.isEmpty) { _buildDefaultFunction(sb, key, value); return; } @@ -20,7 +20,7 @@ class TranslationWriter { final camelKey = CaseUtil.getCamelcase(key); final tmpSb = StringBuffer(' String $camelKey('); - final validMatcher = List(); + final validMatcher = []; allMatched.forEach((match) { final sameTypeMatch = validMatcher.where((validMatch) => validMatch.group(REGEX_INDEX_GROUP_INDEX) == @@ -72,7 +72,7 @@ class TranslationWriter { 'Unsupported argument type for $key. Supported types are -> s,d. Create a github ticket for support -> https://github.com/icapps/flutter-icapps-translations/issues'); } - static void _buildDefaultFunction(StringBuffer sb, String key, String value) { + static void _buildDefaultFunction(StringBuffer sb, String key, String? value) { final camelCaseKey = CaseUtil.getCamelcase(key); sb ..writeln( diff --git a/pubspec.yaml b/pubspec.yaml index c4af17e..e6548a9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,17 +1,16 @@ name: locale_gen description: Dart tool that will convert your default locale json to dart code. -version: 2.1.0 +version: 3.0.0 homepage: https://github.com/vanlooverenkoen/locale_gen environment: - sdk: ">=2.0.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: - path: ^1.7.0 - yaml: ^2.2.1 - meta: ^1.2.3 + path: ^1.8.0 + yaml: ^3.1.0 dev_dependencies: flutter_test: sdk: flutter - test: ^1.15.4 + test: ^1.16.5 From b962d2b2657acbf1f18ee866d06f50505b71b4ec Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 4 Mar 2021 20:08:01 +0100 Subject: [PATCH 2/3] Fixed formatting --- example/lib/util/locale/localization.dart | 26 ++++++++++++------- .../util/locale/localization_delegate.dart | 7 ++--- .../lib/util/locale/localization_keys.dart | 2 -- lib/src/locale_gen_params.dart | 12 ++++++--- lib/src/translation_writer.dart | 3 ++- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/example/lib/util/locale/localization.dart b/example/lib/util/locale/localization.dart index 305079a..1232423 100644 --- a/example/lib/util/locale/localization.dart +++ b/example/lib/util/locale/localization.dart @@ -10,16 +10,20 @@ import 'package:locale_gen_example/util/locale/localization_keys.dart'; class Localization { Map _localisedValues = Map(); - static Localization of(BuildContext context) => Localizations.of(context, Localization)!; + static Localization of(BuildContext context) => + Localizations.of(context, Localization)!; - static Future load(Locale locale, {bool showLocalizationKeys = false}) async { + static Future load(Locale locale, + {bool showLocalizationKeys = false}) async { final localizations = Localization(); if (showLocalizationKeys) { return localizations; } - final jsonContent = await rootBundle.loadString('assets/locale/${locale.languageCode}.json'); + final jsonContent = await rootBundle + .loadString('assets/locale/${locale.languageCode}.json'); // ignore: avoid_as - localizations._localisedValues = json.decode(jsonContent) as Map; + localizations._localisedValues = + json.decode(jsonContent) as Map; return localizations; } @@ -29,7 +33,9 @@ class Localization { var value = _localisedValues[key] as String; if (value == null) return '$key'; if (args == null || args.isEmpty) return value; - args.asMap().forEach((index, arg) => value = _replaceWith(value, arg, index + 1)); + args + .asMap() + .forEach((index, arg) => value = _replaceWith(value, arg, index + 1)); return value; } catch (e) { return '⚠$key⚠'; @@ -52,10 +58,12 @@ class Localization { String testArg2(num arg1) => _t(LocalizationKeys.testArg2, args: [arg1]); - String testArg3(String arg1, num arg2) => _t(LocalizationKeys.testArg3, args: [arg1, arg2]); + String testArg3(String arg1, num arg2) => + _t(LocalizationKeys.testArg3, args: [arg1, arg2]); - String testArg4(String arg1, num arg2) => _t(LocalizationKeys.testArg4, args: [arg1, arg2]); - - String getTranslation(String key, {List? args}) => _t(key, args: args ?? []); + String testArg4(String arg1, num arg2) => + _t(LocalizationKeys.testArg4, args: [arg1, arg2]); + String getTranslation(String key, {List? args}) => + _t(key, args: args ?? []); } diff --git a/example/lib/util/locale/localization_delegate.dart b/example/lib/util/locale/localization_delegate.dart index 847110d..478ff6e 100644 --- a/example/lib/util/locale/localization_delegate.dart +++ b/example/lib/util/locale/localization_delegate.dart @@ -29,16 +29,17 @@ class LocalizationDelegate extends LocalizationsDelegate { } @override - bool isSupported(Locale locale) => supportedLanguages.contains(locale.languageCode); + bool isSupported(Locale locale) => + supportedLanguages.contains(locale.languageCode); @override Future load(Locale locale) async { final newActiveLocale = newLocale ?? locale; activeLocale = newActiveLocale; - return Localization.load(newActiveLocale, showLocalizationKeys: showLocalizationKeys); + return Localization.load(newActiveLocale, + showLocalizationKeys: showLocalizationKeys); } @override bool shouldReload(LocalizationsDelegate old) => true; - } diff --git a/example/lib/util/locale/localization_keys.dart b/example/lib/util/locale/localization_keys.dart index 77c9eca..5205cfc 100644 --- a/example/lib/util/locale/localization_keys.dart +++ b/example/lib/util/locale/localization_keys.dart @@ -2,7 +2,6 @@ //THIS FILE IS AUTO GENERATED. DO NOT EDIT// //============================================================// class LocalizationKeys { - static const test = 'test'; static const testArg1 = 'test_arg1'; @@ -12,5 +11,4 @@ class LocalizationKeys { static const testArg3 = 'test_arg3'; static const testArg4 = 'test_arg4'; - } diff --git a/lib/src/locale_gen_params.dart b/lib/src/locale_gen_params.dart index 597c725..5a27c53 100644 --- a/lib/src/locale_gen_params.dart +++ b/lib/src/locale_gen_params.dart @@ -21,7 +21,8 @@ class LocaleGenParams { LocaleGenParams(this.programName) { final pubspecYaml = File(join(Directory.current.path, 'pubspec.yaml')); if (!pubspecYaml.existsSync()) { - throw Exception('This program should be run from the root of a flutter/dart project'); + throw Exception( + 'This program should be run from the root of a flutter/dart project'); } final pubspecContent = pubspecYaml.readAsStringSync(); @@ -30,7 +31,8 @@ class LocaleGenParams { final projectName = doc['name']; if (projectName == null || projectName.isEmpty) { - throw Exception('Could not parse the pubspec.yaml, project name not found'); + throw Exception( + 'Could not parse the pubspec.yaml, project name not found'); } this.projectName = projectName; @@ -47,14 +49,16 @@ class LocaleGenParams { void configure(YamlMap config) { final YamlList? yamlList = config['languages']; if (yamlList == null || yamlList.isEmpty) { - throw Exception("At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" + throw Exception( + "At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" '$programName\n' " languages: ['en']"); } final languages = yamlList.map((item) => item.toString()).toList(); if (languages.isEmpty) { - throw Exception("At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" + throw Exception( + "At least 1 language should be added to the 'languages' section in the pubspec.yaml\n" '$programName\n' " languages: ['en']"); } diff --git a/lib/src/translation_writer.dart b/lib/src/translation_writer.dart index c945011..ca04918 100644 --- a/lib/src/translation_writer.dart +++ b/lib/src/translation_writer.dart @@ -72,7 +72,8 @@ class TranslationWriter { 'Unsupported argument type for $key. Supported types are -> s,d. Create a github ticket for support -> https://github.com/icapps/flutter-icapps-translations/issues'); } - static void _buildDefaultFunction(StringBuffer sb, String key, String? value) { + static void _buildDefaultFunction( + StringBuffer sb, String key, String? value) { final camelCaseKey = CaseUtil.getCamelcase(key); sb ..writeln( From 298615251bd4c2989c2eebfe99a88c6c67e82dab Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 4 Mar 2021 20:25:13 +0100 Subject: [PATCH 3/3] Added meta because of @mustCallSuper --- pubspec.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pubspec.yaml b/pubspec.yaml index e6548a9..6a61c8f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,7 @@ environment: sdk: ">=2.12.0 <3.0.0" dependencies: + meta: ^1.3.0 path: ^1.8.0 yaml: ^3.1.0