如何在 ASP.NET MVC 页面上显示波斯数字?
我正在建立一个需要支持英语和波斯语的网站。该网站是使用 ASP.NET MVC 3 和 .NET 4 (C#) 构建的。我的所有控制器都继承自 BaseController,它将文化设置为“fa-IR”(在测试期间):
Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa-IR");
在我看来,我使用静态帮助器类来转换为正确的时区并格式化日期。就像:
DateFormatter.ToLocalDateAndTime(model.CreatedOnUtc)
我正在使用 MoneyFormatter 对金钱做同样的事情。对于金钱,货币以波斯语正确打印(或者,至少我这么认为,因为我不懂波斯语。稍后将由我们的翻译人员验证)。日期也是如此,月份打印正确。这是它当前显示的内容:
对于金钱:
قیمت: ريال 1,000
对于日期:
21:01 ديسمبر 3
在这些示例中,我希望数字也以波斯语打印。你如何做到这一点?
我还尝试
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
按照某些论坛的建议在 HEAD 标签中添加:。但不会改变任何东西(据我所见)。我还在我的浏览器语言(IE 和 Firefox)中添加了“fa-IR”作为第一语言。也没有帮助。
有人对如何解决这个问题有任何想法吗?如果可能的话,我宁愿避免在英语数字和波斯语数字之间创建转换表。
谨致问候,
埃里克
I'm building a site which needs to support both English and Persian language. The site is built with ASP.NET MVC 3 and .NET 4 (C#). All my controllers inherit from a BaseController, which sets culture to "fa-IR" (during test):
Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa-IR");
In my views, I'm using static helper classes to convert into right timezone and to format date. Like:
DateFormatter.ToLocalDateAndTime(model.CreatedOnUtc)
I'm doing the same for money with a MoneyFormatter. For money, the currency prints correctly in Persian (or, at least I think so as I don't know any Persian. It will be verified later on though by our translators). The same goes for dates, where the month is printed correctly. This is what it displays as currently:
For money:
قیمت: ريال 1,000
For dates:
21:01 ديسمبر 3
In these examples, I want the numbers to also print in Persian. How do you accomplish that?
I have also tried adding:
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
in the HEAD tag, as recommended on some forums. Does not change anything though (from what I can see). I have also added "fa-IR" as the first language in my browser languages (both IE and Firefox). Didn't help either.
Anyone got any ideas on how to solve this? I'd rather avoid creating translation tables between English numbers and Persian numbers, if possible.
Best regards,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过进一步的研究,我找到了一位 Microsoft 员工的答案,指出他们不翻译数字,尽管完全可以使用 NativeDigits 属性提供的特定区域性的数字数组自行翻译(请参阅 http://msdn.microsoft.com/en-us/library /system.globalization.numberformatinfo.nativedigits.aspx)。
为了查明以表单提交的文本是阿拉伯语还是拉丁语,我现在正在做:
为了将波斯数字转换为“拉丁”等价物,我编写了这个助手:
在模型绑定发生之前,我也已经陷入其中在那里,我将表单中的纯数字输入转换为拉丁数字(如果适用)。
Ako,对于什么方向,我们设法使用网页 html 标签上的“dir”属性(dir => 方向)解决了相当多的问题。像这样:
“dir”属性采用“rtl”(从右到左)或“ltr”(从左到右)。
希望这对某人有帮助!
After some further research I found an answer from a Microsoft employee stating that they don't translate numbers, though it's fully possible to do it yourself using the array of digits for a specific culture, that is provided by the NativeDigits property (see http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.nativedigits.aspx).
To find out if text that is submitted in a form is Arabic or Latin I'm now doing:
And to convert Persian digits into their "Latin" equivalents, I wrote this helper:
I've also hooked in before model binding takes place and there I convert digit-only input from forms into Latin digits, if applicable.
Ako, for what direction goes we managed to solve quite a bit of issues using the 'dir' attribute (dir => direction) on the html tag for our Web page. Like this:
The 'dir' attribute takes either "rtl" (right-to-left) or "ltr" (left-to-right).
Hope this helps someone!
也许你必须改变字体。
如果您知道您的客户端支持特定字体,例如 Badr 或 Nazanin,那么您可以更改这些数字的字体系列,我认为这对您有用。
您可以为您的页面提供字体,但我认为这只适用于现代浏览器,而在旧浏览器中则失败。您可以检查这一点。
希望有帮助。
Maybe you have to change the font.
If you know your client supports a particular font, say Badr or Nazanin, then you can change the font-family for those numbers, and I think that will work for you.
You can supply for the font for your page, but I think this only works in modern browsers and fails in the old ones. You can check this.
Hope that helps.
您如何将数字打印到输出?我在本地化方面没有太多经验,但您可能必须使用适当的
NumberFormatInfo
或类似的数字格式。另外,为了获得最大的可移植性,您可能应该使用 UTF8 作为编码。
How are you printing the numbers to the output? I don't have much experience with localization but you might have to use the appropriate
NumberFormatInfo
or something similar to format the number.Also for greatest portability you should probably be using UTF8 as the encoding.
首先,您应该使用支持波斯数字的字体。
我使用这种技术,可以在我的网站上显示波斯号码。
字体如:BNazanin。
firs of all you should use a font-face that supports Persian number.
I use this technique and I can show Persian number on my web site.
fonts like:BNazanin.