oryksaTvNormalizeLocale function

String oryksaTvNormalizeLocale(
  1. String raw
)

Normalises a locale string (pt_BR, pt-br, PT-BR) to pt-BR. Empty stays empty.

Implementation

String oryksaTvNormalizeLocale(String raw) {
  final p = raw.trim().replaceAll('_', '-').split('-').where((s) => s.isNotEmpty).toList();
  if (p.isEmpty) return '';
  final out = <String>[p.first.toLowerCase()];
  for (final s in p.skip(1)) {
    if (s.length == 4) {
      out.add(s[0].toUpperCase() + s.substring(1).toLowerCase()); // script, e.g. Latn
    } else {
      out.add(s.toUpperCase()); // region, e.g. BR or 419
    }
  }
  return out.join('-');
}