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 (#668)

Prepare for #667
  • Loading branch information
AlexV525 authored Dec 11, 2024
1 parent ba47c9e commit 7835de8
Show file tree
Hide file tree
Showing 2 changed files with 65 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
30 changes: 30 additions & 0 deletions test/delegates_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathWrapper<AssetPathEntity>> paths =
<PathWrapper<AssetPathEntity>>[
Expand Down

0 comments on commit 7835de8

Please sign in to comment.