Is it posible to convert Cyrillic string to English(Latin) in c#? For example I need to convert \"Петролеум\" in \"Petroleum\". Plus I forgot to mention that if I have Cyril
This is solution for serbian cyrillic-latin transliteration for form like this: form
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Dictionary<string, string> slova = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
slova.Add("Љ", "Lj");
slova.Add("Њ", "Nj");
slova.Add("Џ", "Dž");
slova.Add("љ", "lj");
slova.Add("њ", "nj");
slova.Add("џ", "dž");
slova.Add("а", "a");
slova.Add("б", "b");
slova.Add("в", "v");
slova.Add("г", "g");
slova.Add("д", "d");
slova.Add("ђ", "đ");
slova.Add("е", "e");
slova.Add("ж", "ž");
slova.Add("з", "z");
slova.Add("и", "i");
slova.Add("ј", "j");
slova.Add("к", "k");
slova.Add("л", "l");
slova.Add("м", "m");
slova.Add("н", "n");
slova.Add("о", "o");
slova.Add("п", "p");
slova.Add("р", "r");
slova.Add("с", "s");
slova.Add("т", "t");
slova.Add("ћ", "ć");
slova.Add("у", "u");
slova.Add("ф", "f");
slova.Add("х", "h");
slova.Add("ц", "c");
slova.Add("ч", "č");
slova.Add("ш", "š");
}
// Method for cyrillic to latin
private void button1_Click(object sender, EventArgs e)
{
string source = textBox1.Text;
foreach (KeyValuePair<string, string> pair in slova)
{
source = source.Replace(pair.Key, pair.Value);
// For upper case
source = source.Replace(pair.Key.ToUpper(),
pair.Value.ToUpper());
}
textBox2.Text = source;
}
// Method for latin to cyrillic
private void button2_Click(object sender, EventArgs e)
{
string source = textBox2.Text;
foreach (KeyValuePair<string, string> pair in slova)
{
source = source.Replace(pair.Value, pair.Key);
// For upper case
source = source.Replace(pair.Value.ToUpper(),
pair.Key.ToUpper());
}
textBox1.Text = source;
}
}
}
If you're using Windows 7, you can take advantage of the new ELS (Extended Linguistic Services) API, which provides transliteration functionality for you.
Have a look at the Windows 7 API Code Pack - it's a set of managed wrappers on top of many new API in Windows 7 (such as the new Taskbar). Look in the Samples
folder for the Transliterator
example, you'll find it's exactly what you're looking for:
I'm not familiar with Cyrillic, but if it's just a 1-to-1 mapping of Cyrillic characters to Latin characters that you're after, you can use a dictionary of character pairs and map each character individually:
var map = new Dictionary<char, string>
{
{ 'П', "P" },
{ 'е', "e" },
{ 'т', "t" },
{ 'р', "r" },
...
}
var result = string.Concat("Петролеум".Select(c => map[c]));
http://code.google.com/apis/ajaxlanguage/documentation/#Transliteration
Google offer this AJAX based transliteration service. This way you can avoid computing transliterations yourself and let Google do them on the fly. It'd mean letting the client-side make the request to Google, so this means your app would need to have some kind of web-based output for this solution to work.