如何显示ASP.NET MVC页上的波斯数字? [英] How to show Persian numbers on ASP.NET MVC page?

查看:135
本文介绍了如何显示ASP.NET MVC页上的波斯数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立这需要同时支持英语和波斯语网站。该网站是建立与ASP.NET MVC 3和.NET 4(C#)。我的所有控制器从BaseController,这台文化继承为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)

我做的钱一样用MoneyFormatter。为了钱,货币打印正确波斯语(或至少我认为是这样,因为我不知道任何波斯语。这将由我们的翻译稍后进行验证虽然)。这同样适用于日期,其中,一个月被正确打印。这是它显示为当前:

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:

有关金钱:

قیمت: ريال 1,000 

有关日期:

21:01 ديسمبر 3

在这些例子中,我想这些数字同时打印波斯语。你怎么做到的?

In these examples, I want the numbers to also print in Persian. How do you accomplish that?

我也尝试添加

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />

在HEAD标签,在一些论坛的建议。不会改变任何东西,虽然(从我所看到的)。我还补充说:FA-IR作为我的语言(包括IE和Firefox)的第一语言。没有帮助的。

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

推荐答案

在一些进一步的研究,我找到了答案,从微软的一名员工指出,他们不翻译的数字,虽然这是完全有可能利用阵列来自己做为特定文化,即由NativeDigits属性提供数字(参见<一href=\"http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.nativedigits.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.nativedigits.aspx).

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:

public bool IsContentArabic(string content)
{
    string pattern = @"\p{IsArabic}";
    return Regex.IsMatch(
        content, 
        pattern, 
        RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
}

public bool IsContentLatin1(string content)
{
    string pattern = @"\p{IsBasicLatin}";
    return Regex.IsMatch(
        content, 
        pattern, 
        RegexOptions.IgnoreCase | RegexOptions.Multiline);
 }

和波斯的数字转换成自己的拉丁等价物,我写这个助手:

And to convert Persian digits into their "Latin" equivalents, I wrote this helper:

public static class NumberHelper
{
    private static readonly CultureInfo arabic = new CultureInfo("fa-IR");
    private static readonly CultureInfo latin = new CultureInfo("en-US");

    public static string ToArabic(string input)
    {
        var arabicDigits = arabic.NumberFormat.NativeDigits;
        for (int i = 0; i < arabicDigits.Length; i++)
        {
           input = input.Replace(i.ToString(), arabicDigits[i]);
        }
        return input;
    }

    public static string ToLatin(string input)
    {
        var latinDigits = latin.NumberFormat.NativeDigits;
        var arabicDigits = arabic.NumberFormat.NativeDigits;
        for (int i = 0; i < latinDigits.Length; i++)
        {
            input = input.Replace(arabicDigits[i], latinDigits[i]);
        }
        return input;
    }
}

我也模型绑定发生在那里,我从形式到拉美数字只转换数位输入,如果适用前钩住

I've also hooked in before model binding takes place and there I convert digit-only input from forms into Latin digits, if applicable.

赤穗,什么方向去,我们成功地解决了相当多的使用在我们的网页的HTML标记的'目录'属性(DIR =>方向)的问题。像这样的:

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:

<html dir="rtl">

在'目录'属性将不是RTL(从右到左)或升(左到右)。

The 'dir' attribute takes either "rtl" (right-to-left) or "ltr" (left-to-right).

希望这可以帮助别人!

这篇关于如何显示ASP.NET MVC页上的波斯数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