mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
fix(mobile): add translate extension (#18942)
* re-write localization service and add translation extension * Revert "re-write localization service and add translation extension" This reverts commitfdd7386020. * 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 commit16faca46d0. * 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>
This commit is contained in:
parent
16fcb657b7
commit
3d0c851636
11 changed files with 133 additions and 60 deletions
50
mobile/lib/extensions/translate_extensions.dart
Normal file
50
mobile/lib/extensions/translate_extensions.dart
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue