0

I need to transliterate label text from latin to Cyrillic before print. I don't have idea how to do that. I've tried like this but didn't work.

    public void Komponenta()
    {
        words.Add("A", "A");
        words.Add("Б", "B");
        words.Add("В", "C");
        words.Add("Г", "D");
        words.Add("Д", "E");
        words.Add("Ѓ", "F");
        words.Add("Е", "G");
        words.Add("Ж", "H");
                ..and so on

}


  if (Label53.Text == "4")
                {
                    string source = Label47.Text;
                    foreach (KeyValuePair<string, string> pair in words)
                    {

                        source.Replace(pair.Value, pair.Key);
                    }

                    Label47.Text = source;
Cœur
  • 37,241
  • 25
  • 195
  • 267
buba
  • 59
  • 1
  • 15
  • In C#.NET you need to assign the replaced value back to the variable as @Wai-Ha-Lee has done in his answer – Harsh Baid Feb 13 '15 at 08:01
  • @Hais Baid worked, but with wrong letters, expected value is СУПА, but it output ЦУПA, what is the problem now? – buba Feb 13 '15 at 08:57

1 Answers1

0

You want to use

source = source.Replace(pair.Value, pair.Key);

As calling Replace (MSDN link) returns the modified string, as opposed to modifying the instance.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • I assume you don't have any exceptions thrown as you don't mention it in your question. – Wai Ha Lee Feb 13 '15 at 07:50
  • I expect this label to be converted in cyrilic in my text file – buba Feb 13 '15 at 08:07
  • You did say that in your question and I didn't notice. Your dictionary needs to have the keys and values switched, e.g. `words.Add("H", "Ж");` – Wai Ha Lee Feb 13 '15 at 08:10
  • But this don't work, it doesn't give error, but label is not converted in my text file – buba Feb 13 '15 at 08:15
  • Ok,sorry it worked but this give me letters on another language, why is so that??? Expected IS СУПА but it output: ЦУПA ??????? – buba Feb 13 '15 at 08:34
  • Have you tried `Label47.Text = {some Cyrillic text};` to see if that displays it correctly? Also, try looking at `source` after you iterate over each `KeyValuePair` in the quick-watch window ([link](https://msdn.microsoft.com/en-us/library/cyzbs7s2.aspx)) (if you can with ASP.NET, that is - I'm not sure) and see if it shows up as Cyrillic there. – Wai Ha Lee Feb 13 '15 at 08:42
  • you're right, i've tried with cyrilic text and it give the same wrong characters,what can i do now? – buba Feb 13 '15 at 08:51
  • I'm not sure if it'll work for you, but have a look at [this StackOverflow answer](http://stackoverflow.com/a/12350264/1364007). For you, the code would be: `var wc = new WebClient(); wc.Encoding = Encoding.GetEncoding("windows-1251"); Label47.Text = wc.DownloadString(source);` – Wai Ha Lee Feb 13 '15 at 09:25
  • how to achive this without webClient, my page is web deployed and with web client it give me error: The remote server returned an error: (500) Internal Server Error wc.DownloadString("http://localhost:3125/Bar.aspx");??? – buba Feb 13 '15 at 09:59
  • I think you need to change the encoding for your string. I'm not sure what encoding you want to change from/to, but [this StackOverflow answer](http://stackoverflow.com/a/1922253/1364007) says *how* to change encoding. – Wai Ha Lee Feb 13 '15 at 10:10
  • It might also be useful for you to read [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html). – Wai Ha Lee Feb 13 '15 at 10:12