• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⭐ PHP library providing ISO codes with localization: country (ISO 3166-1), subdivision (ISO 3166-2), language (ISO 639-3), currency (ISO 4217) and scripts (ISO 15924)

Stand With Ukraine

SWUbanner


PHP ISO Codes

Continuous integration Latest Stable Version Coverage Status Total Downloads Daily Downloads

This library used to get localized names of countries, currencies, languages and scripts.

📦 Based on Python's pycountry and Debian's iso-codes.

👅 Current translation status: https://salsa.debian.org/iso-codes-team/iso-codes#status-of-translations

Table of contents

ISO Standards

  • ISO 3166-1: Country codes (alpha-2, alpha-3, numeric)
  • ISO 3166-2: Principal subdivisions (e.g., provinces or states) of all countries coded in ISO 3166-1
  • ISO 3166-3: Historic countries (alpha-2, alpha-3, alpha-4, numeric)
  • ISO 15924: Scripts
  • ISO 4217: Currencies
  • ISO 639-3: Languages

Installation

You may use this library in different modes:

  • sokil/php-isocodes (this library) - install library without database and messages and setup periodic updates of database and messages by yourself with cron or inside CI/CD pipeline with ./bin/update_iso_codes_db.sh
  • sokil/php-isocodes-db-only - if you do not need internationalisation, use this library. Database already inside. To update database just periodically update this library.
  • sokil/php-isocodes-db-i18n - if you need internationalisation, use this library. Database and messages already inside. To update database just periodically update this library.

💾 Library with included database and localization

To install library with database and i18n:

Latest Stable Version Total Downloads Daily Downloads

composer require sokil/php-isocodes-db-i18n

💾 Library with included database and without localization

You may also install library with only database (no i18n will be available):

Latest Stable Version Total Downloads Daily Downloads

composer require sokil/php-isocodes-db-only

💾 Library without database and localization, requires manual database installation and updates

You can install library through Composer:

composer require sokil/php-isocodes

Database and gettext files located in related packages inside databases and messages directories. This packages periodically updated with package version increment.

If you want to update database, use script ./bin/update_iso_codes_db.sh. Call this script by cron, during deploy process or when build your docker image.

./bin/update_iso_codes_db.sh {mode} {base_dir} {build_dir}
Argument Required Description
mode Required May be "all" or "db_only". In "all" mode update database (json files) and locallisation (po and mo files), in "db_only" only database will update
base_dir Required Dir where to place database and messages
build_dir Optional. Default: "/tmp/iso-codes-build" Dir where source directory cloned and files original files processed.

Now you need to configure factory to use this directory:

<?php

$databaseBaseDir = '/var/isocodes';

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory($databaseBaseDir);

Translation drivers

Translation drivers required when need to get local names of iso entities.

Translation driver must implement Sokil\IsoCodes\TranslationDriver\TranslationDriverInterface.

Instance of driver may be passed to IsoCodesFactory. If it not passed, default GettextExtensionDriver will be used.

<?php

// gettext driver
$isoCodes = new IsoCodesFactory();
$isoCodes = new IsoCodesFactory(null, new GettextExtensionDriver());

// symfony driver
$driver = new SymfonyTranslationDriver();
$driver->setLocale('uk_UA');

$isoCodes = new IsoCodesFactory(
    null,
    $driver
);

// dummy driver
$isoCodes = new IsoCodesFactory(
    null,
    new DummyDriver()
);

Gettext extension driver

This is default translation driver. It requires ext-gettext.

<?php

// gettext driver
$isoCodes = new IsoCodesFactory();
$isoCodes = new IsoCodesFactory(null, new GettextExtensionDriver());

Locale configuration

Before using IsoCodes database you need to setup valid locale to get translations worked, because ext-gettext uses system local, configured by setlocale.

<?php

// define locale
putenv('LANGUAGE=uk_UA.UTF-8');
putenv('LC_ALL=uk_UA.UTF-8');
setlocale(LC_ALL, 'uk_UA.UTF-8');

// init database
$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();

// get languages database
$languages = $isoCodes->getLanguages();

// get local name of language
echo $languages->getByAlpha2('uk')->getLocalName(); // will print 'українська'

To get list of available locales, execute under console:

$ locale -a
uk_UA
uk_UA.koi8u
uk_UA.utf8

If you don't see required locales in list, you may install them manually (for Ubuntu):

$ locale-gen uk_UA.utf8
Generating locales...
  uk_UA.utf-8... done
Generation complete.

Symfony Translation driver

<?php

