canonicalizePath method
Canonicalizes a localized URL path to canonical English.
/libro/abc123 → /book/abc123
/livre/abc123 → /book/abc123
/book/abc123 → /book/abc123 (already canonical)
Implementation
String canonicalizePath(String path) {
if (path.isEmpty || path == '/') return path;
final qIndex = path.indexOf('?');
final purePath = qIndex > -1 ? path.substring(0, qIndex) : path;
final query = qIndex > -1 ? path.substring(qIndex) : '';
final segments = purePath.split('/');
var changed = false;
for (var i = 0; i < segments.length; i++) {
final seg = segments[i];
if (seg.isEmpty) continue;
final canonical = _reverseMap[seg];
if (canonical != null) {
segments[i] = canonical;
changed = true;
}
}
if (!changed) return path;
return segments.join('/') + query;
}