diff --git a/lib/src/delegates/asset_picker_text_delegate.dart b/lib/src/delegates/asset_picker_text_delegate.dart index b79a8c3c..2a85464b 100644 --- a/lib/src/delegates/asset_picker_text_delegate.dart +++ b/lib/src/delegates/asset_picker_text_delegate.dart @@ -4,12 +4,12 @@ import 'dart:io' show Platform; +import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:photo_manager/photo_manager.dart' show AssetType; /// All text delegates. -const List assetPickerTextDelegates = - [ +const assetPickerTextDelegates = [ AssetPickerTextDelegate(), EnglishAssetPickerTextDelegate(), HebrewAssetPickerTextDelegate(), @@ -28,13 +28,30 @@ AssetPickerTextDelegate assetPickerTextDelegateFromLocale(Locale? locale) { if (locale == null) { return const AssetPickerTextDelegate(); } + final String languageCode = locale.languageCode.toLowerCase(); - for (final AssetPickerTextDelegate delegate in assetPickerTextDelegates) { - if (delegate.languageCode == languageCode) { - return delegate; - } + final String? scriptCode = locale.scriptCode?.toLowerCase(); + final String? countryCode = locale.countryCode?.toLowerCase(); + + final matchedByLanguage = assetPickerTextDelegates.where( + (e) => e.languageCode == languageCode, + ); + if (matchedByLanguage.isEmpty) { + return const AssetPickerTextDelegate(); + } + + final matchedByScript = scriptCode != null + ? matchedByLanguage.where((e) => e.scriptCode == scriptCode) + : null; + if (matchedByScript == null || matchedByScript.isEmpty) { + return matchedByLanguage.first; } - return const AssetPickerTextDelegate(); + + final matchedByCountry = countryCode != null + ? matchedByScript.where((e) => e.countryCode == countryCode) + : null; + + return matchedByCountry?.firstOrNull ?? matchedByScript.first; } /// Text delegate that controls text in widgets. @@ -44,6 +61,17 @@ class AssetPickerTextDelegate { String get languageCode => 'zh'; + String? get scriptCode => 'Hans'; + + String? get countryCode => null; + + @nonVirtual + Locale get locale => Locale.fromSubtags( + languageCode: languageCode, + scriptCode: scriptCode, + countryCode: countryCode, + ); + /// Confirm string for the confirm button. /// 确认按钮的字段 String get confirm => '确认'; diff --git a/test/delegates_test.dart b/test/delegates_test.dart index 2698964b..6ae1386a 100644 --- a/test/delegates_test.dart +++ b/test/delegates_test.dart @@ -2,10 +2,40 @@ // Use of this source code is governed by an Apache license that can be found // in the LICENSE file. +import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:wechat_assets_picker/wechat_assets_picker.dart'; void main() { + group(AssetPickerTextDelegate, () { + test('returns the default when available', () { + expect( + assetPickerTextDelegateFromLocale(null), + equals(const AssetPickerTextDelegate()), + ); + expect( + assetPickerTextDelegateFromLocale(const Locale('zh')), + equals(const AssetPickerTextDelegate()), + ); + expect( + assetPickerTextDelegateFromLocale(const Locale('zxx')), + equals(const AssetPickerTextDelegate()), + ); + }); + + test('each delegate can be obtained by its locale definition', () { + for (final delegate in assetPickerTextDelegates) { + final locale = Locale.fromSubtags( + languageCode: delegate.languageCode, + scriptCode: delegate.scriptCode, + countryCode: delegate.countryCode, + ); + final matchedDelegate = assetPickerTextDelegateFromLocale(locale); + expect(matchedDelegate, equals(delegate)); + } + }); + }); + test('Sort paths correctly', () { final List> paths = >[