75 lines
2.0 KiB
Dart
75 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../services/translation.dart';
|
|
|
|
Locale localeFromTag(String tag) {
|
|
final parts = tag.replaceAll('_', '-').split('-');
|
|
final script = parts.skip(1).where((p) => p.length == 4).firstOrNull;
|
|
final region = parts
|
|
.skip(1)
|
|
.where((p) => p.length == 2 || p.length == 3)
|
|
.firstOrNull;
|
|
return Locale.fromSubtags(
|
|
languageCode: parts.first.toLowerCase(),
|
|
scriptCode: script == null
|
|
? null
|
|
: '${script[0].toUpperCase()}${script.substring(1).toLowerCase()}',
|
|
countryCode: region?.toUpperCase(),
|
|
);
|
|
}
|
|
|
|
class CopyScope extends InheritedWidget {
|
|
CopyScope({
|
|
super.key,
|
|
required this.catalog,
|
|
required this.language,
|
|
this.overrides = const {},
|
|
required super.child,
|
|
}) : translator = catalog.translator(language, overrides: overrides);
|
|
final TranslationCatalog catalog;
|
|
final MessageTranslator translator;
|
|
final String language;
|
|
final Map<String, String> overrides;
|
|
static String text(BuildContext context, String source) {
|
|
final scope = context.dependOnInheritedWidgetOfExactType<CopyScope>();
|
|
return scope?.translator.text(source) ?? source;
|
|
}
|
|
|
|
@override
|
|
bool updateShouldNotify(CopyScope old) =>
|
|
old.language != language ||
|
|
old.catalog != catalog ||
|
|
old.overrides != overrides;
|
|
}
|
|
|
|
class CopyText extends StatelessWidget {
|
|
const CopyText(this.data, {super.key, this.style, this.textAlign});
|
|
final String data;
|
|
final TextStyle? style;
|
|
final TextAlign? textAlign;
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final language = Localizations.maybeLocaleOf(context)?.languageCode ?? 'en';
|
|
final latin = const [
|
|
'en',
|
|
'de',
|
|
'fr',
|
|
'es',
|
|
'it',
|
|
'pt',
|
|
'nl',
|
|
'da',
|
|
'sv',
|
|
'no',
|
|
'fi',
|
|
].contains(language);
|
|
return Text(
|
|
CopyScope.text(context, data),
|
|
style: latin
|
|
? style
|
|
: (style ?? const TextStyle()).copyWith(fontFamily: 'sans-serif'),
|
|
textAlign: textAlign,
|
|
);
|
|
}
|
|
}
|