35 lines
1.1 KiB
Dart
35 lines
1.1 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
||
|
|
|
||
|
|
/// Locale-aware name selector for terminology fields.
|
||
|
|
///
|
||
|
|
/// Selects the appropriate field based on user locale:
|
||
|
|
/// - zh-Hant (Traditional Chinese) → uses `_hant` suffix fields
|
||
|
|
/// - zh (Simplified Chinese) or en (English) → uses base fields
|
||
|
|
class LocaleNameSelector {
|
||
|
|
const LocaleNameSelector._(this._isHant);
|
||
|
|
|
||
|
|
final bool _isHant;
|
||
|
|
|
||
|
|
/// Creates a selector from the given [locale].
|
||
|
|
factory LocaleNameSelector.fromLocale(Locale locale) {
|
||
|
|
final isHant = locale.scriptCode == 'Hant';
|
||
|
|
return LocaleNameSelector._(isHant);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Creates a selector from the given [context].
|
||
|
|
factory LocaleNameSelector.of(BuildContext context) {
|
||
|
|
final locale = Localizations.localeOf(context);
|
||
|
|
return LocaleNameSelector.fromLocale(locale);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Selects between simplified and traditional Chinese name.
|
||
|
|
///
|
||
|
|
/// Returns [nameHant] for zh-Hant locale, [name] otherwise.
|
||
|
|
String name(String name, String nameHant) {
|
||
|
|
return _isHant ? nameHant : name;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Returns true if the current locale uses Traditional Chinese.
|
||
|
|
bool get isTraditionalChinese => _isHant;
|
||
|
|
}
|