mirror of
https://github.com/immich-app/immich.git
synced 2025-06-22 06:50:54 -04:00
* re-write localization service and add translation extension * Revert "re-write localization service and add translation extension" This reverts commit fdd7386020f638b92ad4f4691667d67e8c2935fc. * fix can't use context for easy_localization * fix lint * update new translate context * handle context null * revert main file * Revert "revert main file" This reverts commit 16faca46d0a36abafe41a19bb46b38fffa4940f1. * remove fix nested MaterialApp * change use t extenstion and remove translation utils * update function call similar for consistently --------- Co-authored-by: dvbthien <dvbthien@gmail.com>
51 lines
1.3 KiB
Dart
51 lines
1.3 KiB
Dart
import 'package:easy_localization/easy_localization.dart';
|
|
import 'package:intl/message_format.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
extension StringTranslateExtension on String {
|
|
String t({BuildContext? context, Map<String, Object>? args}) {
|
|
return _translateHelper(context, this, args);
|
|
}
|
|
}
|
|
|
|
extension TextTranslateExtension on Text {
|
|
Text t({BuildContext? context, Map<String, Object>? args}) {
|
|
return Text(
|
|
_translateHelper(context, data ?? '', args),
|
|
key: key,
|
|
style: style,
|
|
strutStyle: strutStyle,
|
|
textAlign: textAlign,
|
|
textDirection: textDirection,
|
|
locale: locale,
|
|
softWrap: softWrap,
|
|
overflow: overflow,
|
|
textScaler: textScaler,
|
|
maxLines: maxLines,
|
|
semanticsLabel: semanticsLabel,
|
|
textWidthBasis: textWidthBasis,
|
|
textHeightBehavior: textHeightBehavior,
|
|
);
|
|
}
|
|
}
|
|
|
|
String _translateHelper(
|
|
BuildContext? context,
|
|
String key, [
|
|
Map<String, Object>? args,
|
|
]) {
|
|
if (key.isEmpty) {
|
|
return '';
|
|
}
|
|
try {
|
|
final translatedMessage = key.tr(context: context);
|
|
return args != null
|
|
? MessageFormat(translatedMessage, locale: Intl.defaultLocale ?? 'en')
|
|
.format(args)
|
|
: translatedMessage;
|
|
} catch (e) {
|
|
debugPrint('Translation failed for key "$key". Error: $e');
|
|
return key;
|
|
}
|
|
}
|