b1gc8v 发表于 2024-11-2 18:46:18

学习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;">//&nbsp;$_SERVER&nbsp;=&nbsp;zh-CN,zh;q=0.9;</span>
    $browserLocale&nbsp;=&nbsp;Locale::acceptFromHttp($_SERVER[<span style="color: black;">HTTP_ACCEPT_LANGUAGE</span>]);

    print_r($browserLocale);
    <span style="color: black;">echo</span>&nbsp;<span style="color: black;">&lt;br/&gt;</span>;
    <span style="color: black;">//&nbsp;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&nbsp;=&nbsp;[<span style="color: black;">en</span>,&nbsp;<span style="color: black;">fr-FR</span>,&nbsp;<span style="color: black;">da,&nbsp;en-gb;q=0.8,&nbsp;en;q=0.7</span>,&nbsp;<span style="color: black;">zh-cn</span>,&nbsp;<span style="color: black;">zh-tw</span>,&nbsp;<span style="color: black;">en-us</span>,&nbsp;<span style="color: black;">en-ca</span>,&nbsp;<span style="color: black;">ja-jp</span>];
    <span style="color: black;">foreach</span>($locale&nbsp;as&nbsp;$code){
    &nbsp;&nbsp;&nbsp;&nbsp;print_r(Locale::acceptFromHttp($code));
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">&lt;br/&gt;</span>;
    }
    <span style="color: black;">//&nbsp;en</span>
    <span style="color: black;">//&nbsp;fr_FR</span>
    <span style="color: black;">//&nbsp;da</span>
    <span style="color: black;">//&nbsp;zh</span>
    <span style="color: black;">//&nbsp;zh</span>
    <span style="color: black;">//&nbsp;en_US</span>
    <span style="color: black;">//&nbsp;en_CA</span>
    <span style="color: black;">//&nbsp;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&nbsp;=&nbsp;[<span style="color: black;">en</span>,&nbsp;<span style="color: black;">fr-FR</span>,&nbsp;<span style="color: black;">en-gb</span>,&nbsp;<span style="color: black;">zh-cn</span>,<span style="color: black;">en-us</span>,&nbsp;<span style="color: black;">ko-kr</span>,&nbsp;<span style="color: black;">ja-jp</span>];

    $money&nbsp;=&nbsp;<span style="color: black;">1234567.89</span>;

    <span style="color: black;">foreach</span>($locale&nbsp;as&nbsp;$code){
    &nbsp;&nbsp;&nbsp;&nbsp;$numberformat&nbsp;=<span style="color: black;">new</span>&nbsp;NumberFormatter($code,&nbsp;NumberFormatter::DECIMAL);
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;$code,&nbsp;<span style="color: black;">":"</span>;
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;$numberformat-&gt;format($money),&nbsp;<span style="color: black;">,&nbsp;</span>;
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>$numberformat-&gt;parse($numberformat-&gt;format($money)),<span style="color: black;">&lt;br/&gt;</span>;
    }
    <span style="color: black;">//&nbsp;en:1,234,567.89,&nbsp;1234567.89</span>
    <span style="color: black;">//&nbsp;fr-FR:1&nbsp;234&nbsp;567,89,&nbsp;</span>
    <span style="color: black;">//&nbsp;en-gb:1,234,567.89,&nbsp;1234567.89</span>
    <span style="color: black;">//&nbsp;zh-cn:1,234,567.89,&nbsp;1234567.89</span>
    <span style="color: black;">//&nbsp;en-us:1,234,567.89,&nbsp;1234567.89</span>
    <span style="color: black;">//&nbsp;ko-kr:1,234,567.89,&nbsp;1234567.89</span>
    <span style="color: black;">//&nbsp;ja-jp:1,234,567.89,&nbsp;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&nbsp;as&nbsp;$code){
    &nbsp;&nbsp;&nbsp;&nbsp;$numberformat&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;NumberFormatter($code,&nbsp;NumberFormatter::PERCENT);
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;$code,&nbsp;<span style="color: black;">":"</span>;
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>$numberformat-&gt;format($money),<span style="color: black;">&lt;br/&gt;</span>;
    }
    <span style="color: black;">//&nbsp;en:¤1,234,567.89</span>
    <span style="color: black;">//&nbsp;fr-FR:1&nbsp;234&nbsp;567,89&nbsp;€</span>
    <span style="color: black;">//&nbsp;en-gb:£1,234,567.89</span>
    <span style="color: black;">//&nbsp;zh-cn:¥1,234,567.89</span>
    <span style="color: black;">//&nbsp;en-us:$1,234,567.89</span>
    <span style="color: black;">//&nbsp;ko-kr:₩1,234,568</span>
    <span style="color: black;">//&nbsp;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&nbsp;=&nbsp;<span style="color: black;">2020-09-25&nbsp;11:05:22</span>;
    <span style="color: black;">foreach</span>($locale&nbsp;as&nbsp;$code){
    &nbsp;&nbsp;&nbsp;&nbsp;$l&nbsp;=&nbsp;<span style="color: black;">new</span>Locale($code);
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;$d&nbsp;=<span style="color: black;">new</span>&nbsp;IntlDateFormatter($code,&nbsp;IntlDateFormatter::FULL,&nbsp;IntlDateFormatter::FULL);
    &nbsp;&nbsp;&nbsp;&nbsp;$c&nbsp;=&nbsp;IntlCalendar::createInstance(<span style="color: black;">NULL</span>,&nbsp;$code);
    &nbsp;&nbsp;&nbsp;&nbsp;$c-&gt;set(<span style="color: black;">2020</span>,&nbsp;<span style="color: black;">09</span>,&nbsp;<span style="color: black;">25</span>,&nbsp;<span style="color: black;">11</span>,&nbsp;<span style="color: black;">22</span>,&nbsp;<span style="color: black;">33</span>);
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;$code,&nbsp;<span style="color: black;">":"</span>;
    &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;$d-&gt;format($c),&nbsp;<span style="color: black;">"&lt;br/&gt;"</span>;
    }

    <span style="color: black;">//&nbsp;en:Friday,&nbsp;September&nbsp;25,&nbsp;2020&nbsp;at&nbsp;2:48:12&nbsp;PM&nbsp;China&nbsp;Standard&nbsp;Time</span>
    <span style="color: black;">//&nbsp;fr-FR:vendredi&nbsp;25&nbsp;septembre&nbsp;2020&nbsp;à&nbsp;14:48:12&nbsp;heure&nbsp;normale&nbsp;de&nbsp;la&nbsp;Chine</span>
    <span style="color: black;">//&nbsp;en-gb:Friday,&nbsp;25&nbsp;September&nbsp;2020&nbsp;at&nbsp;14:48:12&nbsp;China&nbsp;Standard&nbsp;Time</span>
    <span style="color: black;">//&nbsp;zh-cn:2020年9月25日星期五&nbsp;中国标准时间&nbsp;下午2:48:12</span>
    <span style="color: black;">//&nbsp;en-us:Friday,&nbsp;September&nbsp;25,&nbsp;2020&nbsp;at&nbsp;2:48:12&nbsp;PM&nbsp;China&nbsp;Standard&nbsp;Time</span>
    <span style="color: black;">//&nbsp;ko-kr:2020년&nbsp;9월&nbsp;25일&nbsp;금요일&nbsp;오후&nbsp;2시&nbsp;48분&nbsp;12초&nbsp;중국&nbsp;표준시</span>
    <span style="color: black;">//&nbsp;ja-jp:2020年9月25日金曜日&nbsp;14時48分12秒&nbsp;中国標準時</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]
查看完整版本: 学习PHP中的国际化功能来查看货币及日期信息