


A brief discussion on how to operate regional language tag information in PHP?
How to operate on regional language tag information in PHP? The following article will introduce to you the operation of regional language markup information in PHP. I hope it will be helpful to you!
I believe that everyone is definitely familiar with zh_CN. Whether it is in PHP or on our web page, you will see it. 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 marking rule is:
language-extlang-script-region-variant-extension-privateuse 语言文字种类-扩展语言文字种类-书写格式-国家和地区-变体-扩展-私有
In other words, our zh_CN can be written like this:
zh-cmn-Hans-CN-Latn-pinyin
represents: zh language type, Hans writing format is Simplified Chinese, cmn Mandarin, CN countries and regions, Latn variant Latin letters, 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 rule.
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() gets the writing format information.
getDisplayVariant() gets the variant information
Get attribute information in batches
Of course, we can also obtain some language-related information in batches.
$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', // )
Get character set related information
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 mark information in a standardized way. You can see that it turns our underscore into Underline it and convert the following attributes to uppercase. This is the standardized way of writing. 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.
匹配判断语言标记信息
对于语言标记来说,我们可以判断给定的两个标记之间是否相互匹配,比如:
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;
当然,我们也可以使用另一个 lookup() 方法来确定给定的一系列语言标记哪个与指定的标记最接近。
$arr = [ 'zh-hans', 'zh-hant', 'zh', 'zh-cn', ]; echo Locale::lookup($arr, 'zh-Hans-CN-Latn-pinyin', true, 'en_US'), PHP_EOL; // zh_hans
生成一个标准规则的语言标记
既然能够获取各类语言标记的属性信息,那么我们能不能生成一个标准的语言标记内容呢?
$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 信息。
总结
这个 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
推荐学习:《PHP视频教程》
The above is the detailed content of A brief discussion on how to operate regional language tag information in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
