学习PHP中的国际化功能来查看货币及日期信息
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">做为一门在世界范围内广泛<span style="color: black;">运用</span>的编程语言,国际化能力<span style="color: black;">常常</span>是衡量一个编程语言<span style="color: black;">是不是</span>能够大范围流行的重要内容。<span style="color: black;">尤其</span>是<span style="color: black;">针对</span> PHP 这种以 Web 页面编程为主战场的语言<span style="color: black;">来讲</span>,国际化能力<span style="color: black;">更加是</span>重中之重。在 PHP 中,<span style="color: black;">已然</span>为<span style="color: black;">咱们</span>准备好了<span style="color: black;">非常多</span>国际化<span style="color: black;">关联</span>的扩展及函数库,今天<span style="color: black;">咱们</span>先来简单的学习一下,后面在学习到<span style="color: black;">详细</span>的 国际化与字符编码支持 <span style="color: black;">关联</span>的函数库时再进行深入的讲解学习。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">获取当前浏览器用户的所在地</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>来获取当前<span style="color: black;">拜访</span>用户的所在地信息,<span style="color: black;">亦</span><span style="color: black;">便是</span><span style="color: black;">咱们</span><span style="color: black;">平常</span>的 zh-cn 、 en-us 这类的地区语言编码信息。</span></p><span style="color: black;">// $_SERVER = zh-CN,zh;q=0.9;</span>
$browserLocale = Locale::acceptFromHttp($_SERVER[<span style="color: black;">HTTP_ACCEPT_LANGUAGE</span>]);
print_r($browserLocale);
<span style="color: black;">echo</span> <span style="color: black;"><br/></span>;
<span style="color: black;">// zh</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器传递的这类区域信息是 zh-CN,zh;q=0.9; 这种形式的,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span> Locale::acceptFromHttp() <span style="color: black;">得到</span><span style="color: black;">详细</span>的区域信息。<span style="color: black;">不外</span>需要<span style="color: black;">重视</span>的是,不管是 zh-cn 还是 zh-tw ,返回的都是 zh ,而英文则会返回<span style="color: black;">详细</span>的是 en-us 还是 en-gd 之类的内容,<span style="color: black;">亦</span><span style="color: black;">便是</span>说这个函数<span style="color: black;">针对</span><span style="color: black;">咱们</span>中文语言的结果获取是有问题的。</span></p>$locale = [<span style="color: black;">en</span>, <span style="color: black;">fr-FR</span>, <span style="color: black;">da, en-gb;q=0.8, en;q=0.7</span>, <span style="color: black;">zh-cn</span>, <span style="color: black;">zh-tw</span>, <span style="color: black;">en-us</span>, <span style="color: black;">en-ca</span>, <span style="color: black;">ja-jp</span>];
<span style="color: black;">foreach</span>($locale as $code){
print_r(Locale::acceptFromHttp($code));
<span style="color: black;">echo</span> <span style="color: black;"><br/></span>;
}
<span style="color: black;">// en</span>
<span style="color: black;">// fr_FR</span>
<span style="color: black;">// da</span>
<span style="color: black;">// zh</span>
<span style="color: black;">// zh</span>
<span style="color: black;">// en_US</span>
<span style="color: black;">// en_CA</span>
<span style="color: black;">// ja_JP</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>手动来指定区域信息,<span style="color: black;">能够</span>看出除了 zh 之外其它的都是<span style="color: black;">能够</span>获取到正常的<span style="color: black;">详细</span>区域信息的。这点是<span style="color: black;">咱们</span><span style="color: black;">运用</span>这个函数时需要<span style="color: black;">重视</span>的<span style="color: black;">地区</span>。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">获取指定地区所在地的标准数字信息</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span><span style="color: black;">咱们</span>中国人来,数字比较习惯的是百、千、万位,<span style="color: black;">通常</span><span style="color: black;">亦</span>不太需要区分,而西方国家则<span style="color: black;">爱好</span>三位一个逗号的分隔开来,<span style="color: black;">亦</span><span style="color: black;">便是</span>对应<span style="color: black;">她们</span>的英文数字习惯,<span style="color: black;">例如</span> 10000,在西方<span style="color: black;">暗示</span>中是 10,000 ,英语是 ten thousand , 10000000 <span style="color: black;">暗示</span>为 10,000,000 ,英语是 ten million 。<span style="color: black;">因此呢</span>,在 PHP 中,<span style="color: black;">亦</span>是有相<span style="color: black;">针对</span>这种<span style="color: black;">状况</span>的数字<span style="color: black;">暗示</span>形式,<span style="color: black;">亦</span><span style="color: black;">便是</span>为普通数字加上逗号以<span style="color: black;">暗示</span>西方数字标准格式。</span></p>$locale = [<span style="color: black;">en</span>, <span style="color: black;">fr-FR</span>, <span style="color: black;">en-gb</span>, <span style="color: black;">zh-cn</span>,<span style="color: black;">en-us</span>, <span style="color: black;">ko-kr</span>, <span style="color: black;">ja-jp</span>];
$money = <span style="color: black;">1234567.89</span>;
<span style="color: black;">foreach</span>($locale as $code){
$numberformat =<span style="color: black;">new</span> NumberFormatter($code, NumberFormatter::DECIMAL);
<span style="color: black;">echo</span> $code, <span style="color: black;">":"</span>;
<span style="color: black;">echo</span> $numberformat->format($money), <span style="color: black;">, </span>;
<span style="color: black;">echo</span>$numberformat->parse($numberformat->format($money)),<span style="color: black;"><br/></span>;
}
<span style="color: black;">// en:1,234,567.89, 1234567.89</span>
<span style="color: black;">// fr-FR:1 234 567,89, </span>
<span style="color: black;">// en-gb:1,234,567.89, 1234567.89</span>
<span style="color: black;">// zh-cn:1,234,567.89, 1234567.89</span>
<span style="color: black;">// en-us:1,234,567.89, 1234567.89</span>
<span style="color: black;">// ko-kr:1,234,567.89, 1234567.89</span>
<span style="color: black;">// ja-jp:1,234,567.89, 1234567.89</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>直接<span style="color: black;">运用</span> format() <span style="color: black;">办法</span>,就<span style="color: black;">能够</span><span style="color: black;">得到</span>指定地区的标准数字<span style="color: black;">暗示</span>形式。<span style="color: black;">能够</span>看出亚洲地区的都是和欧美国家相同的,但法国则是<span style="color: black;">运用</span>空格进行分隔的。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">获取指定地区所在地的货币符号信息</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">既然有数字格式的<span style="color: black;">区别</span>了,<span style="color: black;">那样</span>货币形式的区别就<span style="color: black;">亦</span>是类似的形式了,只<span style="color: black;">不外</span><span style="color: black;">咱们</span>在<span style="color: black;">运用</span> NumberFormatter 类的时候需要指定后面的数字类型常量为货币形式 NumberFormatter::PERCENT 。</span></p>oreach($locale as $code){
$numberformat = <span style="color: black;">new</span> NumberFormatter($code, NumberFormatter::PERCENT);
<span style="color: black;">echo</span> $code, <span style="color: black;">":"</span>;
<span style="color: black;">echo</span>$numberformat->format($money),<span style="color: black;"><br/></span>;
}
<span style="color: black;">// en:¤1,234,567.89</span>
<span style="color: black;">// fr-FR:1 234 567,89 €</span>
<span style="color: black;">// en-gb:£1,234,567.89</span>
<span style="color: black;">// zh-cn:¥1,234,567.89</span>
<span style="color: black;">// en-us:$1,234,567.89</span>
<span style="color: black;">// ko-kr:₩1,234,568</span>
<span style="color: black;">// ja-jp:¥1,234,568</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">看出来了吗,当指定了货币形式后,format() 出来的结果直接就带上了货币符号。<span style="color: black;">第1</span>个 en 的货币符号其实<span style="color: black;">便是</span>国际通用货币符号,而日元和<span style="color: black;">咱们</span>人民币的符号是相同的,这<span style="color: black;">亦</span>是一个有趣的历史知识,<span style="color: black;">大众</span><span style="color: black;">能够</span><span style="color: black;">自动</span><span style="color: black;">查找</span>下。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">获取指定地区所在地的完整日期</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">最后<span style="color: black;">便是</span>获取的各地的日期标准格式。其实<span style="color: black;">此刻</span><span style="color: black;">大众</span>通用的都是 yyyy-mm-dd 这种形式来<span style="color: black;">暗示</span>日期,<span style="color: black;">不外</span>正式规范的日期标准格式各个地区还是有所<span style="color: black;">区别</span>的。</span></p>$date = <span style="color: black;">2020-09-25 11:05:22</span>;
<span style="color: black;">foreach</span>($locale as $code){
$l = <span style="color: black;">new</span>Locale($code);
$d =<span style="color: black;">new</span> IntlDateFormatter($code, IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$c = IntlCalendar::createInstance(<span style="color: black;">NULL</span>, $code);
$c->set(<span style="color: black;">2020</span>, <span style="color: black;">09</span>, <span style="color: black;">25</span>, <span style="color: black;">11</span>, <span style="color: black;">22</span>, <span style="color: black;">33</span>);
<span style="color: black;">echo</span> $code, <span style="color: black;">":"</span>;
<span style="color: black;">echo</span> $d->format($c), <span style="color: black;">"<br/>"</span>;
}
<span style="color: black;">// en:Friday, September 25, 2020 at 2:48:12 PM China Standard Time</span>
<span style="color: black;">// fr-FR:vendredi 25 septembre 2020 à 14:48:12 heure normale de la Chine</span>
<span style="color: black;">// en-gb:Friday, 25 September 2020 at 14:48:12 China Standard Time</span>
<span style="color: black;">// zh-cn:2020年9月25日星期五 中国标准时间 下午2:48:12</span>
<span style="color: black;">// en-us:Friday, September 25, 2020 at 2:48:12 PM China Standard Time</span>
<span style="color: black;">// ko-kr:2020년 9월 25일 금요일 오후 2시 48분 12초 중국 표준시</span>
<span style="color: black;">// ja-jp:2020年9月25日金曜日 14時48分12秒 中国標準時</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">是不是很方便,直接就以当地的语言来<span style="color: black;">暗示</span>指定地区的标准日期格式了。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>跨境项目<span style="color: black;">来讲</span>,这些功能是非常重要的能力,能够<span style="color: black;">帮忙</span><span style="color: black;">咱们</span>快捷地<span style="color: black;">研发</span>或切换<span style="color: black;">关联</span>的地区,等到深入学习的时候再进行<span style="color: black;">仔细</span>的学习讲解,这次就全当是一次热身吧!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">测试代码:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">https://github.com/zhangyue0503/dev-blog/blob/master/php/202009/source/11.学习PHP中的国际化功能来查看货币及日期信息.php</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">参考文档:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">《PHP7编程实战》</span></p>
页:
[1]