PHP Internationalization and Localization Sorting in a Locale-Aware Order - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Internationalization and Localization Sorting in a Locale-Aware Order - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 29, 2019

PHP Internationalization and Localization Sorting in a Locale-Aware Order

PHP Internationalization and Localization


Sorting in a Locale-Aware Order


Problem

You need to sort text in a way that respects a particular locale’s rules for character ordering.

Solution

Instantiate a Collator object for your locale, and then call its sort() method:

       $words = array('Малина', 'Клубника', 'Огурец');
       $collator = new Collator('ru_RU');
       // Sorts in-place, just like sort()
       $collator->sort($words);

Discussion

PHP’s normal text-handling routines just treat strings as sequences of bytes. They know nothing about multibyte characters, let alone each locale’s rules about which characters go “before” which other ones in that locale’s equivalent of alphabetical order. The Collator class, however, uses ICU’s big database of locale-specific information to do this
properly.

The Collator’s sort() method corresponds to the PHP sort() function. Collator also has an asort() method which, just like PHP’s asort() function, maintains index/value association in the sorted array.


No comments:

Post a Comment

Post Top Ad