places2be/locales

Handling country codes and language codes

3.2.0 2024-12-11 08:26 UTC

This package is auto-updated.

Last update: 2024-12-11 08:27:18 UTC


README

PHP from Packagist Latest Stable Version Total Downloads License

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 from a string 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. Ignore the check by writing:

<?php

use Places2Be\Locales\CountryCode;

CountryCode::ignoreCountryExistence();

$countryCode = new CountryCode('xy');

Alternatively, you can initialise the class with a CountryCodesEnum:

<?php

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

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

Handling language codes

Set up a language code from a string:

<?php

use Places2Be\Locales\LanguageCode;

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

The script will only accept language codes written like that: xx-xx (or xxx-xx) and will throw an InvalidLanguageCode exception otherwise.

Per default, the script will proof if the language code exists. Ignore the check by writing:

<?php

use Places2Be\Locales\LanguageCode;

LanguageCode::ignoreLanguageExistence();

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

If you want to use country-unspecific language codes like de instead of de-de you can allow that by writing LanguageCode::allowCountryUnspecificLanguageCode().

Alternatively, you can initialise the class with a 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.