From a6fb68394e8a8003debcfb6cf570057eac8825ee Mon Sep 17 00:00:00 2001 From: Alex Li Date: Wed, 11 Dec 2024 13:43:52 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Support=20matching=20script=20an?= =?UTF-8?q?d=20country=20code=20of=20locales=20with=20the=20text=20delegat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../delegates/asset_picker_text_delegate.dart | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) 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 => '确认';