getLocalizedValue method

String? getLocalizedValue(
  1. PassFile pass,
  2. Locale? locale
)

Localized version of value based on given locale and pass.

Implementation

String? getLocalizedValue(PassFile pass, Locale? locale) {
  final language = locale?.toLanguageTag();

  if (value is StringDictionaryValue) {
    String string = (value as StringDictionaryValue).string;
    final localizations = pass.getLocalizations(locale);
    return localizations?[string] ?? string;
  } else if (value is DateTimeDictionaryValue) {
    DateTime dateTime = (value as DateTimeDictionaryValue).dateTime;
    DateFormat format = DateFormat(null, language);

    switch (dateStyle) {
      case PassTextDateStyle.none:
        break;
      case PassTextDateStyle.short:
        format = DateFormat.yMd(language);
        break;
      case PassTextDateStyle.medium:
        format = DateFormat.yMMMd(language);
        break;
      case PassTextDateStyle.long:
        format = DateFormat.yMMMMd(language);
        break;
      case PassTextDateStyle.full:
      default:
        format = DateFormat.yMMMMEEEEd(language);
        break;
    }

    switch (timeStyle) {
      case PassTextDateStyle.none:
        break;
      case PassTextDateStyle.short:
        format.add_jm();
        break;
      case PassTextDateStyle.medium:
      case PassTextDateStyle.long:
      case PassTextDateStyle.full:
      default:
        format.add_jms();
        break;
    }

    if (ignoresTimeZone) {
      /// violating standard: return in UTC instead of original timezone
      return format.format(dateTime);
    } else {
      return format.format(dateTime.toLocal());
    }
  } else if (value is NumberDictionaryValue) {
    num number = (value as NumberDictionaryValue).number;
    if (currencyCode != null) {
      return NumberFormat.simpleCurrency(locale: language, name: currencyCode)
          .format(number);
    } else if (numberStyle == PassTextNumberStyle.percent) {
      return NumberFormat.percentPattern(language).format(number);
    } else if (numberStyle == PassTextNumberStyle.scientific) {
      return NumberFormat.scientificPattern(language).format(number);
    } else {
      /// PassTextNumberStyle.SpellOut not implemented
      /// default to decimal format
      return NumberFormat.decimalPattern(language).format(number);
    }
  }

  return null;
}