added month localizations
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user