For the last couple of months, I was working on a large project in which I needed to use the list of countries in both English and Arabic languages. At first glance, I thought finding this list was going to be easy as it looks like a common problem, right? But unfortunately, it wasn't.
Most of the data I've found weren't organized properly, and it was too hard to extract the necessary data without putting in a huge effort.
I decided to use some API having this data already, but I faced some problems. Most of the APIs provide you with a limited number of requests per month. If you want more requests, you've to pay more and more. Unfortunately, the free ones weren't reliable.
Despite all of that, I had two major problems. The first one, we may have situations in which we want to relate some data to some countries in our database with foreign keys. So, the APIs won't be useful, and I have to store the data in the database. For the second one, all the solutions I've found weren't providing Arabic data.
So, I decided to build the data on my own. With the help of GeoNames public data and with some extra effort to translate the data and add extra fields, I created amrsaeedhosny/countries repository on GitHub.
Country Data
Each country has the following data:
- English Name.
- Arabic Name.
- ISO 3166-1 Alpha-2 Code.
- ISO 3166-1 Alpha-3 Code.
- Phone Code.
Available File Formats
The countries data are stored in multiple file formats:
- CSV:
English Name | Arabic Name | Alpha-2 Code | Alpha-3 Code | Phone Code |
---|---|---|---|---|
Egypt | مصر | EG | EGY | 20 |
- JSON:
{
"english_name": "Egypt",
"arabic_name": "مصر",
"alpha2_code": "EG",
"alpha3_code": "EGY",
"phone_code": "20"
}
- SQL:
INSERT INTO COUNTRY (ENGLISH_NAME, ARABIC_NAME, ALPHA2_CODE, ALPHA3_CODE, PHONE_CODE) VALUES('Egypt', 'مصر', 'EG', 'EGY', '20');
Custom File Format
There is also a simple python script that you can use to build your own file format:
import csv
my_file = open("my_file.txt", "w")
with open('countries.csv') as csv_file:
csv_reader = csv.reader(csv_file)
csv_reader.next()
for row in csv_reader:
for column in row:
my_file.write(column + ",")
my_file.write("\n")
my_file.close()
Now the problem has been solved once, and I can use the data easily whenever I need it.
By reaching this, you've to know that you're free to use this data in your own projects. Actually, that was the whole point of this article. So, enjoy!
Top comments (0)