問題描述
如何在不更改字符代碼的情況下設置阿拉伯數字的變體?
How can I set the variant of Arabic numeral without changing character codes?
Eastern Arabic ? ? ? ? ? ? ? ? ? ?
Persian variant ? ? ? ? ? ? ? ? ? ?
Western Arabic 0 1 2 3 4 5 6 7 8 9
(And other numeral systems)
這是一個示例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div lang="fa">0123456789</div>
<div lang="ar">0123456789</div>
<div lang="en">0123456789</div>
</body>
</html>
如何僅使用客戶端技術(HTML
、CSS
、JS
)來做到這一點?
該解決方案應該對頁面的 SEO 分數沒有負面影響.
How can I do this using only client-side technologies (HTML
,CSS
,JS
)?
The solution should have no negative impact on page's SEO score.
請注意,在 Windows 文本框(例如 Run)中,數字會根據周圍文本的語言正確顯示.
Note that in Windows text boxes (e.g. Run) numbers are displayed correctly according to language of surrounding text.
另請參閱:桌面應用程序中的數字本地化
注意:使用這個 PHP 包 https://github.com/salarmehr/cosmopolitan
Note: Localisation of numbers are super easy on backend using this PHP package https://github.com/salarmehr/cosmopolitan
推薦答案
一個新的(迄今為止)簡單的 JS 解決方案是使用 Intl.NumberFormat.它支持數字本地化、格式變化以及本地貨幣(有關更多示例,請參閱文檔).
A new (to date) and simple JS solution would be to use Intl.NumberFormat. It supports numeral localization, formatting variations as well as local currencies (see documentation for more examples).
使用一個與 MDN 自己的非常相似的示例:
To use an example very similar to MDN's own:
const val = 1234567809;
console.log('Eastern Arabic (Arabic-Egyptian)', new Intl.NumberFormat('ar-EG').format(val));
console.log('Persian variant (Farsi)',new Intl.NumberFormat('fa').format(val));
console.log('English (US)',new Intl.NumberFormat('en-US').format(val));
Intl.NumberFormat 似乎也支持字符串數值,并在它不是本地語言中的數字時進行指示.
Intl.NumberFormat also seems to support string numeric values as well as indicates when it's not a number in the local language.
const val1 = '456';
const val2 = 'Numeric + string example, 123';
console.log('Eastern Arabic', new Intl.NumberFormat('ar-EG').format(val1));
console.log('Eastern Arabic', new Intl.NumberFormat('ar-EG').format(val2));
console.log('Persian variant',new Intl.NumberFormat('fa').format(val1));
console.log('Persian variant',new Intl.NumberFormat('fa').format(val2));
console.log('English',new Intl.NumberFormat('en-US').format(val1));
console.log('English', new Intl.NumberFormat('en-US').format(val2));
對于語言環境標識符(傳遞給 NumberFormat
構造函數的字符串,指示語言環境),我嘗試了上面的值,它們看起來很好.我嘗試查找所有可能值的列表,并通過 MDN 遇到 本文檔和此列表可能會有所幫助.
For the locale identifier (string passed to NumberFormat
constructor indicating locale), I experimented with the values above and they seemed fine. I tried finding a list for all possible values, and through MDN came across this documentation and this list that could be helpful.
我不熟悉 SEO,因此不確定這如何回答這部分問題.
I'm not familiar with SEO, and am thus unsure how this answers that part of the question.
這篇關于Web 應用程序中的數字本地化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!