PHP code example of whitecube / lingua

1. Go to this page and download the library: Download whitecube/lingua library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

whitecube / lingua example snippets


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 Lingua())->fromISO_639_3('asm');

// Create a converter from a valid W3C language string
$language = Lingua::createFromW3C('ae');
$language = (new Lingua())->fromW3C('zh-hans-SG');

// Create a converter from a valid PHP localization string
$language = Lingua::createFromPHP('fr_BE');
$language = (new Lingua())->fromPHP('kk_Cyrl_KZ');

use WhiteCube\Lingua\Service as Lingua;

$language = Lingua::createFromNative('français');

// Format a language in a human readable string (the language's english name)
echo $language->toName(); // "french"

// Format a language in its native form
echo $language->toNative(); // "français"

// Format a language in a ISO 639-1 string
echo $language->toISO_639_1(); // "fr"

// Format a language in a ISO 639-2t string
echo $language->toISO_639_2t(); // "fra"

// Format a language in a ISO 639-2b string
echo $language->toISO_639_2b(); // "fre"

// Format a language in a ISO 639-3 string
echo $language->toISO_639_3(); // "fra"

// Format a language in a valid W3C language attribute string (according to BCP 47)
echo $language->toW3C(); // "fr" in this case but could be "fr-BE" if country code was specified

// Format a localization string for PHP's setlocale()
echo $language->toPHP(); // "fr" in this case but could be "fr_BE" if country code was specified

use WhiteCube\Lingua\Service as Lingua;

echo Lingua::createFromName('italian'); // "it"

use WhiteCube\Lingua\Service as Lingua;

Lingua::setFormat('native');
$language = Lingua::createFromISO_639_3('ita');
echo $language; // "italiano"

Lingua::setFormat('iso-639-1');
echo $language; // "it"

use WhiteCube\Lingua\Service as Lingua;

$language = new Lingua('native');
$language->fromISO_639_1('ks');
echo $language; // "कश्मीरी, كشميري‎"

// Trying to change the default formatting
Lingua::setFormat('name');

echo $language; // still "कश्मीरी, كشميري‎"
echo $language->toName(); // "kashmiri"

use WhiteCube\Lingua\Service as Lingua;

echo Lingua::createFromName('maltese', 'native'); // "malti"

use WhiteCube\Lingua\Service as Lingua;
use WhiteCube\Lingua\LanguagesRepository;

// Note: all keys of the register-array showed below are optionnal.
LanguagesRepository::register([
    'name' => 'custom',
    'native' => 'custom-language',
    'iso-639-1' => 'cl',
    'iso-639-2t' => 'cla',
    'iso-639-2b' => 'cla',
    'iso-639-3' => 'cla + 2',
    'countries' => ['MY' => 'My Country'],
    'scripts' => ['latn' => 'latin', 'cyrl' => 'cyrillic']
]);

echo Lingua::createFromName('custom')->toNative(); // "custom-language"