improving parsing of country data:

- if we got a country-name but no -code, look up the code
- if we got a country-name like "Name (Name2)" try both separate first
- if we have a valid country-code set the -name to be the untranslated one as our UI does
This commit is contained in:
Ralf Becker 2021-10-04 09:46:17 +02:00
parent b90dcede90
commit 10c35381d5
2 changed files with 24 additions and 0 deletions

View File

@ -630,6 +630,17 @@ class JsContact
}
$contact[$prefix.$attr] = $address[$js];
}
// no country-code but a name translating to a code --> use it
if (empty($contact[$prefix.'countrycode']) && !empty($contact[$prefix.'countryname']) &&
strlen($code = Api\Country::country_code($contact[$prefix.'countryname'])) === 2)
{
$contact[$prefix.'countrycode'] = $code;
}
// if we have a valid code, the untranslated name as our UI does
if (!empty($contact[$prefix.'countrycode']) && !empty($name = Api\Country::get_full_name($contact[$prefix.'countrycode'], false)))
{
$contact[$prefix.'countryname'] = $name;
}
return $contact;
}

View File

@ -766,6 +766,19 @@ class Country
{
if (!$name) return ''; // nothing to do
// handle names like "Germany (Deutschland)"
if (preg_match('/^([^(]+) \(([^)]+)\)$/', $name, $matches))
{
if (($code = self::country_code($matches[1])) && strlen($code) === 2)
{
return $code;
}
if (($code = self::country_code($matches[2])) && strlen($code) === 2)
{
return $code;
}
}
if (strlen($name) == 2 && isset(self::$country_array[$name]))
{
return $name; // $name is already a country-code