hourAsHumanReadableString static method

String hourAsHumanReadableString(
  1. BuildContext context,
  2. int numberToConvert
)

Converts an hour (in 24-hour format) into a human-readable string based on the locale.

This function formats the given hour using the locale's hour-minute format. If the locale is German (de), the string " Uhr" is appended to the result.

  • context: The BuildContext used to retrieve the current locale.
  • numberToConvert: The hour (in 24-hour format) to convert into a human-readable string.

Returns the hour as a human-readable string.

Implementation

static String hourAsHumanReadableString(BuildContext context, int numberToConvert) {
  // Create a DateFormat object based on the locale's language code.
  var formatter = DateFormat.Hm(Localizations.localeOf(context).languageCode);

  // Format the hour into a human-readable string.
  String hourAsHumanReadableString = formatter.format(DateTime(2023, 5, 9, numberToConvert, 0));

  // Add "Uhr" to the string for the German locale.
  switch (EasyLocalization.of(context)!.locale.languageCode) {
    case 'de':
      hourAsHumanReadableString += " Uhr";
      break;
    case 'en':
    default:
    // No changes needed for the English locale.
      break;
  }
  return hourAsHumanReadableString;
}