$driver = new SymfonyTranslationDriver();
$driver->setLocale('uk_UA');

$isoCodes = new IsoCodesFactory(
    null,
    $driver
);

Dummy driver

This driver may be used, when localisation of names does not require, and only database of codes is required.

<?php

$isoCodes = new IsoCodesFactory(
    null,
    new DummyDriver()
);

Usage

Factory

All databases may be create through factory:

<?php
$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$languages = $isoCodes->getLanguages();

There are large databases: subdivisions and languages. Loading of entire database into memory may require lot of RAM and time to create all entries in memory.

So there are scenarios of usage: with optimisations of memory and with optimisation of time.

Memory optimisation

Database splits into partition files.

Fetching some entry will load only little part of database. Loaded entries not stored statically.

This scenario may be useful when just few entries need to be loaded, for example on web request when one entry fetched.

This may require a lot of file read operations.

Input-output optimisations

Entire database loaded into memory from single JSON file once.

All entries created and stored into RAM. Next read of save entry will just return it without io operations with files and building objects.

This scenario may be useful for daemons to decrease file operations, or when most entries will be fetched from database.

This may require a lot of RAM for storing all entries.

Countries database (ISO 3166-1)

Get localized name of country by it's alpha2 code:

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$isoCodes->getCountries()->getByAlpha2('UA')->getLocalName();

Get localized name of country by it's alpha2 code:

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$isoCodes->getCountries()->getByAlpha2('UKR')->getName();

Get localized name of country by it's numeric code:

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$isoCodes->getCountries()->getByAlpha2('804')->getName();

Get localised list of countries

$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
foreach($isoCodes->getCountries() as $country) {
  echo $country->getLocalName();
}

Subdivisions database (ISO 3166-2)

<?php

$isoCodes = new IsoCodesFactory();

$subDivisions = $isoCodes->getSubdivisions();

// get subdivision by code
$subDivision = $subDivisions->getByCode('UA-43');

// get subdivision code
$subDivision->getCode(); // UA-43

// get subdivision name
$subDivision->getName(); // Respublika Krym

// get localised subdivision name
$subDivision->getLocalName(); // Автономна Республіка Крим

// get subdivision type
$subDivision->getType(); // 'Autonomous republic'

Historic countries database (ISO 3166-3)

<?php

$isoCodes = new IsoCodesFactory();

$countries = $isoCodes->getHistoricCountries();

$country = $countries->getByAlpha4('ZRCD');

$country->getName(); //'Zaire, Republic of'
$country->getAlpha4(); // 'ZRCD'
$country->getAlpha3(); // 'ZAR'
$country->getAlpha2(); // 'ZR'
$country->getWithdrawalDate(); // '1997-07-14'
$country->getNumericCode(); // 180

Scripts database (ISO 15924)

<?php
$isoCodes = new IsoCodesFactory();

$scripts = $isoCodes->getScripts();

$script = $scripts->getByAlpha4('Aghb');

$script->getName(); // Caucasian Albanian
$script->getLocalName(); // кавказька албанська
$script->getAlpha4(); // Aghb
$script->getNumericCode(); 239

Currencies database (ISO 4217)

<?php

$isoCodes = new IsoCodesFactory();

$currencies = $isoCodes->getCurrencies();

$currency = $currencies->getByLetterCode('CZK');

$currency->getName(); // Czech Koruna
$currency->getLocalName(); // Чеська крона
$currency->getLetterCode(); // CZK
$currency->getNumericCode(); // 203

Languages database (ISO 639-3)

<?php
$isoCodes = new IsoCodesFactory();

$languages = $isoCodes->getLanguages();

$language = $languages->getByAlpha2('uk');

$language->getAlpha2(); // uk

$language->getName(); // Ukrainian

$language->getLocalName(); // українська

$language->getAlpha3(); // ukr

// Scope of denotation, see mote at https://iso639-3.sil.org/about/scope
$language->getScope(); // I

// Type of language, see https://iso639-3.sil.org/about/types
$language->getType(); // L

$language->getInvertedName(); // null

Tests

To start docker tests run following command:

./tests/docker/run-test.sh [PHP_VERSION]

For example for PHP 7.1 run following command:

./tests/docker/run-test.sh 7.1

See also

More Repositories

1

php-mongo

MongoDB ODM. Part of @PHPMongoKit
PHP
242
star
2

php-vast

⭐ Generating and parsing VAST documents
PHP
81
star
3

php-mongo-migrator

Migrations of MongoDB. Part of @PHPMongoKit
PHP
29
star
4

koatuu

