places2be/locales

Handling country codes and language codes

Fund package maintenance!
Buymeacoffee

Installs: 6 474

Dependents: 3

Suggesters: 0

Security: 0

pkg:composer/places2be/locales

3.3.0 2025-10-16 06:23 UTC

This package is auto-updated.

Last update: 2025-10-16 06:24:13 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.

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 the CountryCodesEnum:

<?php

use Places2Be\Locales\CountryCode;
use Places2Be\Locales\CountryCodesEnum;

$countryCode = new CountryCode(
    CountryCodesEnum::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 the LanguageCodesEnum:

<?php

use Places2Be\Locales\LanguageCode;
use Places2Be\Locales\LanguageCodesEnum;

$languageCode = new LanguageCode(
    LanguageCodesEnum::DE_DE
);

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.