[Coding] C# – Converting English numerals into Thai numerals
Posted by Khatharsis on July 9, 2013
One of the projects I have been working on is a C# app that generates JavaScript and/or SQL code from an Excel spreadsheet. The “need” for this app came about when I was working on a separate project, building on a tangent to Thai Typing. Thai Typing relies heavily on an array of glyph objects containing properties, particularly the Thai letter, the associated English letter on the keyboard, and it’s position on the canvas. I wasn’t expecting to make another Thai alphabet app, but I did and unfortunately I couldn’t easily reuse the glyph array. It started to make more sense to have a database (I chose to use an Excel spreadsheet for additional reasons) and a script to generate the code for me. Everything was fairly straightforward except when I was comparing the code I generated to my existing code, I noticed the Thai numerals were being converted into English numerals. Much to my dismay, there was no easy way to leave the Thai numerals be and I spent several hours going in circles before finding a simple solution that I can’t believe it’s not in the core .NET framework.
I thought about using PHP for the code generation, but lately I’ve been going through a series of tutorials on C#. I had never coded a desktop app with a GUI before (traditionally my college projects were all command line-based) and I was inspired to try all these fun, if relatively mundane, things with it. So, I mocked up a UI within an evening and had a lot of fun. This was also after a dry spell of being out of ideas for projects, so maybe it was a little more fun than normal.
The actual code wasn’t too bad. However, the one thing that I didn’t notice until I was comparing my generated JS code to my “handwritten” JS code was the Thai numerals, which were in Thai in the Excel spreadsheet, were getting automatically converted into their English equivalents. I don’t know why this occurred since it had no issues copying the Thai letters from the Excel spreadsheet over to the JavaScript code, but it definitely took special consideration with the numerals. I wasn’t able to find any easy “off” switch for this conversion, nor could I find a simple method to convert English numerals into Thai ones. I tried setting CultureInfo and String.Format solutions that others had posted for Arabic conversions, but they didn’t work for Thai.
Eventually, I had to resort to writing a custom conversion method that converted the English numeral into the Thai unicodes. Actually, claiming that I wrote it isn’t true as I modified someone else’s solution for Arabic conversion and put in the Thai unicodes instead. It was a solution I knew must already exist as I was too lazy to write my own. Well, that and I was hoping there was an in-built solution rather than a hack, but there wasn’t.
The solution is as follows:
private string ConvertEnglishToThaiDigits(string digit)
{
return digit.Replace('0', '\u0e50')
.Replace('1', '\u0e51')
.Replace('2', '\u0e52')
.Replace('3', '\u0e53')
.Replace('4', '\u0e54')
.Replace('5', '\u0e55')
.Replace('6', '\u0e56')
.Replace('7', '\u0e57')
.Replace('8', '\u0e58')
.Replace('9', '\u0e59');
}
Simple, right? Why isn’t it readily available in .NET? I don’t know. For all of the articles I must have skimmed on MSDN, including .NET documentation, none of them were particularly helpful for performing this relatively simple task.