如何在具有严格文档类型的 HTML 页面中查看阿拉伯/波斯数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5635939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I view Arabic/Persian numbers in a HTML page with strict doctype?
提问by Mahdi
I have an HTML page that is right-to-left. When I don't use any doctype, my numbers are in Arabic/Persian, but when I use strict mode they turn to English.
我有一个从右到左的 HTML 页面。当我不使用任何文档类型时,我的数字是阿拉伯语/波斯语,但是当我使用严格模式时,它们会变成英语。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">
Before adding doctype:
在添加文档类型之前:
After adding doctype:
添加文档类型后:
also I added these meta tags to my page:
我还向我的页面添加了这些元标记:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="fa" />
So how can I view Arabic numbers in a page with strict doctype (in IE because Firefox doesn't show Arabic numbers anyway)?
那么如何在具有严格文档类型的页面中查看阿拉伯数字(在 IE 中,因为 Firefox 无论如何都不显示阿拉伯数字)?
回答by Ashkan Ghodrat
here is a little javascript code that converts a 1234 string to ???? string
这是一些将 1234 字符串转换为 ???? 的 javascript 代码。细绳
var map =
[
"&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
"&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
];
function getArabicNumbers(str)
{
var newStr = "";
str = String(str);
for(i=0; i<str.length; i++)
{
newStr += map[parseInt(str.charAt(i))];
}
return newStr;
}
回答by Ali Eshghi
You can convert English digits to Persian digits using this JavaScript code in your template:
您可以使用模板中的此 JavaScript 代码将英文数字转换为波斯数字:
<script type="text/javascript">
var replaceDigits = function() {
var map = ["&\#1776;","&\#1777;","&\#1778;","&\#1779;","&\#1780;","&\#1781;","&\#1782;","&\#1783;","&\#1784;","&\#1785;"]
document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fa" lang="fa" dir="rtl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title here</title>
</head>
<body>
<p>Text here</p>
</body>
</html>
) { return map[<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="fa" dir="rtl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title here</title>
</head>
<body>
<p>Text here</p>
</body>
</html>
]});
}
window.onload = replaceDigits;
</script>
回答by kevinji
Assuming you want an XHTML 1.0 Strict document:
假设您想要一个 XHTML 1.0 Strict 文档:
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8" />
<title>Title here</title>
</head>
<body>
<p>Text here</p>
</body>
</html>
Here's an equivalent HTML 4.01 Strict document:
这是一个等效的 HTML 4.01 Strict 文档:
<html>
<head>
</head>
<body>
<style type="text/css">
@font-face {
font-family: ArabicTwo_Bold;
src: url( ArabicTwo_Bold.eot);
}
@font-face {
font-family: ArabicTwo_Bold;
src: url( ArabicTwo_Bold.ttf);
}
div , input {
font-family: ArabicTwo_Bold;
}
</style>
<input type='text' value='??? ?? ???? 123456 ' />
<div> ??? ?? ???? 123456 </div>
</body>
</html>
Here's an equivalent HTML5 page, just for comparison purposes.
这是一个等效的 HTML5 页面,仅用于比较目的。
var map_format_arabic = ["&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;", "&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"];
$.each( $('.format_arabic'), function () {
var n=$(this).text().replace(/\d(?=[^<>]*(<|$))/g, function(<script language="JavaScript" type="text/javascript">
var replaceDigits = function() {
var map =
[
"&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
"&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
]
document.body.innerHTML =
document.body.innerHTML.replace(
/\d(?=[^<>]*(<|$))/g,
function(<script type="text/javascript">
window.onload = replaceDigits
</script>
) { return map[<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body { direction: rtl; }
</style>
</head>
<body>
??
</body>
</html>
] }
);
}
</script>
) { return map_format_arabic[function toArabicNumeral(en) {
return ("" + en).replace(/[0-9]/g, function(t) {
return "??????????".substr(+t, 1);
});
}
]});
$(this).html(n);
});
回答by Muhammad Rashed Otahbashi
It is very simple to view Arabic/Persian numbers in a HTML page.
在 HTML 页面中查看阿拉伯/波斯数字非常简单。
I solved the same problem by changing the font name,
我通过更改字体名称解决了同样的问题,
In my case I want to display the same character to all users
就我而言,我想向所有用户显示相同的字符
you can choose a font that contains Arabic-Hndi digits and import it using the css ( @font-face ) then simply use it,
您可以选择包含阿拉伯-海地语数字的字体并使用 css ( @font-face ) 导入它,然后只需使用它,
This works fine for all major browsers (IE .. Firefox .. Chrome)
这适用于所有主要浏览器(IE .. Firefox .. Chrome)
here is the full code of the page:
这是页面的完整代码:
##代码##回答by khani_mahdi
firefox default render number to latin for change this setting go to addressbar of Firefox and type about:config this page is advanced setting of firefox find this line "bidi.numeral" and double click on it if set value to "1" firefox for render text look at context. if text is persian render number to persian. if set value to "4" alwase render digit number to persian
firefox 默认渲染编号为拉丁语以更改此设置转到 Firefox 的地址栏并输入 about:config 此页面是 firefox 的高级设置 找到此行“bidi.numeral”并双击它,如果将值设置为“1” firefox 进行渲染文字看上下文。如果文本是波斯语,则将数字渲染为波斯语。如果将值设置为“4”,则始终将数字呈现为波斯语
回答by helper
If you use persian fonts like 'BNazanin' and write :
如果你使用像“BNazanin”这样的波斯字体并写:
http-equiv="Content-Type" content="text/html; charset=UTF-8"
and http-equiv="Content-Language" content="fa"
in meta tags.
http-equiv="Content-Type" content="text/html; charset=UTF-8"
并http-equiv="Content-Language" content="fa"
在元标记中。
You can then see the numbers in persian.
然后你可以看到波斯语的数字。
and if you use lang='En'
in some tags in your html page the numbers in that tag will be displayed in english.
如果您lang='En'
在 html 页面中的某些标签中使用,该标签中的数字将以英文显示。
回答by raed
回答by Ali Gh
try this , hope helps you ;)
试试这个,希望对你有帮助;)
between and
之间
##代码##then in end of body tag insert this :
然后在 body 标签的末尾插入:
##代码##回答by Konrad Rudolph
This works for me, regardless of the text direction (in Chrome, Safari, Firefox and Opera):
这对我有用,无论文本方向如何(在 Chrome、Safari、Firefox 和 Opera 中):
##代码##(Omitted the content-language
since that isn't necessary here.)
(省略了,content-language
因为这里没有必要。)
回答by Amr Ragaey
In case you need to replace some English to Arabic numerals and not the whole HTML, pass the number you need to this function.
如果您需要将一些英语替换为阿拉伯数字而不是整个 HTML,请将您需要的数字传递给此函数。
##代码##