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_language入力ロケールのプライマリ言語を取得する

説明

オブジェクト指向型

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

手続き型

locale_get_primary_language(string $locale): ?string

入力ロケールのプライマリ言語を取得します。

パラメータ

locale

プライマリ言語コードを取り出したいロケール。

戻り値

このロケールの言語コードを返します。

locale の長さが INTL_MAX_LOCALE_LEN を超えた場合、null を返します。

例1 locale_get_primary_language() の例

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

例2 オブジェクト指向の例

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

上の例の出力は以下となります。

zh

参考

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