Sta*_*bie 5 javascript php internationalization
尝试格式化货币时,我似乎得到了非常不一致的结果。在 PHP 中,我使用https://www.php.net/manual/en/class.numberformatter.php。我有以下演示代码:
$number = 5125.99;
echo getInternationallyFormattedCurrency($number, 'tl-PH', 'PHP');
echo '<br/>';
echo getInternationallyFormattedCurrency($number, 'fil-PH', 'PHP');
echo '<br/>';
echo getInternationallyFormattedCurrency($number, 'en-US', 'PHP');
echo '<br/>';
echo '<br/>';
echo getInternationallyFormattedCurrency($number, 'tl_PH', 'USD');
echo '<br/>';
echo getInternationallyFormattedCurrency($number, 'fil_PH', 'USD');
echo '<br/>';
echo getInternationallyFormattedCurrency($number, 'en_US', 'USD');
echo '<br/>';
Run Code Online (Sandbox Code Playgroud)
当我在我的本地主机上运行它时(是 PHP 版本 7.3.7,但我更新以匹配我服务器的 PHP 版本 7.3.12 - ICU 版本 64.2),我得到这个:
?5,125.99
?5,125.99
PHP 5,125.99
$5,125.99
$5,125.99
$5,125.99
Run Code Online (Sandbox Code Playgroud)
但是,当我在我的服务器(PHP 版本 7.3.12 - ICU 版本 4.2.1)上运行它时,我得到了这个:
? 5125.99
? 5125.99
?5,125.99
$ 5125.99
$ 5125.99
$5,125.99
Run Code Online (Sandbox Code Playgroud)
为什么会有差异?哪一个实际上是正确的?由于 ICU 版本较高,我猜测我的本地机器?
我也需要来自 JS 的完全相同的功能。因此,在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat 上,我放置了以下等效代码:
var number = 5125.99;
console.log(new Intl.NumberFormat('tl-PH', { style: 'currency', currency: 'PHP' }).format(number));
console.log(new Intl.NumberFormat('fil-PH', { style: 'currency', currency: 'PHP' }).format(number));
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'PHP' }).format(number));
console.log(new Intl.NumberFormat('tl-PH', { style: 'currency', currency: 'USD' }).format(number));
console.log(new Intl.NumberFormat('fil-PH', { style: 'currency', currency: 'USD' }).format(number));
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(number));
Run Code Online (Sandbox Code Playgroud)
我明白了:
> "PHP 5,125.99"
> "?5,125.99"
> "PHP 5,125.99"
> "$5,125.99"
> "$5,125.99"
> "$5,125.99"
Run Code Online (Sandbox Code Playgroud)
所以 - 这是另一个结果。我需要一种方法来格式化 PHP 和 JS 之间的货币一致性。我该怎么做呢?
更新1:
我的本地机器的 ICU 版本是 64.2,而我的服务器是 4.2.1。我会看看我的托管服务提供商是否可以将 ICU 版本更新到最新版本。这可能解释了我的本地机器输出与我的服务器输出之间的差异。
仍然不确定为什么 JS 的行为不同。
更新 2:
我的托管公司说因为cPanel,我需要坚持ICU 4.2.1版本。虽然 cPanel 有一些关于如何升级 ICU 版本的提示,但显然不建议这样做。
更新 3:
我现在正在考虑让我的 JS 对一个处理数字格式的 PHP 方法进行 Ajax 调用,这样我就可以确定我得到了相同的格式输出。不过,感觉像是一个缓慢而昂贵的解决方案。
我尝试了这种方法:
\n\n<?php\n\nfunction process($locale, $value, $currency){\n $fmt = numfmt_create($locale, NumberFormatter::CURRENCY );\n echo "Locale: $locale, Currency: $currency, Result: " . $fmt->formatCurrency($value, $currency) . "\\n";\n}\n\n$locales = ["tl_PH","fil_PH", "en_US"];\n$currencies = ["USD", "PHP"];\n$value = 5125.99;\n\nforeach ($locales as $locale){\n foreach ($currencies as $currency){\n process($locale, $value, $currency);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n并得到一致的结果:
\n\nLocale: tl_PH, Currency: USD, Result: $5,125.99\nLocale: tl_PH, Currency: PHP, Result: \xe2\x82\xb15,125.99\nLocale: fil_PH, Currency: USD, Result: $5,125.99\nLocale: fil_PH, Currency: PHP, Result: \xe2\x82\xb15,125.99\nLocale: en_US, Currency: USD, Result: $5,125.99\nLocale: en_US, Currency: PHP, Result: PHP\xc2\xa05,125.99\nRun Code Online (Sandbox Code Playgroud)\n\n因此,为了 100% 安全,我建议创建一个小型服务器端组件用于格式化目的,以便您可以传递区域设置、值和货币,并且始终获得相同的结果,无论是后端还是前端需要格式化的字符串。
\n\n另请注意,tl-PH和tl_PH并不相同。一致地使用tl_PHandfil_PH和en_US。