[i18n] locale-based sort, new suggestion

persian-horde@metanetworking.com persian-horde at metanetworking.com
Mon May 9 22:08:09 PDT 2005


Hello All,

About locale-based sort problem, we did some research and reached some good
points.

IMP uses php imap_sort function to sort messages by subject, date, size,
etc. Although since php 4.3.3
a charset argument is added but this function is not yet support
localization.
So sorting of many locales such as fa_IR,etc is not done correctly.
PHP developers say that imap_sort uses from IMAP c-client library for
sorting, and the c-client guys
say that they are working on localization, but this feature will not be
available in near future: http://mailman1.u.washington.edu/pipermail/imap-
uw/2005-May/000019.html

Before a locale-aware sort function would be ready for imap_sort in c-
client, the following php function could be used instead, as written in
imap_sort notes in php web site: http://www.php.net/imap_sort#52640

function imap_locale_sort($stream,$criteria,$reverse,$locale,$options)
{
       if ($criteria!=SORTSUBJECT)
               return (imap_sort($stream,$criteria,$reverse,$options));

       $unsorted = array();
       $sortresult = array();

       $MC=imap_check($stream);
       $MN=$MC->Nmsgs;

       $overview = imap_fetch_overview($stream,"1:$MN",0);
       $k=0;
       while( list($key,$val) = each($overview))
       {
               $unsorted[$k]["uid"]=$val->uid;
               $unsorted[$k]["subject"]=imap_utf8($val->subject);
               $k++;
       }
       usort ($unsorted, create_function('$a,$b','setlocale
(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));

       for ($m=0;$m<count($unsorted);$m++)
               array_push($sortresult,$unsorted[$m]["uid"]);

       if ($reverse)
               $sortresult = array_reverse($sortresult);

       return $sortresult;
}

We suggest adding this function to one of IMP LIBs, and sending $GLOBALS
["language"] as the locale parameter
for this function. This way all locales would get a correct sort result.

e.g.
imp/lib/Mailbox.php
Current code:[IMAP 4]
           $cmd = array($imp['stream'], $this->_sortby, $sortdir, SE_UID);
            if (!$this->_usepop && $this->_delhide) {
                $cmd[] = 'UNDELETED';
            }
            $sorted = @call_user_func_array('imap_sort', $cmd);

Locale-aware code:(new)
           $cmd = array($imp['stream'], $this->_sortby, $sortdir, $GLOBALS
["language"],SE_UID);
            if (!$this->_usepop && $this->_delhide) {
                $cmd[] = 'UNDELETED';
            }
            $sorted = @call_user_func_array('imap_locale_sort', $cmd);


its good to note that the correct sorting is one of the essential aspects
of internationalization, so better to fix this in a good way.

Thanks,
Persian-Horde Team






More information about the i18n mailing list