Localization and Country Regions
I was recently assigned a task in my current job, to try and standardize address related data.
My approach was to use the ISO codes for countries and ISO codes for regions. A region is defined as the geographical split of areas in a country. For instance in the US regions are called states, in Canada provinces etc.
I have used several sources on the internet but my main source was Wikipedia.
In order to complete this task I used Zend Locale to retrieve the available countries and using the ISO codes (keys of the returned data).
My method is:
/**
* Gets the countries based on a passed (or default) locale
*
* @param string $locale
* @param boolean $sort
*/
public static function getCountries($locale = NULL, $sort = TRUE)
{
$results = parent::getTranslationList('territory', $locale, 2);
if ($sort)
{
asort($results);
}
return $results;
}
Having the list of countries and their ISO codes, allowed me to have a proper storage of country related data in my addresses table. The use of a select box in the view layer ensures that I keep this standardization in place.
Extending that, I wanted to have an easy way to access the region related data, without having to hit the database. I therefore used JSON encoded files that contain an array with the type of the region as the key and Region ISO Code => Region Name as the array elements. Again I used a select box for that in the view layer, which is automatically refreshed when a new country is selected.
So for instance for the US the JSON file is as follows:
{
"state":{
"US-AL":"Alabama",
"US-AK":"Alaska",
...
"US-WV":"West Virginia",
"US-WI":"Wisconsin",
"US-WY":"Wyoming"
}
}
where the key defines the region - in this case a State, and the array elements are the relevant data. In some instances I wanted to give a bit more selection and visual aids to the end user. I therefore have several sub arrays denoted by optiongroup tags for the relevant select box showing the regions.
In the case of Great Britain:
{
"county":{
"England":{
"GB-BKM":"Buckinghamshire",
"GB-CAM":"Cambridgeshire",
...
"GB-WOK":"Wokingham",
"GB-YOR":"York"
},
"Northern Ireland":{
"GB-ANT":"Antrim",
"GB-ARD":"Ards",
...
"GB-OMH":"Omagh",
"GB-STB":"Strabane"
},
"Scotland":{
"GB-ABE":"Aberdeen City",
"GB-ABD":"Aberdeenshire",
...
"GB-WDU":"West Dunbartonshire",
"GB-WLN":"West Lothian"
},
"Wales":{
"GB-BGW":"Blaenau Gwent",
"GB-BGE":"Bridgend (Pen-y-bont ar Ogwr)",
...
"GB-VGL":"Vale of Glamorgan (Bro Morgannwg)",
"GB-WRX":"Wrexham (Wrecsam)"
}
}
}
So with simple commands as file_get_contents
and json_decode
(or using Zend_Json::decode
) I was able to have a flexible system that would load standardized address related data (regions and countries).
You can download the list of JSON files (one per country) from my Github Repo.
The list I have there is by no means complete, but it does have a lot of data that one can use. Additions are always welcome :)
Enjoy!
-
Nikolaos Dimopoulos
Boldly goes where no other coder has gone before.... and other ramblings
Recent Posts
-
Setting up Docker for Qubes OS
2024-10-05 -
PhpStorm cannot create scratch files
2023-12-07 -
PHP 8.2 Deprecation of Dynamic Properties
2023-07-18 -
New Look
2023-06-12 -
Linux Swap file in RAM
2023-04-17
Tag Cloud
-
amazon (3)
android (1)
angularjs (7)
apps (1)
aurora (1)
aws (1)
backup (2)
bash (1)
bitbucket (1)
blog (2)
books (1)
bootstrap (1)
buzz (1)
cPanel (1)
cache (1)
celebrations (4)
chromium (3)
chromium os (3)
cloud computing (3)
codacy (1)
codecov (1)
communications (1)
composer (1)
conversion (1)
copy (1)
degoogle (5)
design (1)
design patterns (3)
discord (1)
docker (1)
docs (3)
documentation (1)
ec2 (3)
emerge (1)
encoding (1)
factory (1)
froyo (1)
fujitsu (1)
gentoo (7)
git (3)
github (2)
gmail (3)
google (16)
google apps (4)
google maps (1)
gource (1)
ha (1)
hosting (2)
how to (36)
igbinary (1)
information (5)
input (1)
installation (6)
internet (1)
iphone (1)
json (2)
libreoffice (1)
linux (13)
localization (1)
lts (1)
mariadb (1)
memorial day (1)
metrics (1)
migration (1)
mod_rewrite (1)
mov (1)
mp4 (1)
mysql (6)
nas (1)
netlify (1)
new look (1)
nexus one (2)
nfs (1)
notebook (1)
online storage (1)
openoffice (1)
opinion (1)
oracle (1)
patterns (1)
payroll (1)
performance (3)
personal (9)
phalcon (12)
php (23)
php8 (2)
php82 (1)
phpstorm (1)
phpunit (2)
picasa (2)
portage (1)
privacy (1)
programming (9)
proxy (1)
qubes os (1)
rant (5)
rdbms (1)
rds (1)
relationships (1)
release (1)
remove (1)
replication (1)
review (9)
rsync (2)
s1300 (1)
scan (1)
scratch (1)
serialize (1)
series (9)
singleton (1)
sorting (1)
spaceship (1)
spam (1)
ssl (1)
static (1)
storage (6)
submodules (1)
subversion (2)
svn (1)
swap (1)
tdd (1)
technorati (1)
test driven development (1)
testability (1)
testing (2)
titles (1)
traits (1)
ua (1)
ubuntu (1)
update (6)
upgrade (1)
usa (2)
usort (1)
utf8 (1)
video (1)
visualization (1)
vps (1)
webm (1)
website (1)
wget (1)
zend framework (4)
zram (1)
zstd (1)