2016-11-01 75 views
1

我希望在創建新產品類別時在Web層的Content文件夾中創建一個目錄,但由於類別名稱在Cirillyc和Cirillyc路徑中並不好,因此創建一個音譯名稱的文件夾。我試過UnidecodeSharpFork,但它產生不同的符號(如'),這對目錄名也不好。在這種情況下還有其他選擇嗎?從Cirillyc音譯到拉丁文

回答

1

您可以嘗試推行音譯地圖malually

// Capital letters should be transliterated by the same scheme 
public class CaseInsensitiveComparer: IEqualityComparer<char> { 
    public bool Equals(char left, char right) { 
    return char.ToUpperInvariant(left) == char.ToUpperInvariant(right); 
    } 
    public int GetHashCode(char value) { 
    return char.ToUpperInvariant(value).GetHashCode(); 
    } 
} 

... 

// implement map manually 
private static Dictionary<char, String> map = 
    new Dictionary<char, string>(new CaseInsensitiveComparer()) { 
    {'а', "a" }, 
    {'б', "b" }, 
    ... 
    {'я', "ya" }, 
}; 

然後使用它:

string source = "My string (Моя Строка)"; 

string result = string.Concat(source.Select(c => { 
    string st; 

    if (map.TryGetValue(c, out st)) 
    return char.IsUpper(c) ? st.ToUpperInvariant() : st; 
    else 
    return c.ToString(); 
})); 

測試

// My string (Moya Stroka) 
Console.Write(result);