Click here to Skip to main content
15,883,758 members
Articles / Programming Languages / C#
Tip/Trick

How to Convert between Romaji and Hiragana/Katakana

Rate me:
Please Sign up or sign in to vote.
4.82/5 (5 votes)
2 Jul 2014CPOL 25.5K   586   5   5
This is a dirtiest trick that shows you how to convert text between Romaji and Hiragana/Katakana

Image 1

Introduction

Few years ago, when I developed a Japanese translation tool, I realized that user may not have a Japanese IME on their PC, so they cannot input a Japanese Text in my tool's textbox. Then, I wrote a method to convert between Romaji and Kana. The trick is about how I made my database (the order is the most important).

Using the Code

First, declare a List to store database:

C#
private List<string> Database = new List<string>();

Convert Mode enum:

C#
enum Mode
{
    Hiragana,
    Katakana,
    Romaji
}

Read data from text file:

C#
private void GetDatabase()
{
    using (System.IO.StreamReader sr = new System.IO.StreamReader("Database.txt"))
    {
        while (!sr.EndOfStream)
        {
            string splitMe = sr.ReadLine();
            Database.Add(splitMe);
        }
    }
}

Convert method:

C#
private string Convert(string text, Mode convertMode)
{
    text = text.ToLower();
    
    string roma = string.Empty;
    string hira = string.Empty;
    string kata = string.Empty;
    
    foreach (string row in Database)
    {
        var split = row.Split('@');
        roma = split[0];
        hira = split[1];
        kata = split[2];
        
        switch (convertMode)
        {
            case Mode.Romaji:
                text = text.Replace(hira, roma);
                text = text.Replace(kata, roma.ToUpper());
                break;
            case Mode.Hiragana:
                text = text.Replace(roma, hira);
                break;
            case Mode.Katakana:
                text = text.Replace(roma, kata);
                break;
        }
    }
    
    return text;
}

Points of Interest

This is a very simple code. Please download the source code and use this method when you have to deal with Japanese input. :)

EDIT: Thanks to Seishin#, I updated my code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionPerformance Improvement Ideas Pin
agilith29-Mar-24 4:38
agilith29-Mar-24 4:38 
Questionpossible modifications Pin
Seishin#3-Jul-14 4:00
Seishin#3-Jul-14 4:00 
AnswerRe: possible modifications Pin
Nguyen.H.H.Dang3-Jul-14 4:37
professionalNguyen.H.H.Dang3-Jul-14 4:37 
BugImage missing Pin
Akhil Mittal3-Jul-14 0:07
professionalAkhil Mittal3-Jul-14 0:07 
GeneralRe: Image missing Pin
Nguyen.H.H.Dang3-Jul-14 0:52
professionalNguyen.H.H.Dang3-Jul-14 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.