From ebf1246a5530b126ee785fe03c4635ec171d938b Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 18 Dec 2024 18:17:22 +0100 Subject: [PATCH] added month localizations --- lib/constants.dart | 16 ----- lib/l10n/app_de.arb | 16 ++++- lib/l10n/app_en.arb | 16 ++++- lib/services/tools.dart | 101 ++++++++++++++++++++++++++--- lib/widgets/content_list_tile.dart | 8 +-- 5 files changed, 127 insertions(+), 30 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index d386779..0aa4f6b 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,19 +1,3 @@ -/// List of months 0-11 -const months = [ - 'Januar', - 'Februar', - 'März', - 'April', - 'Mai', - 'Juni', - 'July', - 'August', - 'September', - 'Oktober', - 'November', - 'Dezember' -]; - enum ContentType { experience, education, diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index eae7bbc..f79c94e 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -10,5 +10,19 @@ "additional_skills": "Weitere Kenntnisse", "work_experience": "Arbeitserfahrung", "education": "Bildungsweg", - "source_code": "Quellcode" + "source_code": "Quellcode", + + "@_months": {}, + "january": "Januar", + "february": "Februar", + "march": "März", + "april": "April", + "may": "Mai", + "june": "Juni", + "july": "Juli", + "august": "August", + "september": "September", + "october": "Oktober", + "november": "November", + "december": "Dezember" } \ No newline at end of file diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 4690b50..6397306 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -10,5 +10,19 @@ "additional_skills": "Additional Skills", "work_experience": "Work experience", "education": "Education", - "source_code": "Source Code" + "source_code": "Source Code", + + "@_months": {}, + "january": "January", + "february": "February", + "march": "March", + "april": "April", + "may": "May", + "june": "June", + "july": "July", + "august": "August", + "september": "September", + "october": "October", + "november": "November", + "december": "December" } \ No newline at end of file diff --git a/lib/services/tools.dart b/lib/services/tools.dart index aa1a5c4..9177c52 100644 --- a/lib/services/tools.dart +++ b/lib/services/tools.dart @@ -1,12 +1,97 @@ -import 'package:resume/constants.dart' show months; +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class Tools { - /// builds timespan-String out of two date-Strings. - /// Date has to be formatted as yyyy-MM - static buildTimeString(String startDate, String endDate) { - if (startDate.isEmpty || endDate.isEmpty) return ''; - final firstDate = DateTime.parse('$startDate-01'); - final secondDate = DateTime.parse('$endDate-01'); - return '${months[firstDate.month - 1]} ${firstDate.year} - ${months[secondDate.month - 1]} ${secondDate.year}'; + /// Builds a formatted time string from two date strings in the format 'YYYY-MM'. + /// + /// Returns a string in the format 'Month Year - Month Year' using localized month names. + /// Returns an empty string if either input is empty or invalid. + /// + /// Parameters: + /// - [startDate]: String in 'YYYY-MM' format (e.g., '2024-03') + /// - [endDate]: String in 'YYYY-MM' format (e.g., '2024-12') + /// - [context]: BuildContext for accessing localizations + /// + /// Example: + /// ```dart + /// final timeString = buildTimeString('2024-03', '2024-12', context); + /// // Returns "March 2024 - December 2024" (or localized equivalent) + /// ``` + static String buildTimeString( + String startDate, String endDate, BuildContext context) { + // Check for empty or null inputs + if (startDate.isEmpty || endDate.isEmpty) { + return ''; + } + + try { + // Validate date format + if (!startDate.contains(RegExp(r'^\d{4}-\d{2}$')) || + !endDate.contains(RegExp(r'^\d{4}-\d{2}$'))) { + return ''; + } + + // Parse dates with validation + final firstDate = DateTime.tryParse('$startDate-01'); + final secondDate = DateTime.tryParse('$endDate-01'); + + // Check if parsing was successful + if (firstDate == null || secondDate == null) { + return ''; + } + + // Build the formatted string + return '${getLocalizedMonth(context, firstDate.month)} ${firstDate.year} - ' + '${getLocalizedMonth(context, secondDate.month)} ${secondDate.year}'; + } catch (e) { + // Handle any unexpected errors + debugPrint('Error building time string: $e'); + return ''; + } + } + + /// Returns the localized month name as a [String] based on the provided month number. + /// + /// The [context] is used to access the app's localizations. + /// The [monthNumber] must be between 1 and 12, where 1 represents January + /// and 12 represents December. + /// + /// Throws an [ArgumentError] if the month number is not between 1 and 12. + /// + /// Example: + /// ```dart + /// final monthName = getLocalizedMonth(context, 3); // Returns "March" or "März" + /// ``` + static String getLocalizedMonth(BuildContext context, int monthNumber) { + final localizations = AppLocalizations.of(context)!; + + switch (monthNumber) { + case 1: + return localizations.january; + case 2: + return localizations.february; + case 3: + return localizations.march; + case 4: + return localizations.april; + case 5: + return localizations.may; + case 6: + return localizations.june; + case 7: + return localizations.july; + case 8: + return localizations.august; + case 9: + return localizations.september; + case 10: + return localizations.october; + case 11: + return localizations.november; + case 12: + return localizations.december; + default: + throw ArgumentError('Month number must be between 1 and 12'); + } } } diff --git a/lib/widgets/content_list_tile.dart b/lib/widgets/content_list_tile.dart index d4ea81b..2a9cc5f 100644 --- a/lib/widgets/content_list_tile.dart +++ b/lib/widgets/content_list_tile.dart @@ -97,7 +97,7 @@ class ContentListTile extends StatelessWidget { ? Padding( padding: const EdgeInsets.only(bottom: 8), child: Text( - Tools.buildTimeString(startDate, endDate), + Tools.buildTimeString(startDate, endDate, context), style: Theme.of(context).textTheme.labelSmall!.copyWith( color: Theme.of(context).colorScheme.secondary, ), @@ -106,7 +106,7 @@ class ContentListTile extends StatelessWidget { : null, trailing: Breakpoints.sm < screenWidth ? Text( - Tools.buildTimeString(startDate, endDate), + Tools.buildTimeString(startDate, endDate, context), style: TextStyle(color: Theme.of(context).colorScheme.secondary), ) @@ -149,7 +149,7 @@ class ContentListTile extends StatelessWidget { Padding( padding: const EdgeInsets.only(bottom: 8), child: Text( - Tools.buildTimeString(startDate, endDate), + Tools.buildTimeString(startDate, endDate, context), style: Theme.of(context).textTheme.labelSmall!.copyWith( color: Theme.of(context).colorScheme.secondary, ), @@ -160,7 +160,7 @@ class ContentListTile extends StatelessWidget { ), trailing: Breakpoints.sm < screenWidth ? Text( - Tools.buildTimeString(startDate, endDate), + Tools.buildTimeString(startDate, endDate, context), style: TextStyle(color: Theme.of(context).colorScheme.secondary), )