Transliterating Russian to English in C#


/Introduction

If you are struggling with transliterating the Russian names or sentences to the English latin letters, so do I.

  • Input Russian name: Олъга Виктровна Василенко
  • Output English transliteration: Olga Viktrovna Vasilenko

Contents

Source code:

First you need to add the following fields

 private static char cap_a = 'А'; // 1040
 private static char cap_be = 'Б'; // 1041
 private static char cap_ve = 'В'; // 1042
 private static char cap_ge = 'Г'; // 1043
 private static char cap_de = 'Д'; // 1044
 private static char cap_ye = 'Е'; // 1045
 private static char cap_zhe = 'Ж'; // 1046
 private static char cap_ze = 'З'; // 1047
 private static char cap_ee = 'И'; // 1048
 private static char cap_i = 'Й'; // 1049
 private static char cap_ka = 'К'; // 1050
 private static char cap_el = 'Л'; // 1051
 private static char cap_em = 'М'; // 1052
 private static char cap_en = 'Н'; // 1053
 private static char cap_o = 'О'; // 1054
 private static char cap_pe = 'П'; // 1055
 private static char cap_er = 'Р'; // 1056
 private static char cap_es = 'С'; // 1057
 private static char cap_te = 'Т'; // 1058
 private static char cap_u = 'У'; // 1059
 private static char cap_ef = 'Ф'; // 1060
 private static char cap_kha = 'Х'; // 1061
 private static char cap_tse = 'Ц'; // 1062
 private static char cap_che = 'Ч'; // 1063
 private static char cap_sha = 'Ш'; // 1064
 private static char cap_shcha = 'Щ'; // 1065
 private static char cap_hard = 'Ъ'; // 1066
 private static char cap_yeru = 'Ы'; // 1067
 private static char cap_soft = 'Ь'; // 1068
 private static char cap_e = 'Э'; // 1069
 private static char cap_yu = 'Ю'; // 1070
 private static char cap_ya = 'Я'; // 1071
 private static char cap_yo = 'Ё'; // 1025
 private static char blank = ' '; // 0032
 private static char cap_dot_i = Convert.ToChar(1030); // cyrillic capital letter byelorussian-ukrainian i
 private static char cap_fita = Convert.ToChar(1138); // cyrillic capital letter fita
 private static char cap_yat = Convert.ToChar(1122); // cyrillic capital letter yat
 private static char cap_izhitsa = Convert.ToChar(1140); // cyrillic capital letter izhitsa

Here are the methods that we need.

private static string DisplayInEnglish(string text)
 {
 string result = string.Empty;

 for (int i = 0; i < text.Length; i++)
 {
 int charCode = 0x0;
 char russianLetter = '\0';

 russianLetter = Convert.ToChar(text.ToUpper()[i]);

 string englishLetter = string.Empty;

 if (russianLetter == cap_a)
 {
 englishLetter = "A";
 }
 else if (russianLetter == cap_be)
 {
 englishLetter = "B";
 }
 else if (russianLetter == cap_ve)
 {
 englishLetter = "V";
 }
 else if (russianLetter == cap_ge)
 {
 englishLetter = "G";
 }
 else if (russianLetter == cap_de)
 {
 englishLetter = "D";
 }
 else if (russianLetter == cap_ye || russianLetter == cap_yat)
 {
 if (AtStart(i, text) ||
 IsVowel(PrevChar(i, text)) ||
 PrevChar(i, text) == cap_hard ||
 PrevChar(i, text) == cap_soft)
 {
 englishLetter = "Ye";
 }
 else
 {
 englishLetter = "E";
 }
 }
 else if (russianLetter == cap_zhe)
 {
 englishLetter = "Zh";
 }
 else if (russianLetter == cap_ze)
 {
 englishLetter = "Z";
 }
 else if (russianLetter == cap_ee || russianLetter == cap_izhitsa)
 {
 englishLetter = "I";
 }
 else if (russianLetter == cap_i)
 {
 englishLetter = "Y"; // J ???
 }
 else if (russianLetter == cap_ka)
 {
 englishLetter = "K";
 }
 else if (russianLetter == cap_el)
 {
 englishLetter = "L";
 }
 else if (russianLetter == cap_em)
 {
 englishLetter = "M";
 }
 else if (russianLetter == cap_en)
 {
 englishLetter = "N";
 }
 else if (russianLetter == cap_o)
 {
 englishLetter = "O";
 }
 else if (russianLetter == cap_pe)
 {
 englishLetter = "P";
 }
 else if (russianLetter == cap_er)
 {
 englishLetter = "R";
 }
 else if (russianLetter == cap_es)
 {
 englishLetter = "S";
 }
 else if (russianLetter == cap_te)
 {
 englishLetter = "T";
 }
 else if (russianLetter == cap_u)
 {
 englishLetter = "U";
 }
 else if (russianLetter == cap_ef ||
 russianLetter == cap_fita)
 {
 englishLetter = "F";
 }
 else if (russianLetter == cap_kha)
 {
 englishLetter = "Kh";
 }
 else if (russianLetter == cap_tse)
 {
 englishLetter = "Ts";
 }
 else if (russianLetter == cap_che)
 {
 englishLetter = "Ch";
 }
 else if (russianLetter == cap_sha)
 {
 englishLetter = "Sh";
 }
 else if (russianLetter == cap_shcha)
 {
 englishLetter = "Shch";
 }
 else if (russianLetter == cap_soft)
 {
 englishLetter = "";
 }
 else if (russianLetter == cap_yeru)
 {
 englishLetter = "Y";
 }
 else if (russianLetter == cap_hard)
 {
 englishLetter = "";
 }
 else if (russianLetter == cap_e)
 {
 englishLetter = "E";
 }
 else if (russianLetter == cap_yu)
 {
 englishLetter = "Yu";
 }
 else if (russianLetter == cap_ya)
 {
 englishLetter = "Ya";
 }
 else if (russianLetter == cap_yo)
 {
 englishLetter = "Yo";
 }
 else if (russianLetter == cap_dot_i)
 {
 englishLetter = "I";
 }
 else if (russianLetter == blank)
 {
 englishLetter = " ";
 }
 else if (russianLetter == (char)769) // '́ <- this sign
 {
 englishLetter = string.Empty;
 }
 else
 {
 englishLetter = russianLetter.ToString();
 }

 result += englishLetter;
 }

 return result.ToUpper();
 }

private static bool IsVowel(char c)
 {
 return c == cap_a || c == cap_ye || c == cap_ee || c == cap_o || c == cap_u ||
 c == cap_yeru || c == cap_e || c == cap_yu || c == cap_ya;
 }

 private static char PrevChar(int i, string text)
 {
 if (AtStart(i, text))
 {
 return '\0';
 }

 return text[i - 1];
 }

 private static bool AtStart(int i, string text)
 {
 return ((i == 0) || (text[i - 1] == ' '));
 }

Usage:

 string result = DisplayInEnglish("Олъга Виктровна Василенко");
// output: Olga Viktrovna Vasilenko

That finishes this tutorial.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s