使用C#中的区域性信息获取语言明智的数字

时间:2018-08-08 11:42:57

标签: c# .net numbers native cultureinfo

我正在研究语言明智的数字,并且正在通过CultureInfo进行。

CultureInfo类为阿拉伯语和古吉拉特语提供本地数字,但不为印地语提供

例如: 我的电话是英文:123456 古吉拉特语中的相同数字:૧૨૩૪૫6(这很好) 对于英语:१२३४५६ 这不起作用

显示印地语本机数字的替代选项是什么?

示例: 我想用印地语显示数字:०、१、२、३、४、५、६、७、८,९。

这是我的代码段:

CultureInfo languageculture = new CultureInfo("hi-IN");
NumberFormatInfo languageNumberFormat = languageculture.NumberFormat;
string[] nativeDigits = languageNumberFormat.NativeDigits;
string str5 = ConvertToMyLanguageDigits(i.ToString(), mydiditesgu);

转换(替换编号)方法:

private static string ConvertToMyLanguageDigits(string number, string[] myNative)
{
    string myNewMuber = string.Empty;
    myNewMuber = number;
    Regex rgx = new Regex("[^a-zA-Z0-9 -]");
    string number1 = rgx.Replace(number, "");
    var aa = number1.ToCharArray().Distinct().ToArray();
    foreach (var item in aa)
    {
        if (!myNative.Any(x => x.Contains(item)))
        {
            myNewMuber = myNewMuber.Replace(item.ToString(), myNative[int.Parse(item.ToString())].ToString());
        }
    }
    return myNewMuber;
}

1 个答案:

答案 0 :(得分:0)

假设您已在Windows上安装了语言包,您不能这样做。

var hindiCulture = new CultureInfo("hi-IN");

获得正确的文化。然后

var hindiDigits = hindiCulture.NumberFormat.NativeDigits;

获得正确的数字?


例如as demonstrated here

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        var hindiCulture = new CultureInfo("hi-IN");
        Console.WriteLine(hindiCulture.EnglishName);
        Console.WriteLine(hindiCulture.NativeName);
        Console.WriteLine(string.Join("", hindiCulture.NumberFormat.NativeDigits));
    }
}

输出

Hindi (India)
हिंदी (भारत)
०१२३४५६७८९

As demonstrated here,如果我愿意

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var hindiCulture = new CultureInfo("hi-IN");
        Console.WriteLine(hindiCulture.EnglishName);
        Console.WriteLine(hindiCulture.NativeName);
        Console.WriteLine(string.Join("", hindiCulture.NumberFormat.NativeDigits));
        Console.WriteLine(
            ConvertToMyLanguageDigits("0123456789", 
            hindiCulture.NumberFormat.NativeDigits));
    }

    private static string ConvertToMyLanguageDigits(string number, string[] myNative)
        {
            string myNewMuber = string.Empty;
            myNewMuber = number;
            Regex rgx = new Regex("[^a-zA-Z0-9 -]");
            string number1 = rgx.Replace(number, "");
            var aa = number1.ToCharArray().Distinct().ToArray();
            foreach (var item in aa)
            {
                if (!myNative.Any(x => x.Contains(item)))
                {
                    myNewMuber = myNewMuber.Replace(
                        item.ToString(), 
                        myNative[int.Parse(item.ToString())].ToString());
                }
            }
            return myNewMuber;
       }
}

输出为

Hindi (India)
हिंदी (भारत)
०१२३४५६७८९
०१२३४५६७८९

您的代码或.Net都没有问题,我怀疑您没有正确设置系统,是否已安装语言包?