Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Support matching script and country code of locales with the text delegate #668

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading