You've already forked immich
mirror of
https://github.com/immich-app/immich.git
synced 2025-08-09 23:17:29 +02:00
update new translate context
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import 'package:easy_localization/easy_localization.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:intl/message_format.dart';
|
|
||||||
|
|
||||||
extension ContextHelper on BuildContext {
|
extension ContextHelper on BuildContext {
|
||||||
// Returns the current padding from MediaQuery
|
// Returns the current padding from MediaQuery
|
||||||
@@ -60,17 +58,4 @@ extension ContextHelper on BuildContext {
|
|||||||
// Show SnackBars from the current context
|
// Show SnackBars from the current context
|
||||||
void showSnackBar(SnackBar snackBar) =>
|
void showSnackBar(SnackBar snackBar) =>
|
||||||
ScaffoldMessenger.of(this).showSnackBar(snackBar);
|
ScaffoldMessenger.of(this).showSnackBar(snackBar);
|
||||||
|
|
||||||
String t(String key, [Map<String, Object>? args]) {
|
|
||||||
try {
|
|
||||||
String message = this.tr(key);
|
|
||||||
if (args != null) {
|
|
||||||
return MessageFormat(message, locale: Intl.defaultLocale ?? 'en')
|
|
||||||
.format(args);
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
} catch (e) {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 = context.tr(key);
|
||||||
|
return args != null
|
||||||
|
? MessageFormat(translatedMessage, locale: Intl.defaultLocale ?? 'en')
|
||||||
|
.format(args)
|
||||||
|
: translatedMessage;
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Translation failed for key "$key". Error: $e');
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
@@ -4,6 +4,7 @@ import 'package:flutter_hooks/flutter_hooks.dart';
|
|||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:immich_mobile/constants/locales.dart';
|
import 'package:immich_mobile/constants/locales.dart';
|
||||||
|
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
||||||
import 'package:immich_mobile/services/localization.service.dart';
|
import 'package:immich_mobile/services/localization.service.dart';
|
||||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||||
import 'package:immich_mobile/widgets/common/search_field.dart';
|
import 'package:immich_mobile/widgets/common/search_field.dart';
|
||||||
@@ -91,6 +92,7 @@ class LanguageSettings extends HookConsumerWidget {
|
|||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8),
|
||||||
itemCount: filteredLocaleEntries.value.length,
|
itemCount: filteredLocaleEntries.value.length,
|
||||||
itemExtent: 64.0,
|
itemExtent: 64.0,
|
||||||
|
cacheExtent: 100,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final countryName =
|
final countryName =
|
||||||
filteredLocaleEntries.value[index].key;
|
filteredLocaleEntries.value[index].key;
|
||||||
@@ -100,6 +102,7 @@ class LanguageSettings extends HookConsumerWidget {
|
|||||||
selectedLocale.value == localeValue;
|
selectedLocale.value == localeValue;
|
||||||
|
|
||||||
return _LanguageItem(
|
return _LanguageItem(
|
||||||
|
key: ValueKey(localeValue.toString()),
|
||||||
countryName: countryName,
|
countryName: countryName,
|
||||||
localeValue: localeValue,
|
localeValue: localeValue,
|
||||||
isSelected: isSelected,
|
isSelected: isSelected,
|
||||||
@@ -162,7 +165,7 @@ class _LanguageSearchBar extends StatelessWidget {
|
|||||||
child: SearchField(
|
child: SearchField(
|
||||||
autofocus: false,
|
autofocus: false,
|
||||||
contentPadding: const EdgeInsets.all(12),
|
contentPadding: const EdgeInsets.all(12),
|
||||||
hintText: context.t('language_search_hint'),
|
hintText: 'language_search_hint'.t(context),
|
||||||
prefixIcon: const Icon(Icons.search_rounded),
|
prefixIcon: const Icon(Icons.search_rounded),
|
||||||
suffixIcon: controller.text.isNotEmpty
|
suffixIcon: controller.text.isNotEmpty
|
||||||
? IconButton(
|
? IconButton(
|
||||||
@@ -196,14 +199,14 @@ class _LanguageNotFound extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Text(
|
||||||
context.t('language_no_results_title'),
|
'language_no_results_title'.t(context),
|
||||||
style: context.textTheme.titleMedium?.copyWith(
|
style: context.textTheme.titleMedium?.copyWith(
|
||||||
color: context.colorScheme.onSurface,
|
color: context.colorScheme.onSurface,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
Text(
|
Text(
|
||||||
context.t('language_no_results_subtitle'),
|
'language_no_results_subtitle'.t(context),
|
||||||
style: context.textTheme.bodyMedium?.copyWith(
|
style: context.textTheme.bodyMedium?.copyWith(
|
||||||
color: context.colorScheme.onSurface.withValues(alpha: 0.8),
|
color: context.colorScheme.onSurface.withValues(alpha: 0.8),
|
||||||
),
|
),
|
||||||
@@ -245,13 +248,13 @@ class _LanguageApplyButton extends StatelessWidget {
|
|||||||
strokeWidth: 2,
|
strokeWidth: 2,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
: Text(
|
: const Text(
|
||||||
context.t('setting_languages_apply'),
|
'setting_languages_apply',
|
||||||
style: const TextStyle(
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.w600,
|
fontWeight: FontWeight.w600,
|
||||||
fontSize: 16.0,
|
fontSize: 16.0,
|
||||||
),
|
),
|
||||||
),
|
).t(context),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -261,6 +264,7 @@ class _LanguageApplyButton extends StatelessWidget {
|
|||||||
|
|
||||||
class _LanguageItem extends StatelessWidget {
|
class _LanguageItem extends StatelessWidget {
|
||||||
const _LanguageItem({
|
const _LanguageItem({
|
||||||
|
super.key,
|
||||||
required this.countryName,
|
required this.countryName,
|
||||||
required this.localeValue,
|
required this.localeValue,
|
||||||
required this.isSelected,
|
required this.isSelected,
|
||||||
|
Reference in New Issue
Block a user