daniel-km / simple-iso-639-3
Convert two and three letters language codes and names from official standard sources.
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^9
README
Simple ISO 639-3 is a small library to convert languages codes between ISO 639-1 (two letters language codes) and ISO 639-2 and 639-3 (three letters). It is built from the official standard lists ISO 639-3 of (SIL) and is compatible with IETF language tags (BCP 47, improvment of RFC 4646). Languages names are available in native language, English and French.
Note: The current standard (2007) uses the native language as a base for the
codes. For example, three letters code for French is fra, not fre, or, for
Chinese, zho, not chi. English language is deprecated.
Every language is identified by a canonical three letters code, but only 184 of the 7929 languages have a two letters code. So there are three ways to get a code:
code3letters()always returns the canonical three letters code. All the lists of names are keyed by it.code()returns the shortest code: the two letters one when the language has one, else the three letters one.code2letters()returns the two letters code, or an empty string when the language has none.
Warning: until version 0.2, code() always returned the three letters code, and
code3letters() was an alias of it. Replace code() by code3letters() to keep
the previous result.
This library is used in the module Internationalisation for Omeka S and some other places.
The list of codes may be updated by SIL, so some codes may be removed and some other ones may be added regularly.
Installation
This module is a composer library available on [packagist]:
composer require daniel-km/simple-iso-639-3
Usage
Once included in your code via composer or with require_once 'path/to/vendor/daniel-km/simple-iso-639-3/src/Iso639p3.php;',
you can use it like that:
$languages = [
'fr',
'fra',
'fre',
'fr_FR',
'fr-FR',
'French',
'frAnÇaiS',
'frm',
'fro',
'aaa',
'fxxx',
];
$result = [];
foreach ($languages as $language) {
$result[$language] = [
'language' => $language,
'code' => \Iso639p3\Iso639p3::code($language),
'code3letters' => \Iso639p3\Iso639p3::code3letters($language),
'short' => \Iso639p3\Iso639p3::code2letters($language),
'all' => \Iso639p3\Iso639p3::codes($language),
'name' => \Iso639p3\Iso639p3::name($language),
'English name' => \Iso639p3\Iso639p3::englishName($language),
'English inverted name' => \Iso639p3\Iso639p3::englishInvertedName($language),
'French name' => \Iso639p3\Iso639p3::frenchName($language),
'French inverted name' => \Iso639p3\Iso639p3::frenchInvertedName($language),
];
}
print_r($result);
Result:
| language | code | code3letters | short | all | name | English name | English inverted name | French name | French inverted name |
|---|---|---|---|---|---|---|---|---|---|
| fr | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| fra | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| fre | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| fr_FR | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| fr-FR | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| French | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| frAnÇaiS | fr | fra | fr | fr, fra, fre | Français | French | French | français | français |
| frm | frm | frm | frm | François | Middle French (ca. 1400-1600) | French, Middle (ca. 1400-1600) | moyen français | français moyen (1400-1600) | |
| fro | fro | fro | fro | Franceis | Old French (842-ca. 1400) | French, Old (842-ca. 1400) | ancien français | français ancien (842-ca.1400) | |
| aaa | aaa | aaa | aaa | Ghotuo | Ghotuo | ||||
| fxxx |
Notes:
codes()returns the two letters code first, then the three letters ones, terminologic and bibliographic.- The case is not significant, and a locale or a IETF language tag is accepted:
fr_FR,fr-FRandfr-CAall resolve to French. - The alternate names of the standard are accepted as input, even if they are not
displayed: only the reference name is, so
Flemishresolves tonld, whose English name isDutch. A name is searched before cutting a language tag, because some names cannot be distinguished from a tag, likeAka-Bo. - A code always wins over a name:
Boois the code of Banka, not the alternate name of Bua. Some names are used by two languages, so the first one wins. - Only the English names are available for all the languages. The native name is
set for 405 languages and the French one for 491, out of 7929, so
name(),frenchName()andfrenchInvertedName()return an empty string for most of them, as foraaaabove. - The names keep the case of the source, so the native name of French is
Français, with a capital, and the French name isfrançais. - Every method returns an empty string when the language does not exist, and
codes()returns an empty array.
Development
The lists are automatically generated from this command:
php -f scripts/generate.php
The codes and the names are fetched online, from SIL and the
Library of Congress, so the command requires an internet connection. Any data
can be added or fixed with the file scripts/extra_codes.php.
Run the tests with:
composer install
vendor/bin/phpunit
Warning
Use it at your own risk.
It’s always recommended to backup your files and your databases and to check your archives regularly so you can roll back if needed.
Troubleshooting
See online issues page on GitLab.
License
Module
This module is published under the CeCILL v2.1 license, compatible with GNU/GPL and approved by FSF and OSI.
This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, modify and/ or redistribute the software under the terms of the CeCILL license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license, users are provided only with a limited warranty and the software’s author, the holder of the economic rights, and the successive licensors have only limited liability.
In this respect, the user’s attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the software’s suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions as regards security.
The fact that you are presently reading this means that you have had knowledge of the CeCILL license and that you accept its terms.
Copyright
- Copyright The Internet Society (2005) (RFC 4646)
- Copyright IETF Trust (2009) (RFC 5646)
- Copyright http://www.iso639-3.sil.org (language codes)
- Copyright Daniel Berthereau, 2019-2023 (see Daniel-KM on GitLab)