[PHP] (translated) Localization demystified:php-intl for Everyone

Source: Internet
Author: User
Tags intl time zones
Original address: https://www.sitepoint.com/localization-demystified-understanding-php-intl/

Most application execution farms can recognize operations like handling text, dates, time zones, and so on. The PHP Intl extension provides a good API to connect the functions of the well-known ICU library.

Installation

This extension is installed by default in PHP 5.3 and later, and you can find it by using the following command:

If this extension does not exist, you can follow the installation guide to install it manually. If you use Ubuntu, you can install it directly using the following command:

If you are using PHP 7, you need to add the PPA (ppa:ondrej/php), upgrade your system and install the extended feature Intl.

Message formatting

Most modern applications are built with great attention to the local. Sometimes, these messages are a simple string that has a variable placeholder, and sometimes a complex, pluralistic string.

Simple information

We'll start with a simple message that contains placeholders. Placeholders are characters that are enclosed in curly braces. Here's an example:

Var_dump (    messageformatter::formatmessage (        "en_US",        "I have {0, number, integer} apples.",        [3]    

The class functions that pass the feed to Messageformatter::formatmessage are as follows:

    • Language environment for messages
    • Message string
    • Placeholder Data

The placeholder {0, number, integer} will type in one count-integer as the first item in the database array (see table below for options) we can also use named parameters in placeholders. The following example outputs the same result.

Var_dump (    messageformatter::formatmessage (        "en_US",        "I have {number_apples, number, integer} apples.",        [' number_apples ' = 3]    

Different languages have different numbers, such as Arabic, Hindi, and so on.

The previous example points to the en_US environment and lets us change to an AR environment and see what's different.

Var_dump (    messageformatter::formatmessage (        "ar",        "I have {number_apples, number, integer} apples.",        [' number_apples ' = 3]    

Let's change to Bengali scenario (BN).

Var_dump (    messageformatter::formatmessage (        "bn",        "I have {number_apples, number, integer} apples.",        [' number_apples ' = 3]    

So far, we've only explained the numbers. Now let's look at other types that we can use.

$time = time (); Var_dump (Messageformatter::formatmessage (    "en_US",    "Today is {0, date, full}-{0, time}",    

Var_dump (Messageformatter::formatmessage (    "en_US",    "duration: {0, duration}",    

We can also represent fractions.

Var_dump (Messageformatter::formatmessage (    "en_US",    "I have {0, spellout} apples",    

This applies to different locales as well.

Var_dump (Messageformatter::formatmessage (    "ar",    "لدي{0, spellout}تفاحة",    

Diversified

An important part of localizing applications is managing multivariate information to make the user interface as intuitive as possible. This is illustrated by an example of Apple above. The following is the information representation in this example:

    • (number_apples = 0): no apples.
    • (Number_apples = 1): an apple.
    • (Number_apples > 1): Multiple apples.

Var_dump (Messageformatter::formatmessage (    "en_US",    ' I have {number_apples, plural, =0{no apples} =1{one Apple } other{# Apples}} ',    

This syntax is really simple and straightforward, and most packages contain this syntax. Please see this document for more details.

    • Data: Value Index
    • Plural: Event parameter type
    • Offsetvalue: Optional (offset:value). It subtracts the offset from the value.
    • =value{message}: Test equivalence, information within curly braces. We can repeat multiple times (=0{no apples} =1{one Apple} =2{two Apple}).
    • Other{message}: Default, such as in Switch-case declaration. The # symbol can be used to type the data value.

Options

Sometimes, we need to list the different information for each of the ranges. such as the following example:

Var_dump (Messageformatter::formatmessage (    "en_US",    ' the value of {0,number} is {0, choice,                                        0 # between 0 and |                                        # between |                                        # between |                                        # between |                                        # between |                                        < more than 100} ',    

Argtype is set up here for choice, with the following syntax:

The official definition of the ICU file is as follows:

Choicestyle = numberseparatormessage (' | ' numberseparatormessage) * Number = Normal_number | ['-']  ∞ (u+221e, infinity) Normal_number = Double value (unlocalizedasciistring) separator = Less_than | Less_than_or_equalless_than = ' < ' less_than_or_equal = ' # ' |  

Note: ICU developers do not encourage the use of selection types.

Select item

Sometimes we need to select the options UI component. The profile page uses this method to update the UI information based on the gender of the user. Here's an example:

Var_dump (Messageformatter::formatmessage (    "en_US",    "{Gender, select,".      ) Female {She has some apples} ".      " Male {He has some apples.} ".      " Other {It has some apples.} "."    } ",    

The pattern is defined as follows:

The message feed includes similar options and other patterns for complex numbers. The next section will explain an example of how many patterns we have combined.

Complex situation

So far, we've seen simple examples such as selection, diversification, and so on. But a lot of things can be more complicated. The ICU documentation has a good example to illustrate this point. For the sake of understanding, we see for a while.

Var_dump (Messageformatter::formatmessage (    "en_US",    "{gender_of_host, select,".      ) Female {She had a party} ".      " Male {He has some apples.} ".      " Other {He has some apples.} '. '    } ',    

This is one of the same examples we used before, unlike the simple information we used before, we relied on num_guests values to customize the following (the discussion is a diversified case).

Var_dump (Messageformatter::formatmessage (    "en_US",    "{gender_of_host, select,".      ) Female {".        " {num_guests, plural, offset:1 ".          " =0 {{host} does not has a party.} ".          " =1 {host} invites {Guest} to her party.} ".          " =2 {{host} invites {Guest} and one other person to the party.} ".          " Other {{host} invites {guest} and # Other people to her party.}}} ".      " Male {He has some apples.} ".      " Other {He has some apples.}} ",    

Note that we used offset:1 to remove a guest from the num_guests.

The following is the complete segment of the example.

Var_dump (Messageformatter::formatmessage ("en_US", "{gender_of_host, select,".        "Female {".          "{num_guests, plural, offset:1".          "=0 {{host} does not has a party.}".          "=1 {{host} invites {Guest} to his party.}".          "=2 {{host} invites {Guest} and one other person to her party.}".      "Other {{host} invites {guest} and # Other people.}}".        "Male {".          "{num_guests, plural, offset:1".          "=0 {{host} does not has a party.}".          "=1 {{host} invites {Guest} to he party.}".          "=2 {{host} invites {Guest} and one other person to he party.}".      "Other {{host} invites {guest} and # Other people to its party.}}".        "Other {".          "{num_guests, plural, offset:1".          "=0 {{host} does not has a party.}".          "=1 {{host} invites {Guest} to their party.}".          "=2 {{host} invites {Guest} and one other person to their party.}". "Other {{host} invites {guest} and # other PEOple to their party.}}} ", Array (' gender_of_host ' = ' female '," num_guests "= 5, ' host ' = ' Hanae ', ' guest ' =  > ' younes '));

Change the number of guests to test all types of information:

Message parsing

There is not much that can be said about parsing information; We use the previous pattern to format the extra information from the output information.

$messageFormater = new Messageformatter ("en_US", ' I has {0, number} '); Var_dump ($messageFormater->parse ("I have AP Ples ")); Array (1) {  [0]=>  

View the documentation for more information about parsing.

Conclusion

In this introductory article, we've learned to localize our information using PHP Intel's extended capabilities. The next section deals with formatting numbers and dates, and the use of calendars. If you have any doubts about the above content, please leave us a message.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.