fix can't use context for easy_localization

This commit is contained in:
dvbthien
2025-06-05 22:25:31 +07:00
parent 53a4a0d15a
commit f269634070
3 changed files with 35 additions and 22 deletions
@@ -1,4 +1,7 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:immich_mobile/utils/translation.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
@@ -58,4 +61,17 @@ 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;
}
}
} }
+15 -18
View File
@@ -205,27 +205,24 @@ class ImmichAppState extends ConsumerState<ImmichApp>
overrides: [ overrides: [
localeProvider.overrideWithValue(context.locale), localeProvider.overrideWithValue(context.locale),
], ],
child: MaterialApp( child: MaterialApp.router(
title: 'Immich',
debugShowCheckedModeBanner: true,
localizationsDelegates: context.localizationDelegates, localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales, supportedLocales: context.supportedLocales,
locale: context.locale, locale: context.locale,
debugShowCheckedModeBanner: true, themeMode: ref.watch(immichThemeModeProvider),
home: MaterialApp.router( darkTheme: getThemeData(
title: 'Immich', colorScheme: immichTheme.dark,
debugShowCheckedModeBanner: false, locale: context.locale,
themeMode: ref.watch(immichThemeModeProvider), ),
darkTheme: getThemeData( theme: getThemeData(
colorScheme: immichTheme.dark, colorScheme: immichTheme.light,
locale: context.locale, locale: context.locale,
), ),
theme: getThemeData( routeInformationParser: router.defaultRouteParser(),
colorScheme: immichTheme.light, routerDelegate: router.delegate(
locale: context.locale, navigatorObservers: () => [AppNavigationObserver(ref: ref)],
),
routeInformationParser: router.defaultRouteParser(),
routerDelegate: router.delegate(
navigatorObservers: () => [AppNavigationObserver(ref: ref)],
),
), ),
), ),
); );
@@ -162,7 +162,7 @@ class _LanguageSearchBar extends StatelessWidget {
child: SearchField( child: SearchField(
autofocus: false, autofocus: false,
contentPadding: const EdgeInsets.all(12), contentPadding: const EdgeInsets.all(12),
hintText: 'language_search_hint'.tr(), hintText: context.t('language_search_hint'),
prefixIcon: const Icon(Icons.search_rounded), prefixIcon: const Icon(Icons.search_rounded),
suffixIcon: controller.text.isNotEmpty suffixIcon: controller.text.isNotEmpty
? IconButton( ? IconButton(
@@ -196,14 +196,14 @@ class _LanguageNotFound extends StatelessWidget {
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
Text( Text(
'language_no_results_title'.tr(), context.t('language_no_results_title'),
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(
'language_no_results_subtitle'.tr(), context.t('language_no_results_subtitle'),
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),
), ),
@@ -246,7 +246,7 @@ class _LanguageApplyButton extends StatelessWidget {
), ),
) )
: Text( : Text(
'setting_languages_apply'.tr(), context.t('setting_languages_apply'),
style: const TextStyle( style: const TextStyle(
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
fontSize: 16.0, fontSize: 16.0,