CakeFest 2024: The Official CakePHP Conference

Locale::getPrimaryLanguage

locale_get_primary_language

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Locale::getPrimaryLanguage -- locale_get_primary_languageLit la langue principale de la locale

Description

Style orienté objet

public static Locale::getPrimaryLanguage(string $locale): ?string

Style procédural

locale_get_primary_language(string $locale): ?string

Lit la langue principale de la locale.

Liste de paramètres

locale

La locale dont il faut extraire la langue principale.

Valeurs de retour

Le code de langue, associé à la langue.

Retourne null quand la longueur de locale excède INTL_MAX_LOCALE_LEN.

Exemples

Exemple #1 Exemple avec locale_get_primary_language(), procédural

<?php
echo locale_get_primary_language('zh-Hant');
?>

Exemple #2 Exemple avec locale_get_primary_language(), POO

<?php
echo Locale::getPrimaryLanguage('zh-Hant');
?>

L'exemple ci-dessus va afficher :

zh

Voir aussi

add a note

User Contributed Notes 1 note

up
16
Mahn
8 years ago
The behaviour when a falsy value is passed as the $locale is undocumented, but it appears that it returns the primary language of the default system language. In my case:

Locale::getPrimaryLanguage(null);

Returns 'en'. So make sure to test $locale before passing it to the method.
To Top