Alex Rivera | Logout

How to group Japanese and other non-Latin names in LongListSelector?

Asked 2012-02-02T18:11:46.710
8

If you put your Windows Phone Emulator or device into Japanese, Korean, or other non latin languages and use the people app, their implementation of the LongListSelector shows Japanese grouping characters, then a unicode "globe with meridians" character, followed by a-z characters:

WP7 People App WP7 People App

With the LongListSelector from the Windows Phone toolkit, you have to do your own manual grouping logic. How do I get the list of Japanese/Korean/etc name grouping characters and how do I determine what grouping character a name goes under (since looking at my 2nd screenshot, the grouping character is nowhere in the users name)?

Edit
Report

1 Answer

3

The short answer is: you add 96 to the unicode value of hiragana (ま) to get the equivalent katakana (マ).

You can determine if a character is hiragana by checking that it's unicode value falls in the range 3040-309F.

Unfortunately, as Noah mentions, many names are spelt using kanji: an alphabet of about 40,000 characters each with hiragana equivalents and many contextual to their surroundings. If you want to support those, you'll need to look for a Japanese language library to help you.

FYI, katakana is occasionally used to represent CAPITAL letters so that would explain their usage here. (Given Metro's lowercase preference, I would have thought katakana a better fit).

If you only want to support hiragana, here's something that should help:

const int KatakanaStartCode = 0x30A0;
const int HiraganaStartCode = 0x3040;
const int HiraganaEndCode = 0x309F;

private char GetGroupChar(string name)
{
    // Check for null/blank
    // Check for numbers, etc

    char firstChar = name[0];
    int firstCharCode = (int)firstChar;

    bool isHiragana = (HiraganaStartCode <= firstCharCode && 
        firstCharCode <= HiraganaEndCode);

    if (isHiragana)
    {
        char katakanaChar = (char)(firstCharCode + 
            (KatakanaStartCode - HiraganaStartCode));

        return katakanaChar;
    }

    return Char.ToLowerInvariant(firstChar);
}

And then:

string name = "またな たなかあ";

char s = GetGroupChar(name);

Debug.WriteLine(s); // マ
answered 2012-02-03T08:07:24.553

Your Answer