项目作者: sokil

项目描述 :
:star: 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)
高级语言: PHP
项目地址: git://github.com/sokil/php-isocodes.git
创建时间: 2014-05-24T09:28:17Z
项目社区:https://github.com/sokil/php-isocodes

开源协议:MIT License

下载


Stand With Ukraine 🇺🇦

SWUbanner


PHP ISO Codes

Continuous integration
Latest Stable Version
Coverage Status
Total Downloads
Daily Downloads

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

:package: Based on Python’s pycountry and Debian’s iso-codes.

:tongue: 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

  1. 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

  1. composer require sokil/php-isocodes-db-only

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

You can install library through Composer:

  1. 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.

  1. ./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:

  1. <?php
  2. $databaseBaseDir = '/var/isocodes';
  3. $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.

  1. <?php
  2. // gettext driver
  3. $isoCodes = new IsoCodesFactory();
  4. $isoCodes = new IsoCodesFactory(null, new GettextExtensionDriver());
  5. // symfony driver
  6. $driver = new SymfonyTranslationDriver();
  7. $driver->setLocale('uk_UA');
  8. $isoCodes = new IsoCodesFactory(
  9. null,
  10. $driver
  11. );
  12. // dummy driver
  13. $isoCodes = new IsoCodesFactory(
  14. null,
  15. new DummyDriver()
  16. );

Gettext extension driver

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

  1. <?php
  2. // gettext driver
  3. $isoCodes = new IsoCodesFactory();
  4. $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.

  1. <?php
  2. // define locale
  3. putenv('LANGUAGE=uk_UA.UTF-8');
  4. putenv('LC_ALL=uk_UA.UTF-8');
  5. setlocale(LC_ALL, 'uk_UA.UTF-8');
  6. // init database
  7. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  8. // get languages database
  9. $languages = $isoCodes->getLanguages();
  10. // get local name of language
  11. echo $languages->getByAlpha2('uk')->getLocalName(); // will print 'українська'

To get list of available locales, execute under console:

  1. $ locale -a
  1. uk_UA
  2. uk_UA.koi8u
  3. uk_UA.utf8

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

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

Symfony Translation driver

  1. <?php
  2. $driver = new SymfonyTranslationDriver();
  3. $driver->setLocale('uk_UA');
  4. $isoCodes = new IsoCodesFactory(
  5. null,
  6. $driver
  7. );

Dummy driver

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

  1. <?php
  2. $isoCodes = new IsoCodesFactory(
  3. null,
  4. new DummyDriver()
  5. );

Usage

Factory

All databases may be created through factory:

  1. <?php
  2. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  3. $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)

Country contains next names:

Name Description Example
Name Required Moldova, Republic of
Official Name Optional Republic of Moldova
Common Name Optional Moldova
Localised Name Optional Молдова

This names may be get from country entity:

  1. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  2. $country = $isoCodes->getCountries()->getByAlpha2('UA');
  3. echo $country->getName();
  4. echo $country->getLocalName();
  5. echo $country->getOfficialName();
  6. echo $country->getCommonName();

Also you may get flag of country:

  1. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  2. $country = $isoCodes->getCountries()->getByAlpha2('UA');
  3. echo $country->getFlag();

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

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

Get localized name of country by it’s alpha3 code:

  1. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  2. $isoCodes->getCountries()->getByAlpha3('UKR')->getLocalName();

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

  1. $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
  2. $isoCodes->getCountries()->getByNumericCode('804')->getLocalName();

Get localised list of countries

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

Subdivisions database (ISO 3166-2)

  1. <?php
  2. $isoCodes = new IsoCodesFactory();
  3. $subDivisions = $isoCodes->getSubdivisions();
  4. // get subdivision by code
  5. $subDivision = $subDivisions->getByCode('UA-43');
  6. // get subdivision code
  7. $subDivision->getCode(); // UA-43
  8. // get subdivision name
  9. $subDivision->getName(); // Respublika Krym
  10. // get localised subdivision name
  11. $subDivision->getLocalName(); // Автономна Республіка Крим
  12. // get subdivision type
  13. $subDivision->getType(); // 'Autonomous republic'

Historic countries database (ISO 3166-3)

  1. <?php
  2. $isoCodes = new IsoCodesFactory();
  3. $countries = $isoCodes->getHistoricCountries();
  4. $country = $countries->getByAlpha4('ZRCD');
  5. $country->getName(); //'Zaire, Republic of'
  6. $country->getAlpha4(); // 'ZRCD'
  7. $country->getAlpha3(); // 'ZAR'
  8. $country->getAlpha2(); // 'ZR'
  9. $country->getWithdrawalDate(); // '1997-07-14'
  10. $country->getNumericCode(); // 180

Scripts database (ISO 15924)

  1. <?php
  2. $isoCodes = new IsoCodesFactory();
  3. $scripts = $isoCodes->getScripts();
  4. $script = $scripts->getByAlpha4('Aghb');
  5. $script->getName(); // Caucasian Albanian
  6. $script->getLocalName(); // кавказька албанська
  7. $script->getAlpha4(); // Aghb
  8. $script->getNumericCode(); 239

Currencies database (ISO 4217)

  1. <?php
  2. $isoCodes = new IsoCodesFactory();
  3. $currencies = $isoCodes->getCurrencies();
  4. $currency = $currencies->getByLetterCode('CZK');
  5. $currency->getName(); // Czech Koruna
  6. $currency->getLocalName(); // Чеська крона
  7. $currency->getLetterCode(); // CZK
  8. $currency->getNumericCode(); // 203

Languages database (ISO 639-3)

  1. <?php
  2. $isoCodes = new IsoCodesFactory();
  3. $languages = $isoCodes->getLanguages();
  4. $language = $languages->getByAlpha2('uk');
  5. $language->getAlpha2(); // uk
  6. $language->getName(); // Ukrainian
  7. $language->getLocalName(); // українська
  8. $language->getAlpha3(); // ukr
  9. // Scope of denotation, see mote at https://iso639-3.sil.org/about/scope
  10. $language->getScope(); // I
  11. // Type of language, see https://iso639-3.sil.org/about/types
  12. $language->getType(); // L
  13. $language->getInvertedName(); // null

Tests

To start docker tests run following command:

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

For example for PHP 7.1 run following command:

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

See also