diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1dc68..aefb9b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## [5.0.0] - 2022-08-23 +### Fixed +- Sorting of the default language in the supported languages + ## [4.1.2] - 2022-01-01 ### Fixed - changed language code to `toLanguageTag()` diff --git a/lib/src/extensions/list_extensions.dart b/lib/src/extensions/list_extensions.dart new file mode 100644 index 0000000..5ce8dde --- /dev/null +++ b/lib/src/extensions/list_extensions.dart @@ -0,0 +1,13 @@ +extension ListExtension on List { + List moveToFirstIndex(T item) { + for (var i = 0; i < length; ++i) { + if (this[i] == item) { + final temp = this[0]; + this[0] = this[i]; + this[i] = temp; + break; + } + } + return this; + } +} diff --git a/lib/src/locale_gen_sb_writer.dart b/lib/src/locale_gen_sb_writer.dart index f9681b2..b06d70d 100644 --- a/lib/src/locale_gen_sb_writer.dart +++ b/lib/src/locale_gen_sb_writer.dart @@ -1,4 +1,5 @@ import 'package:locale_gen/src/case_util.dart'; +import 'package:locale_gen/src/extensions/list_extensions.dart'; import 'package:locale_gen/src/locale_gen_parser.dart'; import 'locale_gen_params.dart'; @@ -160,8 +161,9 @@ class LocaleGenSbWriter { LocaleGenParser.parseDefaultLanguageLocale(params.defaultLanguage)) ..writeln() ..writeln(' static const _supportedLocales = ['); - params.languages.forEach((language) => - sb.writeln(LocaleGenParser.parseSupportedLocale(language))); + params.languages.moveToFirstIndex(params.defaultLanguage).forEach( + (language) => + sb.writeln(LocaleGenParser.parseSupportedLocale(language))); sb ..writeln(' ];') ..writeln() diff --git a/pubspec.yaml b/pubspec.yaml index e1a0e19..bce7df6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: locale_gen description: Dart tool that will convert your default locale json to dart code. -version: 4.1.2 +version: 5.0.0 homepage: https://github.com/vanlooverenkoen/locale_gen environment: diff --git a/test/extension/list_extension.dart b/test/extension/list_extension.dart new file mode 100644 index 0000000..0e5067a --- /dev/null +++ b/test/extension/list_extension.dart @@ -0,0 +1,30 @@ +import 'package:locale_gen/src/extensions/list_extensions.dart'; +import 'package:test/test.dart'; + +void main() { + group('List Extensions', () { + test('Test if move to first index is correct when not yet firsst', () { + final list = ['Test', 'blabla', 'what']; + final newList = list.moveToFirstIndex('what'); + expect(newList[0], 'what'); + expect(newList[1], 'Test'); + expect(newList[2], 'blabla'); + }); + test('Test if move to first index is correct when already first', () { + final list = ['Test', 'blabla', 'what']; + final newList = list.moveToFirstIndex('Test'); + expect(newList[0], 'Test'); + expect(newList[1], 'blabla'); + expect(newList[2], 'what'); + }); + test( + 'Test const list to move to first index is correct when already first f', + () { + const list = ['Test', 'blabla', 'what']; + final newList = list.moveToFirstIndex('Test'); + expect(newList[0], 'Test'); + expect(newList[1], 'blabla'); + expect(newList[2], 'what'); + }); + }); +} diff --git a/test/locale_gen_sb_writer_test.dart b/test/locale_gen_sb_writer_test.dart index 721602b..492f395 100644 --- a/test/locale_gen_sb_writer_test.dart +++ b/test/locale_gen_sb_writer_test.dart @@ -815,6 +815,247 @@ class LocalizationDelegate extends LocalizationsDelegate { @override bool shouldReload(LocalizationsDelegate old) => true; } +'''); + }); + test('Test createLocalizationDelegateFile with unsorted default language', + () { + final params = LocaleGenParams.fromYamlString( + 'locale_gen', '''name: locale_gen_example +locale_gen: + languages: ['nl','en'] + locale_assets_path: test/assets/locale +'''); + final result = LocaleGenSbWriter.createLocalizationDelegateFile(params); + expect(result, r'''import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:locale_gen_example/util/locale/localization.dart'; +import 'package:locale_gen_example/util/locale/localization_overrides.dart'; + +//============================================================// +//THIS FILE IS AUTO GENERATED. DO NOT EDIT// +//============================================================// + +typedef LocaleFilter = bool Function(String languageCode); + +class LocalizationDelegate extends LocalizationsDelegate { + static LocaleFilter? localeFilter; + static const defaultLocale = Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null); + + static const _supportedLocales = [ + Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null), + Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null), + ]; + + static List get supportedLanguages { + final supportedLanguageTags = _supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false); + if (localeFilter == null) return supportedLanguageTags; + return supportedLanguageTags.where((element) => localeFilter?.call(element) ?? true).toList(); + } + + static List get supportedLocales { + if (localeFilter == null) return _supportedLocales; + return _supportedLocales.where((element) => localeFilter?.call(element.toLanguageTag()) ?? true).toList(); + } + + LocalizationOverrides? localizationOverrides; + Locale? newLocale; + Locale? activeLocale; + final bool useCaching; + bool showLocalizationKeys; + + LocalizationDelegate({ + this.newLocale, + this.localizationOverrides, + this.showLocalizationKeys = false, + this.useCaching = !kDebugMode, + }) { + if (newLocale != null) { + activeLocale = newLocale; + } + } + + @override + bool isSupported(Locale locale) => supportedLanguages.contains(locale.toLanguageTag()); + + @override + Future load(Locale locale) async { + final newActiveLocale = newLocale ?? locale; + activeLocale = newActiveLocale; + return Localization.load( + newActiveLocale, + localizationOverrides: localizationOverrides, + showLocalizationKeys: showLocalizationKeys, + useCaching: useCaching, + ); + } + + @override + bool shouldReload(LocalizationsDelegate old) => true; +} +'''); + }); + test( + 'Test createLocalizationDelegateFile with sorted default language but use nl as default', + () { + final params = LocaleGenParams.fromYamlString( + 'locale_gen', '''name: locale_gen_example +locale_gen: + default_language: 'nl' + languages: ['nl','en'] + locale_assets_path: test/assets/locale +'''); + final result = LocaleGenSbWriter.createLocalizationDelegateFile(params); + expect(result, r'''import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:locale_gen_example/util/locale/localization.dart'; +import 'package:locale_gen_example/util/locale/localization_overrides.dart'; + +//============================================================// +//THIS FILE IS AUTO GENERATED. DO NOT EDIT// +//============================================================// + +typedef LocaleFilter = bool Function(String languageCode); + +class LocalizationDelegate extends LocalizationsDelegate { + static LocaleFilter? localeFilter; + static const defaultLocale = Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null); + + static const _supportedLocales = [ + Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null), + Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null), + ]; + + static List get supportedLanguages { + final supportedLanguageTags = _supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false); + if (localeFilter == null) return supportedLanguageTags; + return supportedLanguageTags.where((element) => localeFilter?.call(element) ?? true).toList(); + } + + static List get supportedLocales { + if (localeFilter == null) return _supportedLocales; + return _supportedLocales.where((element) => localeFilter?.call(element.toLanguageTag()) ?? true).toList(); + } + + LocalizationOverrides? localizationOverrides; + Locale? newLocale; + Locale? activeLocale; + final bool useCaching; + bool showLocalizationKeys; + + LocalizationDelegate({ + this.newLocale, + this.localizationOverrides, + this.showLocalizationKeys = false, + this.useCaching = !kDebugMode, + }) { + if (newLocale != null) { + activeLocale = newLocale; + } + } + + @override + bool isSupported(Locale locale) => supportedLanguages.contains(locale.toLanguageTag()); + + @override + Future load(Locale locale) async { + final newActiveLocale = newLocale ?? locale; + activeLocale = newActiveLocale; + return Localization.load( + newActiveLocale, + localizationOverrides: localizationOverrides, + showLocalizationKeys: showLocalizationKeys, + useCaching: useCaching, + ); + } + + @override + bool shouldReload(LocalizationsDelegate old) => true; +} +'''); + }); + test( + 'Test createLocalizationDelegateFile with unsorted default language but use nl as default', + () { + final params = LocaleGenParams.fromYamlString( + 'locale_gen', '''name: locale_gen_example +locale_gen: + default_language: 'nl' + languages: ['en', 'nl'] + locale_assets_path: test/assets/locale +'''); + final result = LocaleGenSbWriter.createLocalizationDelegateFile(params); + expect(result, r'''import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:locale_gen_example/util/locale/localization.dart'; +import 'package:locale_gen_example/util/locale/localization_overrides.dart'; + +//============================================================// +//THIS FILE IS AUTO GENERATED. DO NOT EDIT// +//============================================================// + +typedef LocaleFilter = bool Function(String languageCode); + +class LocalizationDelegate extends LocalizationsDelegate { + static LocaleFilter? localeFilter; + static const defaultLocale = Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null); + + static const _supportedLocales = [ + Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null), + Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null), + ]; + + static List get supportedLanguages { + final supportedLanguageTags = _supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false); + if (localeFilter == null) return supportedLanguageTags; + return supportedLanguageTags.where((element) => localeFilter?.call(element) ?? true).toList(); + } + + static List get supportedLocales { + if (localeFilter == null) return _supportedLocales; + return _supportedLocales.where((element) => localeFilter?.call(element.toLanguageTag()) ?? true).toList(); + } + + LocalizationOverrides? localizationOverrides; + Locale? newLocale; + Locale? activeLocale; + final bool useCaching; + bool showLocalizationKeys; + + LocalizationDelegate({ + this.newLocale, + this.localizationOverrides, + this.showLocalizationKeys = false, + this.useCaching = !kDebugMode, + }) { + if (newLocale != null) { + activeLocale = newLocale; + } + } + + @override + bool isSupported(Locale locale) => supportedLanguages.contains(locale.toLanguageTag()); + + @override + Future load(Locale locale) async { + final newActiveLocale = newLocale ?? locale; + activeLocale = newActiveLocale; + return Localization.load( + newActiveLocale, + localizationOverrides: localizationOverrides, + showLocalizationKeys: showLocalizationKeys, + useCaching: useCaching, + ); + } + + @override + bool shouldReload(LocalizationsDelegate old) => true; +} '''); }); test('Test createLocalizationDelegateFile with other output path', () {