replaceSubdomainWithCodes static method

Uri replaceSubdomainWithCodes(
  1. Uri uri, {
  2. String? languageCode,
  3. String? countryCode,
})

Replaces the subdomain of an URI with specific country and language.

No default language nor country: null means no parameter. For instance

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.'),
  );
}