123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import 'dart:ui';
- import 'package:fis_common/helpers/string.dart';
- class _TrStrInner {
- _TrStrInner(String value) {
- _value = value;
- }
- late final String _value;
- /// 值
- String get value => _value;
- @override
- int get hashCode => _value.hashCode;
- @override
- bool operator ==(dynamic other) {
- if (other != null && other is _TrStrInner) return value == other.value;
- return false;
- }
- @override
- String toString() => value;
- }
- /// Fis翻译字典值
- class FTrStr extends _TrStrInner implements Comparable<String>, Pattern {
- /// Fis翻译字典值
- ///
- /// [value] 翻译值
- FTrStr(String value) : super(value);
- @override
- Iterable<Match> allMatches(String string, [int start = 0]) {
- return value.allMatches(string, start);
- }
- @override
- Match? matchAsPrefix(String string, [int start = 0]) {
- return value.matchAsPrefix(string, start);
- }
- @override
- int compareTo(dynamic other) {
- return value.compareTo(other);
- }
- }
- extension FTrStrExtension on FTrStr {
- /// 翻译值
- String get t => this.value;
- /// 翻译
- ///
- /// [args] 参数集合
- String translate([List<dynamic>? args]) =>
- FStringHelper.formart(this.value, args);
- }
- abstract class ModuleBase implements IPropQueryable {}
- /// 属性可查询
- abstract class IPropQueryable {
- /// 根据字段名获取
- dynamic getProp(String propName);
- }
- class StringMapUtils {
- /// 剥取翻译值
- ///
- /// [key] 字典键
- ///
- /// [defaultValue] 默认翻译
- static FTrStr pickTrStr(Map<String, dynamic> map, String key,
- [String? defaultValue]) {
- dynamic value = map[key];
- if (value == null) value = defaultValue;
- if (value == null)
- throw UnimplementedError(
- 'translation keybook is not complete: Key - $key in some module.');
- return FTrStr(value);
- }
- }
- extension FTrMapPickExtension on Map<String, dynamic> {
- /// 剥取翻译值
- ///
- /// [key] 字典键
- ///
- /// [defaultValue] 默认翻译
- FTrStr pick(String key, [String? defaultValue]) =>
- StringMapUtils.pickTrStr(this, key, defaultValue);
- }
- extension LocaleExtensions on Locale {
- String toCodeString([String separator = '_']) =>
- '${this.languageCode}$separator${this.countryCode ?? ""}';
- }
- extension StringLocaleExtensions on String {
- Locale? toLocale([String separator = '_']) {
- if (this.length < 2) return null;
- final splited = this.split(separator);
- return Locale(splited[0], splited.length > 1 ? splited[1] : null);
- }
- }
|