PathTranslator.build constructor

PathTranslator.build({
  1. required Map<String, Map<String, String>> translations,
  2. required Set<String> routeSegments,
})

Builds a PathTranslator from SINT's loaded translations and the static route segments extracted from registered SintPage names.

translations — full translation map from Sint.translations ({locale: {key: value}}). routeSegments — static segments extracted via extractSegments.

Implementation

factory PathTranslator.build({
  required Map<String, Map<String, String>> translations,
  required Set<String> routeSegments,
}) {
  final forwardMaps = <String, Map<String, String>>{};
  final reverseMap = <String, String>{};

  for (final localeEntry in translations.entries) {
    // Handle both 'es' and 'es_MX' style keys.
    final locale = localeEntry.key.split('_').first;
    if (locale == 'en') continue; // English is canonical — no mapping.

    final localeMap = <String, String>{};
    for (final segment in routeSegments) {
      final value = localeEntry.value[segment];
      if (value == null) continue;

      final normalized = _removeDiacritics(value.toLowerCase());
      if (normalized.contains(' ')) continue; // Multi-word: can't be URL segment.
      if (normalized == segment) continue; // Same as canonical: no-op.

      localeMap[segment] = normalized;
      reverseMap[normalized] = segment;
    }

    if (localeMap.isNotEmpty) {
      // Merge into existing locale map (handles 'es' + 'es_MX' both present).
      forwardMaps.putIfAbsent(locale, () => {}).addAll(localeMap);
    }
  }

  return PathTranslator._(forwardMaps, reverseMap);
}