Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Convert between Romaji and Hiragana/Katakana

, 2 Jul 2014
Rate this:
Please Sign up or sign in to vote.
This is a dirtiest trick that shows you how to convert text between Romaji and Hiragana/Katakana

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:

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

Convert Mode enum:

enum Mode
{
    Hiragana,
    Katakana,
    Romaji
}

Read data from text file:

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:

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. Smile | :)

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)

Share

About the Author

Nguyen.H.H.Dang
Software Developer (Junior)
Vietnam Vietnam
No Biography provided

Comments and Discussions

 
Questionpossible modifications PinmemberSeishin#3-Jul-14 4:00 
AnswerRe: possible modifications PinprofessionalNguyen.H.H.Dang3-Jul-14 4:37 
BugImage missing PinmvpAkhil Mittal3-Jul-14 0:07 
GeneralRe: Image missing PinprofessionalNguyen.H.H.Dang3-Jul-14 0:52 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 3 Jul 2014
Article Copyright 2014 by Nguyen.H.H.Dang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid