Files
kooltab/lib/l10n/app_localizations_helpers.dart
T
2026-10-26 23:15:05 +01:00

167 lines
5.8 KiB
Dart

import 'package:intl/intl.dart';
import '../models/app_error.dart';
import '../models/remote_command.dart';
import '../models/update_failure.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,
};
}
/// "just now", "5 minutes ago"…, and the full date once a day has passed.
String relativeTime(DateTime time, String locale, {DateTime? now}) {
final elapsed = (now ?? DateTime.now()).difference(time);
if (elapsed.inMinutes < 1) return justNow;
if (elapsed.inHours < 1) return minutesAgo(elapsed.inMinutes);
if (elapsed.inDays < 1) return hoursAgo(elapsed.inHours);
return DateFormat.yMMMd(locale).add_jm().format(time.toLocal());
}
String remoteCommandLabel(String rawType) {
return switch (CommandType.fromValue(rawType)) {
CommandType.closeTab => remoteCommandCloseTab,
null => remoteCommandUnknown(rawType),
};
}
String commandResultLabel(CommandResult result) {
return switch (result) {
CommandResult.completed => commandCompleted,
CommandResult.conflict => commandConflict,
CommandResult.failed => commandFailed,
CommandResult.expired => commandExpired,
};
}
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,
};
}
String updateFailureText(UpdateFailure? failure) {
return switch (failure) {
UpdateFailure.download => updateDownloadFailed,
UpdateFailure.checksum => updateChecksumFailed,
UpdateFailure.permission => updatePermissionDenied,
UpdateFailure.install => updateInstallFailed,
UpdateFailure.alreadyRunning => updateAlreadyInProgress,
UpdateFailure.simulated => simulatedDownloadError,
UpdateFailure.unknown || null => updateFailed,
};
}
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,
};
}
}