开发者

How to correctly uppercase Greek words in .NET?

We have ASP.NE开发者_如何学GoT application which runs different clients around the world. In this application we have dictionary for each language. In dictionary we have words in lowercase and sometimes we uppercase it in code for typographic reasons.

var greek= new CultureInfo("el-GR");
string grrr = "Πόλη";
string GRRR = grrr.ToUpper(greek); // "ΠΌΛΗ"

The problem is:

...if you're using capital letters then they must appear like this: f.e. ΠΟΛΗ and not like ΠΌΛΗ, same for all other words written in capital letters

So is it possible generically to uppercase Greek words correctly in .NET? Or should I wrote my own custom algorithm for Greek uppercase?

How do they solve this problem in Greece?


I suspect that you're going to have to write your own method, if el-GR doesn't do what you want. Don't think you need to go to the full length of creating a custom CultureInfo, if this is all you need. Which is good, because that looks quite fiddly.

What I do suggest you do is read this Michael Kaplan blog post and anything else relevant you can find by him - he's been working on and writing about i18n and language issues for years and years and his commentary is my first point of call for any such issues on Windows.


I don't know much about ASP.Net but I know how I'd do this in Java.

If the characters are Unicode, I would just post-process the output from ToUpper with some simple substitutions, one being the conversion of \u038C (Ό) to \u039F (Ο) or \u0386 (Ά) to \u0391 (Α).

From the looks of the Greek/Coptic code page (\u0370 through \u03ff), there's only a few characters (6 or 7) you'll need to change.


Check out How do I remove diacritics (accents) from a string in .NET?


How about replacing the wrong characters with the right ones:

/// <summary>
/// Returns the string to uppercase using Greek uppercase rules.
/// </summary>
/// <param name="source">The string that will be converted to uppercase</param>
public static string ToUpperGreek(this string source)
{
    Dictionary<char, char> mappings = new Dictionary<char, char>(){
        {'Ά','Α'}, {'Έ','Ε'}, {'Ή','Η'}, {'Ί','Ι'}, {'Ό','Ο'}, {'Ύ','Υ'}, {'Ώ','Ω'}
    };

    source = source.ToUpper();

    char[] result = new char[source.Length];
    for (int i = 0; i < result.Length; i++)
    {
        result[i] = mappings.ContainsKey(source[i]) ? mappings[source[i]] : source[i];
    }

    return new string(result);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