1
0
mirror of https://github.com/immich-app/immich.git synced 2025-08-09 23:17:29 +02:00

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

View File

@ -1,4 +1,7 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:immich_mobile/utils/translation.dart';
import 'package:intl/message_format.dart';
extension ContextHelper on BuildContext {
// Returns the current padding from MediaQuery
@ -58,4 +61,17 @@ extension ContextHelper on BuildContext {
// Show SnackBars from the current context
void showSnackBar(SnackBar 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;
}
}
}

View File

@ -205,27 +205,24 @@ class ImmichAppState extends ConsumerState<ImmichApp>
overrides: [
localeProvider.overrideWithValue(context.locale),
],
child: MaterialApp(
child: MaterialApp.router(
title: 'Immich',
debugShowCheckedModeBanner: true,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
debugShowCheckedModeBanner: true,
home: MaterialApp.router(
title: 'Immich',
debugShowCheckedModeBanner: false,
themeMode: ref.watch(immichThemeModeProvider),
darkTheme: getThemeData(
colorScheme: immichTheme.dark,
locale: context.locale,
),
theme: getThemeData(
colorScheme: immichTheme.light,
locale: context.locale,
),
routeInformationParser: router.defaultRouteParser(),
routerDelegate: router.delegate(
navigatorObservers: () => [AppNavigationObserver(ref: ref)],
),
themeMode: ref.watch(immichThemeModeProvider),
darkTheme: getThemeData(
colorScheme: immichTheme.dark,
locale: context.locale,
),
theme: getThemeData(
colorScheme: immichTheme.light,
locale: context.locale,
),
routeInformationParser: router.defaultRouteParser(),
routerDelegate: router.delegate(
navigatorObservers: () => [AppNavigationObserver(ref: ref)],
),
),
);

View File

@ -162,7 +162,7 @@ class _LanguageSearchBar extends StatelessWidget {
child: SearchField(
autofocus: false,
contentPadding: const EdgeInsets.all(12),
hintText: 'language_search_hint'.tr(),
hintText: context.t('language_search_hint'),
prefixIcon: const Icon(Icons.search_rounded),
suffixIcon: controller.text.isNotEmpty
? IconButton(
@ -196,14 +196,14 @@ class _LanguageNotFound extends StatelessWidget {
),
const SizedBox(height: 8),
Text(
'language_no_results_title'.tr(),
context.t('language_no_results_title'),
style: context.textTheme.titleMedium?.copyWith(
color: context.colorScheme.onSurface,
),
),
const SizedBox(height: 4),
Text(
'language_no_results_subtitle'.tr(),
context.t('language_no_results_subtitle'),
style: context.textTheme.bodyMedium?.copyWith(
color: context.colorScheme.onSurface.withValues(alpha: 0.8),
),
@ -246,7 +246,7 @@ class _LanguageApplyButton extends StatelessWidget {
),
)
: Text(
'setting_languages_apply'.tr(),
context.t('setting_languages_apply'),
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16.0,