Skip to content

Commit 29d1156

Browse files
committed
Merge pull request #64 from mlocati/improve-locales
Add Locales::getLanguages and Locales::getTerritories
2 parents 6723b68 + b14f6b6 commit 29d1156

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/Utils/Locales.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,4 +1124,60 @@ public static function getLocaleInfo($code)
11241124

11251125
return $result;
11261126
}
1127+
1128+
/**
1129+
* Returns the list of languages
1130+
*
1131+
* @param bool $excludeCompounds Exclude compounds languages (eg 'Brazilian Portuguese', 'Traditional Chinese')?
1132+
* @param bool $excludeFake Exclude fake languages (eg 'Unknown Language', 'No linguistic content')?
1133+
*
1134+
* @return array
1135+
*/
1136+
public static function getLanguages($excludeCompounds = false, $excludeFake = false)
1137+
{
1138+
$result = static::$languageNames;
1139+
if ($excludeCompounds) {
1140+
$keysToKeep = array_filter(
1141+
array_keys($result),
1142+
function ($key) {
1143+
return (strpos($key, '_') === false) ? true : false;
1144+
}
1145+
);
1146+
$result = array_intersect_key($result, array_flip($keysToKeep));
1147+
}
1148+
if ($excludeFake) {
1149+
unset($result['und']);
1150+
unset($result['zxx']);
1151+
}
1152+
1153+
return $result;
1154+
}
1155+
1156+
/**
1157+
* Returns the list of territories
1158+
*
1159+
* @param bool $excludeRegions Exclude the regions (eg 'World', 'Europe')?
1160+
* @param bool $excludeFake Exclude fake territories (eg 'Unknown Region')?
1161+
*
1162+
* @return array
1163+
*/
1164+
public static function getTerritories($excludeRegions = false, $excludeFake = false)
1165+
{
1166+
$result = static::$territoryNames;
1167+
if ($excludeRegions) {
1168+
unset($result['EU']);
1169+
$keysToKeep = array_filter(
1170+
array_keys($result),
1171+
function ($key) {
1172+
return preg_match('/^\d{3}$/', $key) ? false : true;
1173+
}
1174+
);
1175+
$result = array_intersect_key($result, array_flip($keysToKeep));
1176+
}
1177+
if ($excludeFake) {
1178+
unset($result['ZZ']);
1179+
}
1180+
1181+
return $result;
1182+
}
11271183
}

0 commit comments

Comments
 (0)