Files
kooltab/lib/l10n/app_localizations_helpers.dart
T
2026-10-08 04:12:36 +02:00

166 lines
5.6 KiB
Dart

import '../models/app_error.dart';
import 'app_localizations.dart';
extension AppLocalizationsHelpers on AppLocalizations {
String categoryLabel(String category) {
return switch (category.trim().toLowerCase()) {
'bier' || 'beer' => categoryBeer,
'wijn' || 'wine' => categoryWine,
'frisdrank' || 'soft drinks' => categorySoftDrinks,
'cocktails' => categoryCocktails,
'snacks' => categorySnacks,
'coffee' || 'koffie' => categoryCoffee,
'thee' || 'tea' => categoryTea,
'other' || 'overig' => categoryOther,
_ => category,
};
}
String errorText(AppError? error) {
return switch (error) {
null => genericError,
AppError.loadBar => couldNotLoadBar,
AppError.createTab => couldNotCreateTabMessage,
AppError.outOfStock => productOutOfStock,
AppError.addProductToTab => couldNotAddProductToTab,
AppError.updateQuantity => couldNotUpdateQuantity,
AppError.closeTab => couldNotCloseTab,
AppError.renameTab => couldNotRenameTab,
AppError.deleteTab => couldNotDeleteTab,
AppError.loadHistory => couldNotLoadHistory,
AppError.loadMoreTabs => couldNotLoadMoreTabs,
AppError.loadProducts => couldNotLoadProducts,
AppError.addProduct => couldNotAddProduct,
AppError.updateProduct => couldNotUpdateProduct,
AppError.deleteProduct => couldNotDeleteProduct,
AppError.checkPinStatus => couldNotCheckPinStatus,
AppError.savePin => couldNotSavePin,
AppError.incorrectPin => incorrectPin,
AppError.verifyPin => couldNotVerifyPin,
AppError.currentPinIncorrect => currentPinIncorrect,
AppError.tooManyPinAttempts => tooManyPinAttempts,
AppError.loadSettings => couldNotLoadSettings,
AppError.saveSettings => couldNotSaveSettings,
};
}
/// Update messages still come from the OTA plugin as plain strings.
String _updateMessage(String? message) {
return switch (message) {
'Update failed' => updateFailed,
'Download failed' => downloadFailed,
'Installation failed' => installationFailed,
'Update already in progress' => updateAlreadyInProgress,
_ => message ?? genericError,
};
}
String updateStatus(String status) {
if (status.startsWith('Downloading ')) {
return downloading(status.substring('Downloading '.length));
}
return switch (status) {
'Preparing download…' => preparingDownload,
'Download failed' => downloadFailed,
'Installing update…' => installingUpdate,
'Update installed! Restarting…' => updateInstalledRestarting,
'Installation failed' => installationFailed,
'Update already running' => updateAlreadyRunning,
_ => status,
};
}
String updateError(String? message) {
if (message == null) return updateFailed;
if (message.startsWith('Download failed')) return downloadFailedWithDetail;
if (message.startsWith('Installation failed')) {
return installationFailedWithDetail;
}
if (message == 'Simulated download error (network timeout)') {
return simulatedDownloadError;
}
if (message == 'Update already in progress') return updateAlreadyInProgress;
return _updateMessage(message);
}
String devAction(String? message) {
if (message == null) return '';
final seeded = RegExp(
r'Seeded (?:default products )?\((\d+) total\)',
).firstMatch(message);
if (seeded != null) {
return devActionSeededProducts(int.parse(seeded.group(1)!));
}
final reseeded = RegExp(
r'Cleared and reseeded \((\d+) products\)',
).firstMatch(message);
if (reseeded != null) {
return devActionReseededProducts(int.parse(reseeded.group(1)!));
}
final resetStock = RegExp(
r'Reset stock for (\d+) products',
).firstMatch(message);
if (resetStock != null) {
return devActionResetStock(int.parse(resetStock.group(1)!));
}
final seededDemo = RegExp(
r'Seeded (\d+) demo products',
).firstMatch(message);
if (seededDemo != null) {
return devActionSeededProducts(int.parse(seededDemo.group(1)!));
}
final testTab = RegExp(
r'Created test tab with (\d+) items',
).firstMatch(message);
if (testTab != null) {
return devActionTestTab(int.parse(testTab.group(1)!));
}
final testProducts = RegExp(
r'Added (\d+) test products',
).firstMatch(message);
if (testProducts != null) {
return devActionTestProducts(int.parse(testProducts.group(1)!));
}
final lowStock = RegExp(
r'Simulated low stock for (\d+) products',
).firstMatch(message);
if (lowStock != null) {
return devActionLowStock(int.parse(lowStock.group(1)!));
}
final mockOrders = RegExp(
r'Generated (\d+) mock orders across (\d+) customers',
).firstMatch(message);
if (mockOrders != null) {
return devActionMockOrders(
int.parse(mockOrders.group(1)!),
int.parse(mockOrders.group(2)!),
);
}
return switch (message) {
'Cleared all open tabs' => devActionClearedOpenTabs,
'Cleared closed tab history' => devActionClearedHistory,
'Cleared all products' => devActionClearedProducts,
'Product images cleared' => devActionImagesCleared,
'PIN reset (no PIN required)' => devActionResetPin,
'No products to add to test tab' => devActionNoProducts,
'No products available — seed demo data first' => devActionNoProducts,
'Product images reloaded' => devActionImagesReloaded,
'Image cache cleared (no-op in debug mode)' => devActionCacheCleared,
_ when message.startsWith('Failed to clear images') =>
devActionFailedImages,
_ => message,
};
}
}