class Utility (View source)

The Utility class for locale specific actions

Constants

PATTERN_MATCH_ACCEPTLANGUAGE

A pattern which matches HTTP Accept-Language Headers

Methods

static mixed
parseAcceptLanguageHeader(string $acceptLanguageHeader)

Parses Accept-Language header and returns array of locale tags (like: en-GB, en), or false if no tags were found.

static mixed
extractLocaleTagFromFilename(string $filename)

Extracts a locale tag (identifier) from the filename given.

static mixed
extractLocaleTagFromDirectory(string $directory)

Extracts a locale tag (identifier) from the directory name given.

static bool
stringBeginsWith(string $haystack, string $needle)

Checks if $haystack string begins with $needle string.

static bool
stringEndsWith(string $haystack, string $needle)

Checks if $haystack string ends with $needle string.

Details

static mixed parseAcceptLanguageHeader(string $acceptLanguageHeader)

Parses Accept-Language header and returns array of locale tags (like: en-GB, en), or false if no tags were found.

This method only returns tags that conforms ISO 639 for language codes and ISO 3166 for region codes. HTTP spec (RFC 2616) defines both of these parts as 1*8ALPHA, but this method ignores tags with longer (or shorter) codes than defined in ISO mentioned above.

There can be an asterisk "*" in the returned array, which means that any language is acceptable.

Warning: This method expects that locale tags are placed in descending order by quality in the $header string. I'm not sure if it's always true with the web browsers.

Parameters

string $acceptLanguageHeader

Return Value

mixed

The array of locale identifiers or false

static mixed extractLocaleTagFromFilename(string $filename)

Extracts a locale tag (identifier) from the filename given.

Locale tag should be placed just before the extension of the file. For example, filename bar.png can be localized as bar.en_GB.png, and this method extracts en_GB from the name.

Note: this ignores matches on rss, xml and php and validates the identifier.

Parameters

string $filename

Filename to extract locale identifier from

Return Value

mixed

The string with extracted locale identifier of false on failure

static mixed extractLocaleTagFromDirectory(string $directory)

Extracts a locale tag (identifier) from the directory name given.

Note: Locale tag will be extracted from the last directory path segment only.

Parameters

string $directory

Directory path to extract locale identifier from

Return Value

mixed

The string with extracted locale identifier of false on failure

static bool stringBeginsWith(string $haystack, string $needle)

Checks if $haystack string begins with $needle string.

Parameters

string $haystack
string $needle

Return Value

bool

true if $haystack begins with $needle

static bool stringEndsWith(string $haystack, string $needle)

Checks if $haystack string ends with $needle string.

Parameters

string $haystack
string $needle

Return Value

bool

true if $haystack ends with $needle