簡體   English   中英

OSX / FreeBSD上的PHP setlocale不起作用

[英]PHP setlocale on OSX/FreeBSD doesn't work

我在FreeBSD / OSX上遇到按地區排序結果的問題。 這是代碼(在Linux上可以正常運行)

<?php
header('Content-Type: text/html; charset=utf-8');
echo "<pre>"; 
setlocale(LC_ALL, 'cs_CZ.UTF-8', 'Czech');
echo "Set locale: ". setlocale(LC_ALL, 0) . "\n";
$countries = array(
    'Bulharsko',
    'Česko',
    'Polsko',
    'Čína'
);

sort($countries, SORT_LOCALE_STRING);
print_r($countries);
?>

我在OSX上檢查了獲得的語言環境:

$locale -a
...
cs_CZ
cs_CZ.ISO8859-2
cs_CZ.UTF-8
...

然后按文件:

$ pwd
/usr/share/locale/cs_CZ.UTF-8
$ ls -la
total 24
drwxr-xr-x    8 root  wheel   272 Oct 23 18:07 ./
drwxr-xr-x  226 root  wheel  7684 Oct 23 18:07 ../
lrwxr-xr-x    1 root  wheel    28 Oct 23 18:06 LC_COLLATE@ -> ../la_LN.US-ASCII/LC_COLLATE
lrwxr-xr-x    1 root  wheel    17 Oct 23 18:06 LC_CTYPE@ -> ../UTF-8/LC_CTYPE
drwxr-xr-x    3 root  wheel   102 Oct 23 18:07 LC_MESSAGES/
-r--r--r--    1 root  wheel    36 Oct 23 18:06 LC_MONETARY
lrwxr-xr-x    1 root  wheel    29 Oct 23 18:06 LC_NUMERIC@ -> ../cs_CZ.ISO8859-2/LC_NUMERIC
-r--r--r--    1 root  wheel   397 Oct 23 18:06 LC_TIME

有趣的是,OSX中的每種語言的LC_COLLATE都指向同一文件。 系統信息:

PHP 5.4.23 (cli) (built: Dec 16 2013 00:36:15) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

(OSX 10.9.1): Darwin MBP.local 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64

知道使用語言環境如何正確排序嗎?

改用Collat​​or(兼容Linux / Win / Mac):

$usedLocale = setlocale (LC_COLLATE, 'cs_CZ.utf8', 'cs_CZ.UTF-8', 'Czech', 'cs_CZ', 'cs');
$collator = new Collator($usedLocale);
$collator->asort($countries, SORT_LOCALE_STRING);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM