Add i18n support and integrate localized strings across modules (Login, AppShell, etc.)

This commit is contained in:
2025-09-27 12:15:57 +02:00
parent 0a0e421158
commit 140e3a7328
8 changed files with 576 additions and 71 deletions

View File

@@ -1,3 +1,4 @@
import 'package:app/core/i18n/translations.g.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
@@ -23,53 +24,55 @@ class _AppShellState extends State<AppShell> {
// --- NAV ITEMS -------------------------------------------------------------
final _items =
const <
({IconData icon, IconData? selectedIcon, String label, String route})
>[
(
icon: Icons.dashboard_outlined,
selectedIcon: Icons.dashboard,
label: 'Dashboard',
route: '/home',
),
(
icon: Icons.account_balance_wallet_outlined,
selectedIcon: Icons.account_balance_wallet,
label: 'Budgets',
route: '/budget',
),
(
icon: Icons.inventory_2_outlined,
selectedIcon: Icons.inventory_2,
label: 'Inventar',
route: '/inventory',
),
(
icon: Icons.bar_chart_outlined,
selectedIcon: Icons.bar_chart,
label: 'Reports',
route: '/reports',
),
(
List<({IconData icon, IconData? selectedIcon, String label, String route})> _getItems(BuildContext context) {
final t = Translations.of(context);
return [
(
icon: Icons.dashboard_outlined,
selectedIcon: Icons.dashboard,
label: t.app.navigationDashboard,
route: '/home',
),
(
icon: Icons.account_balance_wallet_outlined,
selectedIcon: Icons.account_balance_wallet,
label: t.app.navigationBudgets,
route: '/budget',
),
(
icon: Icons.inventory_2_outlined,
selectedIcon: Icons.inventory_2,
label: t.app.navigationInventory,
route: '/inventory',
),
(
icon: Icons.bar_chart_outlined,
selectedIcon: Icons.bar_chart,
label: t.app.navigationReports,
route: '/reports',
),
(
icon: Icons.settings,
selectedIcon: Icons.settings_outlined,
label: 'Settings',
label: t.app.navigationSettings,
route: '/settings',
),
];
),
];
}
int _indexForPath(String p) {
for (var i = 0; i < _items.length; i++) {
if (p.startsWith(_items[i].route)) return i;
int _indexForPath(String p, List<({IconData icon, IconData? selectedIcon, String label, String route})> items) {
for (var i = 0; i < items.length; i++) {
if (p.startsWith(items[i].route)) return i;
}
return 0;
}
void _goForIndex(BuildContext ctx, int i) => ctx.go(_items[i].route);
void _goForIndex(BuildContext ctx, int i, List<({IconData icon, IconData? selectedIcon, String label, String route})> items) => ctx.go(items[i].route);
@override
Widget build(BuildContext context) {
final t = Translations.of(context);
final items = _getItems(context);
final width = MediaQuery.of(context).size.width;
final isRail = width >= _railBreakpoint;
final currentPath = GoRouterState.of(context).matchedLocation;
@@ -89,17 +92,17 @@ class _AppShellState extends State<AppShell> {
builder: (ctx) => IconButton(
icon: const Icon(Icons.menu),
onPressed: () => Scaffold.of(ctx).openDrawer(),
tooltip: 'Menü',
tooltip: t.app.tooltipMenu,
),
),
actions: [
IconButton(
tooltip: 'Benachrichtigungen',
tooltip: t.app.tooltipNotifications,
onPressed: () {},
icon: const Icon(Icons.notifications_none),
),
IconButton(
tooltip: 'Benutzer-Einstellungen',
tooltip: t.app.tooltipUserSettings,
onPressed: () => context.push('/settings'),
icon: const Icon(Icons.account_circle_outlined),
),
@@ -108,16 +111,16 @@ class _AppShellState extends State<AppShell> {
if (!isRail) {
// ------------------- MOBILE: Drawer -------------------
final selectedIndex = _indexForPath(currentPath);
final selectedIndex = _indexForPath(currentPath, items);
return Scaffold(
appBar: appBar,
drawer: _AppDrawer(items: _items, selectedIndex: selectedIndex),
drawer: _AppDrawer(items: items, selectedIndex: selectedIndex),
body: SafeArea(child: widget.child),
);
}
// ------------------- TABLET/DESKTOP: NavigationRail -------------------
final selected = _indexForPath(currentPath);
final selected = _indexForPath(currentPath, items);
final scheme = Theme.of(context).colorScheme;
return Scaffold(
@@ -143,13 +146,13 @@ class _AppShellState extends State<AppShell> {
child: NavigationRail(
extended: _railExtended,
selectedIndex: selected,
onDestinationSelected: (i) => _goForIndex(context, i),
onDestinationSelected: (i) => _goForIndex(context, i, items),
// leading: const Padding(
// padding: EdgeInsets.only(top: 8),
// child: _LogoHeader(),
// ),
destinations: [
for (final it in _items)
for (final it in items)
NavigationRailDestination(
icon: Icon(it.icon),
selectedIcon: Icon(it.selectedIcon ?? it.icon),
@@ -164,8 +167,8 @@ class _AppShellState extends State<AppShell> {
// toggle lives at the very bottom so layout doesn't jump
IconButton(
tooltip: _railExtended
? 'Leiste verkleinern'
: 'Leiste erweitern',
? t.app.tooltipCollapseRail
: t.app.tooltipExpandRail,
onPressed: () =>
setState(() => _railExtended = !_railExtended),
icon: Icon(
@@ -234,7 +237,7 @@ class _AppDrawer extends StatelessWidget {
const Divider(),
ListTile(
leading: const Icon(Icons.settings_outlined),
title: const Text('Einstellungen'),
title: Text(Translations.of(context).app.drawerSettings),
onTap: () {
Navigator.of(context).maybePop();
context.go('/settings');