[PHP] LocalizationDemystified: Php-IntlforEveryone address: https://www.sitepoint.com/localization-demystified-understanding-php-intl/
Most application execution fields can recognize operations similar to processing text, date, and time zone. The extension function of PHP Intl can provide a good API to connect to the well-known ICU Library.
Install
This extension function is installed in PHP 5.3 or later by default. you can run the following command to find it:
php -m | grep 'intl'
If this extension does not exist, you can install it manually according to the installation guide. If you use Ubuntu, you can directly install it using the following command:
sudoapt-getupdatesudoapt-getinstallphp5-intl
If you are using PHP 7, you need to add PPA (ppa: ondrej/php), upgrade your system, and install the extended function Intl.
# Add PPAsudoadd-apt-repositoryppa: ondrej/php-7.0 # upgrade index Library sudoapt-getupdate # install extended feature sudoapt-getinstallphp7.0-intl
Message formatting
Most modern applications must pay attention to the local architecture. Sometimes, these messages are simple strings with placeholder variables, and sometimes they are complex and diversified strings.
Simple information
We start with a simple information that contains placeholders. Placeholders are characters enclosed in curly brackets. Here is an example:
var_dump( MessageFormatter::formatMessage( "en_US", "I have {0, number, integer} apples.", [ 3 ] )); // output string(16) "I have 3 apples."
The class function for passing the feed to MessageFormatter: formatMessage is as follows:
- Message language environment
- Message string
- Placeholder data
The placeholder {0, number, integer} will type a number-an integer as the first item of the database array (see the table listing options below). We can also use named parameters in the placeholder. 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 and Hindi.
The previous example points to the en_US environment. let's switch to the ar environment and see what's different.
var_dump( MessageFormatter::formatMessage( "ar", "I have {number_apples, number, integer} apples.", [ 'number_apples' => 3 ] )); string(17) "I have ٣ apples."
Let's replace it with the Bengale scene (bn ).
var_dump( MessageFormatter::formatMessage( "bn", "I have {number_apples, number, integer} apples.", [ 'number_apples' => 3 ] )); string(18) "I have ৩ apples."
So far, we have explained only numbers. Now let's take a look at other available types.
$time = time();var_dump( MessageFormatter::formatMessage( "en_US", "Today is {0, date, full} - {0, time}", array( $time )) ); string(47) "Today is Wednesday, April 6, 2016 - 11:21:47 PM"
var_dump( MessageFormatter::formatMessage( "en_US", "duration: {0, duration}", array( $time )) ); string(23) "duration: 405,551:27:58"
We can also represent scores.
var_dump( MessageFormatter::formatMessage( "en_US", "I have {0, spellout} apples", array( 34 )) ); string(25) "I have thirty-four apples"
This applies to different language environments.
var_dump( MessageFormatter::formatMessage( "ar", "لدي {0, spellout} تفاحة", array( 34 )) ); string(44) "لدي أربعة و ثلاثون تفاحة"
Diversified
An important part of localized applications is managing diverse information to make the user interface as intuitive as possible. This can be illustrated in Apple's example above. The information in this example is shown below:
- (Number_apples = 0): no Apple.
- (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}}', array('number_apples' => 10)) );
// number_apples = 0string(16) "I have no apples" // number_apples = 1string(16) "I have one apple" // number_apples = 10string(16) "I have 10 apples"
This syntax is really simple and straightforward, and most packages contain this syntax. For more information, see this document.
{data, plural, offsetValue =value{message}... other{message}}
- Data: Value Index
- Plural: event parameter type
- OffsetValue: optional (offset: value ). It removes the offset from the value.
- = Value {message}: Test the equivalence. The information is in curly brackets. We can repeat it multiple times (= 0 {no apples} = 1 {one apple} = 2 {two apple }).
- Other {message}: default, for example, declare in switch-case. # The symbol can be used to type data values.
Option
Sometimes, we need to list different information about each value range. For example:
var_dump( MessageFormatter::formatMessage( "en_US", 'The value of {0,number} is {0, choice, 0 # between 0 and 19 | 20 # between 20 and 39 | 40 # between 40 and 59 | 60 # between 60 and 79 | 80 # between 80 and 100 | 100 < more than 100 }', array(60)) ); string(38) "The value of 60 is between 60 and 79 "
ArgType is set for choice here. the syntax is as follows:
{value, choice, choiceStyle}
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 = '#' | ≤ (U+2264)
Note: ICU developers are not encouraged to select a type.
Select item
Sometimes we need to select the option UI component. The personal data page uses this method to update UI information based on the user's gender and so on. Here is 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.}". "}", array('gender' => 'female')) );string(19) "She has some apples"
The mode is defined as follows:
{value, select, selectStyle} // selectStyleselectValue {message} (selectValue {message})*
The message feed contains other modes similar to options and plural values. The next section will explain an example of combining multiple modes.
Complexity
So far, we have seen simple examples such as selection and diversification. However, many cases may be complicated. The ICU document provides a good example to illustrate this. For ease of understanding, let's take a look.
var_dump( MessageFormatter::formatMessage( "en_US", "{gender_of_host, select, ". "female {She has a party} ". "male {He has some apples.}". "other {He has some apples.}". "}", array('gender_of_host' => 'female', "num_guests" => 5, 'host' => "Hanae", 'guest' => 'Younes' )) );
This is the same example we used before. Unlike the previous simple information, we have customized the num_guests value (the case of diversification is discussed ).
var_dump( MessageFormatter::formatMessage( "en_US", "{gender_of_host, select, ". "female {". "{num_guests, plural, offset:1 ". "=0 {{host} does not have a party.}". "=1 {{host} invites {guest} to her party.}". "=2 {{host} invites {guest} and one other person to her party.}". "other {{host} invites {guest} and # other people to her party.}}}". "male {He has some apples.}". "other {He has some apples.}}", array('gender_of_host' => 'female', "num_guests" => 5, 'host' => "Hanae", 'guest' => 'Younes' )) );
Note that offset: 1 is used to remove a guest from num_guests.
string(53) "Hanae invites Younes and 4 other people to her party."
The complete section of the example is as follows.
var_dump( MessageFormatter::formatMessage( "en_US", "{gender_of_host, select, ". "female {". "{num_guests, plural, offset:1 ". "=0 {{host} does not have a party.}". "=1 {{host} invites {guest} to her party.}". "=2 {{host} invites {guest} and one other person to her party.}". "other {{host} invites {guest} and # other people to her party.}}}". "male {". "{num_guests, plural, offset:1 ". "=0 {{host} does not have a party.}". "=1 {{host} invites {guest} to his party.}". "=2 {{host} invites {guest} and one other person to his party.}". "other {{host} invites {guest} and # other people to his party.}}}". "other {". "{num_guests, plural, offset:1 ". "=0 {{host} does not have 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:
// num_guests = 2string(55) "Hanae invites Younes and one other person to her party." // num_guests = 1string(34) "Hanae invites Younes to her party." // num_guests = 0string(28) "Hanae does not have a party."
Message parsing
There is not much to say about parsing information; we use the previous mode to format additional information from the output information.
$messageFormater = new MessageFormatter("en_US", 'I have {0, number}');var_dump( $messageFormater->parse("I have 10 apples") ); array(1) { [0]=> int(10)}
View documents to obtain