Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manipulate Regional language markup Information in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to manipulate the regional language markup information in PHP, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I believe that everyone is no stranger to zh_CN, whether it is in PHP or on our web page, we will see it. In fact, this is to specify what country or region our display code is and what language to use. There is also a lot of fun in PHP for this regional language markup. Today, the Locale class we are going to learn is about manipulating regional language-related content, it cannot be instantiated, and all functional methods are static.

Gets and sets the current regional language information

First of all, 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 valueecho Locale::getDefault (), PHP_EOL; / / en_US_POSIXini_set ('intl.default_locale',' zh_CN'); echo Locale::getDefault (), PHP_EOL; / / zh_CNLocale::setDefault ('fr'); echo Locale::getDefault (), PHP_EOL; / / fr

By default, the contents of the intl.default_locale configuration in the php.ini file are obtained using the getDefault () method. If there is no configuration in php.ini, it will take the contents of the operating system's $LANG value, which is the en_US_POSIX output in our example above, and POSIX represents the configuration from the operating system.

Using ini_set () to modify the configuration of ini directly or using the setDefault () method can dynamically modify the current locale settings.

Rules about language markup

Before moving on to the following, let's take a look at the specifications of language tags. For most people, they may only be exposed to tags such as en_US and zh_CN, but in fact, its full definition is very long, but when we use this abbreviated way, a lot of content is provided in the default form. The complete marking rules are:

Language-extlang-script-region-variant-extension-privateuse language type-extended language type-writing format-country and region-variant-extension-proprietary

In other words, our zh_CN can write like this:

Zh-cmn-Hans-CN-Latn-pinyin

Represents: zh language type, Hans writing format for simplified Chinese, cmn Mandarin, CN countries and regions, Latn variant Latin alphabet, pinyin variant Pinyin.

Do you feel like something so simple suddenly becomes high-end? In addition, the prefix zh- is no longer recommended. Zh- is no longer a language code, but macrolang is a macro language. We can directly use things like cmn, yue (Cantonese), wuu (Wu dialect) and hsn (Hunan dialect) as language. Therefore, the above paragraph can also be written as follows:

Cmn-Hans-CN-Latn-pinyin

In the last article, when we talked about NumberFormatter, we said that we could get the output in Chinese digital format directly. Now we want the result of traditional Chinese? It is very simple, plus the Hant logo is written in traditional Chinese.

On the content of the language marking rules, you can take a look at the reference link at the end of the article, which is introduced in more detail.

$fmt = new NumberFormatter ('zh-Hant', NumberFormatter::SPELLOUT); echo $fmt- > format (1234567.891234567890000), PHP_EOL; / / 123 @ 4,567: 89912345679 get all kinds of information in the markup rules of the specified language

What can you do after learning the rules of language markup? The main function of the Locale class is that it can analyze and obtain these attribute information.

Obtain various attribute information separately: echo Locale::getDisplayLanguage ('cmn-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / cmnecho Locale::getDisplayLanguage ('zh-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / Chinese echo Locale::getDisplayName ('cmn-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL / / cmn (simplified, China, LATN_PINYIN) echo Locale::getDisplayName ('zh-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / Chinese (simplified, China, LATN_PINYIN) echo Locale::getDisplayRegion ('cmn-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / Chinese echo Locale::getDisplayRegion ('zh-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL / / Chinese echo Locale::getDisplayScript ('cmn-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / simplified Chinese echo Locale::getDisplayScript ('zh-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / simplified Chinese echo Locale::getDisplayVariant ('cmn-Hans-Latn-pinyin',' zh_CN'), PHP_EOL / / LATN_PINYINecho Locale::getDisplayVariant ('zh-Hans-CN-Latn-pinyin',' zh_CN'), PHP_EOL; / / LATN_PINYIN

We use two markup methods to test the code, and we can see the comparison of the results.

The getDisplayLanguage () method is used to get the displayed language information, that is, the language content in the rule.

The getDisplayName () method is used to get the standard language name, and you can see that the content is richer.

The getDisplayRegion () method obviously gets the country information.

What getDisplayScript () gets is the information in written format.

What getDisplayVariant () gets is the variant information.

Batch acquisition of attribute information

Of course, we can also get some language-related information in bulk.

$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

Using the parseLocale () method, you can get all kinds of information in a language tag and save it in an array, with the key as the tag rule name and the value as the corresponding content to see if it is the same as what we described above.

Get all variant information

As you can see from the above code, we have two variant information, which can also be obtained directly from an array of all the variant information in the language tag through a getAllVariants () method.

$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 normalize the display of language markup information, and you can see that it turns our middle underscore into underscore and the following attributes to uppercase, which is normalized writing. However, underlining and case are supported for our applications and web pages. Of course, it is best to define it according to the standard way of writing.

GetKeywords () is used to get language-related information attributes from the @ symbol, such as the zh-cn we defined, and then define its currency as CMY and character set as UTF-8. You can get an array of currency and character set attributes directly through getKeywords ().

Matching judgment language tag information

For language tags, we can determine whether a given two 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 generates a standard regular language tag

Now that we can get the attribute information of all kinds of language tags, can we generate a standard language tag content?

$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

Yes, the composeLocale () method generates a complete standard language markup format based on the content in an array format. Of course, the test code is scribbled, which is equivalent to an en_CN tag, which is not normally written.

AcceptFromHttp reads language information from the request header

In addition, the Locale class provides a method to get the client browser language information from the Accept Language in the header header.

/ / Locale::acceptFromHttp ($_ SERVER ['HTTP_ACCEPT_LANGUAGE']); echo Locale::acceptFromHttp (' en_US'), PHP_EOL; / / en_USecho Locale::acceptFromHttp ('en_AU'), PHP_EOL; / / en_AUecho Locale::acceptFromHttp (' zh_CN'), PHP_EOL; / / zhecho Locale::acceptFromHttp ('zh_TW'), PHP_EOL; / / zh

However, from the test results, it only needs a string parameter, so we can also test it on the command line. It should be noted that for Chinese, it cannot return region information, only language information.

The above is how to manipulate the regional language markup information in PHP. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report