Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New country abbreviation data source for better abbreviations in multiple languages #535

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions app/Dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -1489,26 +1489,27 @@ private function addIndiFamilies($fid, $pid, $ind, &$families)
public static function getAbbreviatedPlace(string $place_long, array $settings): string
{
// If chose no abbreviating, then return string untouched
if ($settings["use_abbr_place"] == 0 /* Full place name */) {
if ($settings["use_abbr_place"] == Settings::OPTION_FULL_PLACE_NAME) {
return $place_long;
}

// Cut the place name up into pieces using the commas
$place_chunks = explode(",", $place_long);
$place = "";
$chunk_count = count($place_chunks);
$abbreviating_country = !($chunk_count == 1 && ($settings["use_abbr_place"] == Settings::OPTION_2_LETTER_ISO || $settings["use_abbr_place"] == Settings::OPTION_3_LETTER_ISO));

// Add city to our return string as we always keep this
if (!empty($place_chunks[0])) {
// Add city to our return string
if (!empty($place_chunks[0]) && $abbreviating_country) {
$place .= trim($place_chunks[0]);

if ($settings["use_abbr_place"] == 5 /* City only */) {
if ($settings["use_abbr_place"] == Settings::OPTION_CITY_ONLY) {
return $place;
}
}

// Chose to keep just the first and last sections
if ($settings["use_abbr_place"] == 10 /* City and Country */) {
if ($settings["use_abbr_place"] == Settings::OPTION_CITY_AND_COUNTRY) {
if (!empty($place_chunks[$chunk_count - 1]) && ($chunk_count > 1)) {
if (!empty($place)) {
$place .= ", ";
Expand All @@ -1520,10 +1521,10 @@ public static function getAbbreviatedPlace(string $place_long, array $settings):

/* Otherwise, we have chosen one of the ISO code options */
switch ($settings["use_abbr_place"]) {
case 20: //City and 2-Letter ISO Country Code
case Settings::OPTION_2_LETTER_ISO:
$code = "iso2";
break;
case 30: //City and 3-Letter ISO Country Code
case Settings::OPTION_3_LETTER_ISO:
$code = "iso3";
break;
default:
Expand All @@ -1542,7 +1543,7 @@ public static function getAbbreviatedPlace(string $place_long, array $settings):
$place .= $settings["countries"][$code][strtolower(trim($place_chunks[$chunk_count - 1]))];
} else {
// We didn't find country in the abbreviation list, so just add the full country name
if (!empty($place_chunks[$chunk_count - 1]) && ($chunk_count > 1)) {
if (!empty($place_chunks[$chunk_count - 1])) {
$place .= trim($place_chunks[$chunk_count - 1]);
}
}
Expand Down
41 changes: 32 additions & 9 deletions app/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Settings
public const USER_ROLES = ['Visitor', 'Member', 'Editor', 'Moderator', 'Manager'];
const TREE_PREFIX = "_t";
const USER_PREFIX = "_u";
const OPTION_FULL_PLACE_NAME = 0;
const OPTION_CITY_ONLY = 5;
const OPTION_CITY_AND_COUNTRY = 10;
const OPTION_2_LETTER_ISO = 20;
const OPTION_3_LETTER_ISO = 30;
private array $settings_json_cache = [];
private array $defaultSettings;

Expand All @@ -61,7 +66,7 @@ public function __construct($tree = null){
$this->defaultSettings['url_xref_treatment_options']['add'] = "Add to list";
$this->defaultSettings['url_xref_treatment_options']['nothing'] = "Don't add to list";
$this->defaultSettings['url_xref_treatment_options']['overwrite'] = "Overwrite";
$this->defaultSettings['use_abbr_places'] = [0 => "Full place name", 5 => "City only" , 10 => "City and country" , 20 => "City and 2 letter ISO country code", 30 => "City and 3 letter ISO country code"];
$this->defaultSettings['use_abbr_places'] = [self::OPTION_FULL_PLACE_NAME => "Full place name", self::OPTION_CITY_ONLY => "City only" , self::OPTION_CITY_AND_COUNTRY => "City and country" , self::OPTION_2_LETTER_ISO => "City and 2 letter ISO country code", self::OPTION_3_LETTER_ISO => "City and 3 letter ISO country code"];
$this->defaultSettings['use_abbr_names'] = [0 => "Full name", 10 => "Given and surnames", 20 => "Given names" , 30 => "First given name only", 80 => "Preferred given name and surname", 40 => "Surnames", 50 => "Initials only", 60 => "Given name initials and surname", 70 => "Don't show names"];
$this->defaultSettings['photo_shape_options'] = [Person::SHAPE_NONE => "No change", Person::SHAPE_OVAL => "Oval", Person::SHAPE_CIRCLE => "Circle" , Person::SHAPE_SQUARE => "Square", Person::SHAPE_ROUNDED_RECT => "Rounded rectangle", Person::SHAPE_ROUNDED_SQUARE => "Rounded square"];
$this->defaultSettings['photo_quality_options'] = [0 => "Lowest", 20 => "Low", 50 => "Medium" , 75 => "High", 100 => "Highest"];
Expand Down Expand Up @@ -395,20 +400,38 @@ private function isGraphvizAvailable($binPath)

/**
* Load country data for abbreviating place names
* Data comes from https://www.datahub.io/core/country-codes
* This material is licensed by its maintainers under the Public Domain Dedication and License, however,
* they note that the data is ultimately sourced from ISO who have an unclear licence regarding use,
* particularly around commercial use. Though all data sources providing ISO data have this problem.
* Data comes from https://github.com/stefangabos/world_countries
*
* @return array
*/
private function getCountryAbbreviations(): array
{
$string = file_get_contents(dirname(__FILE__) . "/../resources/data/country-codes_json.json");
$countries['iso2'] = $this->loadCountryDataFile('iso2');
$countries['iso3'] = $this->loadCountryDataFile('iso3');
return $countries;
}

/**
* Loads country data from JSON file
*
* @param $type
* @return array|false
*/
private function loadCountryDataFile($type) {
switch ($type) {
case 'iso2':
$string = file_get_contents(dirname(__FILE__) . "/../resources/data/CountryRegionCodes2Char.json");
break;
case 'iso3':
$string = file_get_contents(dirname(__FILE__) . "/../resources/data/CountryRegionCodes3Char.json");
break;
default:
return false;
}
$json = json_decode($string, true);
$countries = [];
foreach ($json as $row) {
$countries['iso2'][strtolower($row['Name'])] = $row['ISO3166-1-Alpha-2'];
$countries['iso3'][strtolower($row['Name'])] = $row['ISO3166-1-Alpha-3'];
foreach ($json as $row => $value) {
$countries[strtolower($row)] = strtoupper($value);
}
return $countries;
}
Expand Down
Loading
Loading