From 10c35381d57d1a77570e4705ff735362d3d5babc Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 4 Oct 2021 09:46:17 +0200 Subject: [PATCH] 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 --- api/src/Contacts/JsContact.php | 11 +++++++++++ api/src/Country.php | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/api/src/Contacts/JsContact.php b/api/src/Contacts/JsContact.php index c456c27583..9c8c9b932f 100644 --- a/api/src/Contacts/JsContact.php +++ b/api/src/Contacts/JsContact.php @@ -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; } diff --git a/api/src/Country.php b/api/src/Country.php index 5b86e0e75d..9f9610d4dd 100755 --- a/api/src/Country.php +++ b/api/src/Country.php @@ -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