Sort filter values in a locale aware way - #1262
Conversation
Feature filter values were ordered with natcasesort(), which compares strings byte by byte. Multi-byte accented characters (Czech č, š, ř, etc.) therefore sorted after every plain ASCII letter instead of next to their base letter, so a value like "Černá" ended up at the bottom of the list rather than near "Czech". Use a Collator built from the current language locale when the intl extension is available, with a secondary strength (case insensitive, like before) and numeric collation (to keep the natural 2-before-10 ordering). Fall back to natcasesort() when intl is not present.
|
Hello @boo-code! This is your first pull request on ps_facetedsearch repository of the PrestaShop project. Thank you, and welcome to this Open Source community! |
|
thanks @boo-code CI is red. Details: |
getValuesCollator() read $this->context->language->locale unconditionally, so any call path where the language carries no locale raised an Undefined property notice (surfaced by the existing BlockTest fixtures, whose context language has no locale). Guard the access and fall back to natcasesort when no locale is set, which also protects a real store whose language has an empty locale configured.
|
Thanks for catching that. Fixed in f51ef4d: |
|
The |
|
@boo-code Audrius, ping us on slack DM every time you need something bumped, so we are not stuck. Me, @Codencode or @kpodemski for example. |
natcasesort()(inFilters\Block::sortFeatureBlock()), which compares strings byte by byte. Multi-byte accented characters (Czechč,š,ř, …) therefore sorted after every plain ASCII letter instead of next to their base letter, so e.g.Černálanded at the bottom of the list rather than right afterCzech. The fix sorts with aCollatorbuilt from the current language locale (when theintlextension is available), using a secondary strength (case-insensitive, asnatcasesortwas) and numeric collation (to keep the natural2-before-10ordering). It falls back tonatcasesort()whenintlis unavailable.BlockTest::testSortFeatureBlockSortsValuesLocaleAware): with localecs-CZ, a feature whose values areValue, Else, Beta, Černá, Czech, Other, Alphais sorted toAlpha, Beta, Czech, Černá, Else, Other, Value(red before / green after). Manually: create a feature with Czech accented values (č,š,ř, …), enable it as a filter, and check the storefront filter block — the values are now alphabetically ordered per the language instead of pushing accented ones to the end.Reported by @Hlavtox. Verified that
Collator(cs-CZ)reproduces the exact expected order from the issue (Alpha, Beta, Czech, Černá, Else, Other, Value), whereasnatcasesort()producesAlpha, Beta, Czech, Else, Other, Value, Černá.