Skip to content

Commit

Permalink
🚀 Support matching script and country code of locales with the text d…
Browse files Browse the repository at this point in the history
…elegate
  • Loading branch information
AlexV525 committed Dec 11, 2024
1 parent ba47c9e commit a6fb683
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/src/delegates/asset_picker_text_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetPickerTextDelegate> assetPickerTextDelegates =
<AssetPickerTextDelegate>[
const assetPickerTextDelegates = <AssetPickerTextDelegate>[
AssetPickerTextDelegate(),
EnglishAssetPickerTextDelegate(),
HebrewAssetPickerTextDelegate(),
Expand All @@ -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.
Expand All @@ -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 => '确认';
Expand Down

0 comments on commit a6fb683

Please sign in to comment.