Skip to content

A PHP-7 language codes converter, from and to the most common formats (ISO or not)

License

Notifications You must be signed in to change notification settings

whitecube/lingua

Repository files navigation

Lingua - Language Codes Converter

Travis Test Coverage GitHub release Packagist GitHub issues GitHub pull requests license

This package will convert languages from and to some common formats (ISO codes, W3C standards, PHP localization strings), including human-readable strings.

Installation

composer require whitecube/lingua

Content

The package currently supports over 220 languages. Most living languages are included, with country codes, names and possible alphabet sets (Latin, Cyrillic, Arabic, ...).

Available input and output formats are:

  1. name: The english name of the language (most of the time).
  2. native: The autonym of the language.
  3. ISO-639-1: The official two-letter code for the language. Some languages do not have this code.
  4. ISO-639-2t: An official three-letter code for terminology applications (ISO 639-2/T) for the language.
  5. ISO-639-2b: An official three-letter code for bibliographic applications (ISO 639-2/B) for the language.
  6. ISO-639-3: The official three-letter code. This is also the most common terminology and therefore the most complete language representation. In most cases, this is the same format as ISO-639-2t, except for macrolanguages.
  7. W3C: A valid string as described by the BCP 47 specification (used in the W3C's language attributes recommendations).
  8. PHP: A string with the appropriate format for PHP's setlocale(). This does not check if the locale is available on your server.

Note on macrolanguages: The ISO-639-3 output will also indicate the amount of sub-languages represented with the same ISO code.

Usage

The Lingua converter works in two stages: first you'll need to instantiate it by providing the original format, than you can convert this string as many times as you want to any of the available formats.

Note: you can instanciate some languages with their deprecated ISO code. For example, you can instanciate Hebrew with the ISO 639_1 code "iw" instead of "he".

Setters

use WhiteCube\Lingua\Service as Lingua;

// Create a converter, without knowing the original format (this will try to guess it for you)
$language = Lingua::create('en_GB');

// Create a converter from a language name
$language = Lingua::createFromName('french');
$language = (new Lingua())->fromName('german');

// Create a converter from a language's native name
$language = Lingua::createFromNative('nederlands');
$language = (new Lingua())->fromNative('magyar');

// Create a converter from a ISO 639-1 code
$language = Lingua::createFromISO_639_1('mk');
$language = (new Lingua())->fromISO_639_1('ko');

// Create a converter from a ISO 639-2t code
$language = Lingua::createFromISO_639_2t('heb');
$language = (new Lingua())->fromISO_639_2t('gle');

// Create a converter from a ISO 639-2b code
$language = Lingua::createFromISO_639_2b('her');
$language = (new Lingua())->fromISO_639_2b('iku');

// Create a converter from a ISO 639-3 code
$language = Lingua::createFromISO_639_3('aze + 2');
$language = (new