places2be/locales

Handling country codes and language codes in an object-oriented way. Includes Enums for ISO 639-1, ISO 639-2, ISO 639-3, ISO 3166-1 Alpha-1, ISO 3166-1 Alpha-2, and ISO 3166-1 Numeric.

Maintainers

Package info

bitbucket.org/wirbelwild/places2be-locales

Homepage

Issues

pkg:composer/places2be/locales

Transparency log

Fund package maintenance!

Buymeacoffee

Statistics

Installs: 8 366

Dependents: 4

Suggesters: 0

3.4.0 2026-07-28 11:19 UTC

README

PHP from Packagist Latest Stable Version Total Downloads License

Bit&Black Logo

Places2Be Locales

Handling country codes and language codes in an object-oriented way. Includes Enums for ISO 639-1, ISO 639-2, ISO 639-3, ISO 3166-1 Alpha-1, ISO 3166-1 Alpha-2, and ISO 3166-1 Numeric.

TOC

Installation

This library is made for the use with Composer. Add it to your project by running $ composer require places2be/locales.

Usage

Handling country codes

Set up a country code like that:

<?php

use Places2Be\Locales\CountryCode;

$countryCode = new CountryCode('de');

The script will only accept country codes having a length of two characters and will throw an InvalidCountryCode exception otherwise.

Per default, the script will proof if the country code exists. You can change this behaviour in two ways:

<?php

use Places2Be\Locales\CountryCode;

/**
 * 1. Disable the validation globally: 
 */
CountryCode::ignoreCountriesExistence();

/**
 * 2. Disable the validation only for the current object:
 */
$countryCode = new CountryCode('xx', ignoreCountryExistence: true);

Instead of initializing the class with a string you can also use one of the country code enums, defined in ISO 3166-1 Alpha-2, ISO 3166-1 Alpha-3 and ISO 3166-1 Numeric:

<?php

use Places2Be\Locales\CountryCode;
use Places2Be\Locales\ISO_3166_1_Alpha_2\CountryCodeEnum;

$countryCode = new CountryCode(
    CountryCodeEnum::DE
);

Handling language codes

Set up a language code like that:

<?php

use Places2Be\Locales\LanguageCode;

$languageCode = new LanguageCode('de-ch');

Per default, the script will proof if the language code exists. You can change this behaviour in two ways:

<?php

use Places2Be\Locales\LanguageCode;

/**
 * 1. Disable the validation globally: 
 */
LanguageCode::ignoreLanguagesExistence();

/**
 * 2. Disable the validation only for the current object:
 */
$languageCode = new LanguageCode('xx-yy', ignoreLanguageExistence: true);

Per default, the class will only accept language codes written like that: xx-xx (or xxx-xx) and will throw an InvalidLanguageCode exception otherwise.

If you want to use country-unspecific language codes like de instead of de-de you can allow that in two ways:

<?php

use Places2Be\Locales\LanguageCode;

/**
 * 1. Disable the validation globally: 
 */
LanguageCode::allowCountryUnspecificLanguageCodes();

/**
 * 2. Disable the validation only for the current object:
 */
$languageCode = new LanguageCode('de', allowCountryUnspecificLanguageCode: true);

Instead of initializing the class with a string you can also use one of the language codes enums, following ISO 639-1, ISO 639-2 and ISO 639-3:

<?php

use Places2Be\Locales\LanguageCode;
use Places2Be\Locales\ISO_3166_1_Alpha_2\CountryCodeEnum;
use Places2Be\Locales\ISO_639_1\LanguageCodeEnum;

$languageCode = new LanguageCode(
    LanguageCodeEnum::DE
);

// or:

$languageCode = LanguageCode::create(
    LanguageCodeEnum::DE,
    CountryCodeEnum::AT
);

Handling the reading mode

You can also access the reading mode from a language code by calling the getReadingMode method. It will return a instance of the ReadingModeEnum.

<?php

use Places2Be\Locales\LanguageCode;

$languageCode = new LanguageCode('ar-ae');
$readingMode = $languageCode->getReadingMode();

Help

If you have any questions, feel free to contact us under hello@bitandblack.com.

Further information about Bit&Black can be found under www.bitandblack.com.