Operation of regional language tag information in PHP
I believe that everyone is definitely familiar with zh_CN. You will see it whether in PHP or on our web page. its figure. In fact, this is to specify which country or region our display encoding is and which language is used. There's also a lot of fun in PHP for regional language markup. Today, the Locale class we are going to learn is for operating regional language-related content. It cannot be instantiated, and all functional methods are static.
Get and set the current regional language information
The first is that we can dynamically obtain and set the corresponding regional language information.
// # echo $LANG; // en_US.UTF-8 // php.ini // intl.default_locale => no value => no value echo Locale::getDefault(), PHP_EOL; // en_US_POSIX ini_set('intl.default_locale', 'zh_CN'); echo Locale::getDefault(), PHP_EOL; // zh_CN Locale::setDefault('fr'); echo Locale::getDefault(), PHP_EOL; // fr
By default, the content of the intl.default_locale configuration in the php.ini file is obtained using the getDefault() method. If there is no configuration in php.ini, the contents of the $LANG value of the operating system will be taken, which is the en_US_POSIX output in our example above. POSIX represents the configuration from the operating system.
Use ini_set() to directly modify the ini configuration or use the setDefault() method to dynamically modify the current regional language settings.
Rules about language tags
Before continuing to study the following content, let us first learn the specifications of language tags. For most people, they may have only been exposed to tags such as en_US and zh_CN, but in fact its complete definition is very long, but when we use this abbreviation, a lot of content will be provided in the default form. The complete tagging rules are:
language-extlang-script-region-variant-extension-privateuse
Language and text type-extended language and text type-writing format-country and region-variant-extension-private
In other words, our zh_CN can be like this Write:
zh-cmn-Hans-CN-Latn-pinyin
represents: zh language type, Hans writing format is simplified Chinese, cmn Mandarin, CN country and region, Latn variant Latin alphabet, pinyin variant Pinyin.
Do you feel like something so simple suddenly becomes so big? In addition, the prefix zh- is no longer recommended. zh- is no longer the language code, but macrolang, which is the macro language. We directly use cmn, yue (Cantonese), wuu (Wu dialect), hsn (Hunan dialect, Hunan dialect) can be used as language. Therefore, the above paragraph can also be written like this:
cmn-Hans-CN-Latn-pinyin
In the previous article, when we talked about NumberFormatter, we said that we can directly obtain the output in Chinese digital format. Now what do we want the traditional result? It's very simple, just add the Hant logo writing format to Traditional Chinese.
Regarding the content of language markup rules, you can check out the Zhihu reference link at the end of the article for a more detailed introduction.
$fmt = new NumberFormatter('zh-Hant', NumberFormatter::SPELLOUT); echo $fmt->format(1234567.891234567890000), PHP_EOL; // 一百二十三萬四千五百六十七點八九一二三四五六七九
Get various information in the specified language tag rules
What can you do after learning the rules of language tags? The main function of the Locale class is to analyze and obtain these attribute information.
Get various attribute information separately
echo Locale::getDisplayLanguage('cmn-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // cmn echo Locale::getDisplayLanguage('zh-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 中文 echo Locale::getDisplayName('cmn-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // cmn(简体,中国,LATN_PINYIN) echo Locale::getDisplayName('zh-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 中文(简体,中国,LATN_PINYIN) echo Locale::getDisplayRegion('cmn-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 中国 echo Locale::getDisplayRegion('zh-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 中国 echo Locale::getDisplayScript('cmn-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 简体中文 echo Locale::getDisplayScript('zh-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // 简体中文 echo Locale::getDisplayVariant('cmn-Hans-Latn-pinyin', 'zh_CN'), PHP_EOL; // LATN_PINYIN echo Locale::getDisplayVariant('zh-Hans-CN-Latn-pinyin', 'zh_CN'), PHP_EOL; // LATN_PINYIN
We use two marking methods to test the code, and you can see the comparison of the results.
The getDisplayLanguage() method is used to obtain the displayed language information, which is the language content in the rules.
The getDisplayName() method is used to obtain the standard language name, and you can see that the content is richer.
The getDisplayRegion() method obviously obtains the country information.
getDisplayScript() obtains writing format information.
getDisplayVariant() gets variant information
Get attribute information in batches
Of course, we can also get some language-related information in batches Information.
$arr = Locale::parseLocale('zh-Hans-CN-Latn-pinyin'); if ($arr) { foreach ($arr as $key => $value) { echo "$key : $value ", PHP_EOL; } } // language : zh // script : Hans // region : CN // variant0 : LATN // variant1 : PINYIN
Use the parseLocale() method to obtain various information in a language tag and save it in an array. The key is the tag rule name and the value is the corresponding content. See if it is the same as what we introduced above. The content is the same.
Get all variant information
As you can see from the above code, we have two variant information, which can also be obtained through a getAllVariants() method Directly obtain an array of all variant information in the language tag.
$arr = Locale::getAllVariants('zh-Hans-CN-Latn-pinyin'); var_export($arr); echo PHP_EOL; // array ( // 0 => 'LATN', // 1 => 'PINYIN', // ) 获取字符集相关信息 echo Locale::canonicalize('zh-Hans-CN-Latn-pinyin'), PHP_EOL; // zh_Hans_CN_LATN_PINYIN $keywords_arr = Locale::getKeywords('zh-cn@currency=CMY;collation=UTF-8'); if ($keywords_arr) { foreach ($keywords_arr as $key => $value) { echo "$key = $value", PHP_EOL; } } // collation = UTF-8 // currency = CMY
The canonicalize() method is used to display language tag information in a standardized way. You can see that it changes our underscores into underlines and converts the following attributes into uppercase. This is canonicalization. Writing method. However, for our applications and web pages, underscores and upper and lower case are supported. Of course, it is best for everyone to define it according to the standard writing method.
getKeywords() is used to obtain language-related information attributes from the @ symbol, such as the zh-cn we defined, and then defined its currency as CMY and character set as UTF-8, directly through getKeywords () to get an array of currency and character set attributes.
Matching judgment language tag information
For language tags, we can judge whether the two given tags match each other, such as:
echo (Locale::filterMatches('cmn-CN', 'zh-CN', false)) ? "Matches" : "Does not match", PHP_EOL; echo (Locale::filterMatches('zh-CN-Latn', 'zh-CN', false)) ? "Matches" : "Does not match", PHP_EOL;
Of course, we can also use another lookup() method to determine which of a given series of language tags is closest to the specified tag.
$arr = [ 'zh-hans', 'zh-hant', 'zh', 'zh-cn', ]; echo Locale::lookup($arr, 'zh-Hans-CN-Latn-pinyin', true, 'en_US'), PHP_EOL; // zh_hans
Generate a standard rule language tag
既然能够获取各类语言标记的属性信息,那么我们能不能生成一个标准的语言标记内容呢?
$arr = [ 'language' => 'en', 'script' => 'Hans', 'region' => 'CN', 'variant2' => 'rozaj', 'variant1' => 'nedis', 'private1' => 'prv1', 'private2' => 'prv2', ]; echo Locale::composeLocale($arr), PHP_EOL; // en_Hans_CN_nedis_rozaj_x_prv1_prv2
没错,composeLocale() 方法根据一个数组格式的内容,就可以生成一个完整标准的语言标记格式内容。当然,这个测试代码是乱写的,相当于是一个 en_CN 的标记,正常不会这么写的。
acceptFromHttp 从请求头中读取语言信息
另外,Locale 类中还提供了一个从 header 头中的 Accept Language 中获取客户浏览器语言信息的方法。
// Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); echo Locale::acceptFromHttp('en_US'), PHP_EOL; // en_US echo Locale::acceptFromHttp('en_AU'), PHP_EOL; // en_AU echo Locale::acceptFromHttp('zh_CN'), PHP_EOL; // zh echo Locale::acceptFromHttp('zh_TW'), PHP_EOL; // zh
不过从测试的结果来说,其实它只需要一个字符串参数就可以了,所以我们在命令行也可以测试它。需要注意的是,对于中文来说,它不能返回区域信息,只能返回 language 信息。
推荐学习:《PHP视频教程》
总结
这个 Locale 类相关的内容其实在笔者日常的开发中基本没怎么接触过,但相信不少做跨境项目的同学会多少对它们会有一些了解。只能说业务接触不到,那就只能先简单地学习一下看看了,同样地,以后大家遇到相关的业务需求时,别忘了它们的存在哦!
测试代码: https://github.com/zhangyue0503/dev-blog/blob/master/php/202011/source/5.PHP中针对区域语言标记信息的操作.php 参考文档: https://www.php.net/manual/zh/class.locale.php https://www.zhihu.com/question/20797118/answer/63480740
The above is the detailed content of Introduction to regional language information in PHP. For more information, please follow other related articles on the PHP Chinese website!