How to show Persian numbers on ASP.NET MVC page?

允我心安 提交于 2019-12-04 12:22:30

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.

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">

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

Hope this helps someone!

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!