suggestLanguageCodes function
Suggests similar language codes if the given code
is not found.
Uses fuzzy matching to find potentially intended language codes.
Example:
final suggestions = suggestLanguageCodes('fre'); // ['fr']
Implementation
List<String> suggestLanguageCodes(String code) {
final normalized = code.toLowerCase();
final suggestions = <String>[];
// Exact substring matches
for (final lang in supportedLanguages.values) {
if (lang.code.contains(normalized) ||
lang.name.toLowerCase().contains(normalized) ||
lang.nativeName.toLowerCase().contains(normalized)) {
suggestions.add(lang.code);
}
}
// If no suggestions found, try partial matches
if (suggestions.isEmpty && normalized.length >= 2) {
final prefix = normalized.substring(0, 2);
for (final lang in supportedLanguages.values) {
if (lang.code.startsWith(prefix)) {
suggestions.add(lang.code);
}
}
}
return suggestions.take(5).toList(); // Limit to top 5 suggestions
}