Skip to content

Language::validateLang

James Cobban edited this page Sep 28, 2023 · 2 revisions

Language::validateLang($lang, $langtext)

Up: class Language

This static method validates that the parameter is a string identifying an internationalization context as defined by Internet Best Common Practices 47. Such a string consists of an ISO 639-1 language code optionally qualified by a 2 or 3 character ISO 3166-1 country code. Such a qualifier is used to select the closest implemented match for a page template and a translation table. Note that the method does not validate that the components of the identifier are actually defined by the ISO standards, only that they conform to the abstract syntax. See new FtTemplate($include, $dialog) for information on how this identifier is used to select the template to use for displaying a page.

If the input value is a syntactically valid BCP 47 language code optionally qualified by a country code this method returns the parameter value with the first alphanumeric identifier folded to lower case, to conform to ISO 639 and the second alphanumeric identifier folded to upper case, to conform to ISO 3166. If the parameter value is not valid this method returns 'en' and if the optional second parameter is passed it sets an HTML output safe version of the invalid value into that parameter.

For example:

$lang     = Language::validateLang('');                 // returns 'en'
$lang     = Language::validateLang('FR');               // returns 'fr'
$lang     = Language::validateLang('es-mx');            // returns 'es-MX'
$lang     = Language::validateLang('deu-ot');           // returns 'deu-OT'
$lang     = Language::validateLang('en-usa');           // returns 'en-USA'
$lang     = Language::validateLang('FR-CAN');           // returns 'fr-CAN'
$lang     = Language::validateLang('1234');             // returns 'en'
$lang     = Language::validateLang('1234',$langtext);   // returns 'en' and sets $langtext to '1234'
$lang     = Language::validateLang('<p>',$langtext);    // returns 'en' and sets $langtext to '&lt;p&gt;'
$lang     = Language::validateLang('bb',$langtext);     // returns 'bb' and does not set $langtext

The last example demonstrates that the method only validates the syntax of the parameter. 'bb' is not a language code defined by ISO 639-1. The application can use:

$langObj        = new Language(array('code'  => $lang));
if ($langObj->isExisting())
{                          // $lang is a valid ISO 639 language code
    $langName       = $langObj->getName();        // name of the language in English
    $langCountry    = $langObj->getCountry();     // null if $lang does not include a country code
    ....

to determine whether the returned value is defined by ISO 639-1. See new Language.

Next: class LegacyHeader

Clone this wiki locally