Державний класифікатор об'єктів адміністративно-територіального устрою України (КОАТУУ)
Python
27
star
5

php-concurrency-labs

PHP Concurency research
PHP
16
star
6

go-connection-pool

Connection pool is a thread safe list of net.Conn
Go
12
star
7

php-bitmap

Bitmap representation with bitwise operations
PHP
11
star
8

php-fraud-detect

Checker of flood requests
PHP
10
star
9

laravel-mongo-odm

Laravel adapter for PHPMongo ODM. Part of @PHPMongoKit
PHP
6
star
10

php-image

Image manipulation library (resize, filtering, combine, watermarks)
PHP
5
star
11

php-isocodes-db-i18n

PHP
4
star
12

php-viber-notifier

Broadcast notification through Viber to subscribed users
PHP
4
star
13

php-merchant-product-feed

Builder of Facebook and Google product feeds
PHP
4
star
14

php-acestream

ACEStream p2p player widget
JavaScript
4
star
15

php-library-starter-kit

Starter kit for creating composer compatible library
PHP
4
star
16

php-state

Finite State Machine for PHP
PHP
4
star
17

statsd-http-proxy

ABANDONED! Go to https://github.com/GoMetric/statsd-http-proxy. StatsD HTTP proxy with REST interface for using in browsers
Go
4
star
18

php-mongo-bundle

MongoDB Symfony Bundle. Part of @PHPMongoKit
PHP
4
star
19

DeployBundle

Symfony application deploy bundle. Full stack deply task management tool
PHP
3
star
20

php-debug-utils

Debug utils for PHP
Shell
3
star
21

CommandBusBundle

Yes, this is yet another command bus
PHP
3
star
22

php-rest

Framework to build SDK for interacting with RESTful services
PHP
3
star
23

php-opengraph

Manage Facebook's OpenGraph or your page
PHP
3
star
24

php-isocodes-db-only

Database for ISO country, subdivision, language, currency and script definitions and their translations. Based on pythons pycountry and Debian's iso-codes.
PHP
3
star
25

ChromedIngress

Google Chrome extension, that implement some additional functions to Ingress Intel interface
JavaScript
2
star
26

FileStorageBundle

Write files to external file system and store metadata to database
PHP
2
star
27

php-diff

Highlight diffs provided by Sebastian Bergmann diff
PHP
2
star
28

DockerSentry

A simple docker compose to install and run Sentry
Shell
2
star
29

php-list

Priority list
PHP
2
star
30

php-upload

PHP uploader
PHP
2
star
31

DockerDevelopmentEnvironment

Builder of development environment based on Docker
Shell
2
star
32

PhotoGallery

Photo Gallery based on XML configs
PHP
2
star
33

upload.js

JavaScript upload handler with easy ui customization
JavaScript
2
star
34

light-toggle-firebase-lambda

Google Lambda that notifies to Telegram on toggle electricity by pinging some device
JavaScript
2
star
35

id-bench

Database identifier benchmarks
PHP
2
star
36

opendata.ua

Відкриті дані, довідники, реєстри України
2
star
37

browser-communication-labs

Server to browser communication examples
HTML
2
star
38

knowledge-list

Knowledge database
2
star
39

akka-labs

Experiments with Akka. Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala
Java
1
star
40

php-cardnumber

Validating of bank card number
PHP
1
star
41

pubsub-server

Publish/Subscribe server in Golang which broadcast messages to all other connected clients
Go
1
star
42

DistributiveManager

Distributive manager able to upload, manage and show information about distributives
Python
1
star
43

go-statsd-client

ABANDONED! Go to https://github.com/GoMetric/go-statsd-client. Client for StatsD (Golang)
Go
1
star
44

VotingEngine

Voting platform
Python
1
star
45

php-worker

Experiments with processes, daemonization and multitasking
PHP
1
star
46

php-telegram-bot-starter-kit

(ALPHA) Fully featured infrastructure to organize your telegram bot. Just add your dialogs.
PHP
1
star
47

esp8266

1
star
48

OpenAdvert

Advetising platform (VAST supported)
PHP
1
star
49

DockerELKStack

Elasticsearch, Logstash and Kibana Test Laboratory
1
star
50

php-clickhouse

PHP
1
star
51

CorsBundle

Handling CORS Requests
PHP
1
star
52

FrontendBundle

Single Page Application for Symfony with Backbone, Marionette and Twitter Bootstrap inside
JavaScript
1
star
53

PackagistDashboard

⭐ Tool to get statistics of packagist libraries
JavaScript
1
star