replaceSubdomainWithCodes static method
Replaces the subdomain of an URI with specific country and language.
No default language nor country: null means no parameter. For instance
- https://world.xxx... would be standard
- https://world-fr.xxx... would be "no country" in French
- https://fr.xxx... would be France
- https://fr-es.xxx... would be France in Spanish
Implementation
static Uri replaceSubdomainWithCodes(
final Uri uri, {
final String? languageCode,
String? countryCode,
}) {
final String initialSubdomain = uri.host.split('.')[0];
countryCode = countryCode ?? initialSubdomain;
final String subdomain;
if (languageCode != null) {
subdomain = '$countryCode-$languageCode';
} else {
subdomain = countryCode;
}
return uri.replace(
host: uri.host.replaceFirst('$initialSubdomain.', '$subdomain.'),
);
}