2
\$\begingroup\$

Input :

<h2>{translate} Dear Atteck {/translate} <br/></h2>{translate}Please complete registration</h5> {/translate}

Output :

<h2> Дeaр Аттeҗк<b/></h2>Плeaсe җомплeтe</һ5>

Main code:

private static readonly Dictionary<string, IOrderedEnumerable<KeyValuePair<string, string>>> TransliterationMaps = new Dictionary<string, IOrderedEnumerable<KeyValuePair<string, string>>>();

static void Main(string[] args)
{
    Transliterate();

    const string source = "<h2>{translate} Dear Atteck {/translate} <br/></h2>{translate}Please complete</h5> {/translate} ";

    var splitData = Regex.Split(source, "{/");
    string alldata = string.Empty;

    foreach (var splt in splitData)
    {
        var data = splt.Split(new[] { "{translate}" }, StringSplitOptions.None);

        if (data.Count() > 1)
        {
            data[0] = Regex.Replace(data[0], "[translate}]", string.Empty);
            var result = TransliterationMaps["tatartocyrillic"].Aggregate(data[1], (current, map) => current.Replace(map.Key, map.Value));
            alldata += data[0] + result;
        }
    }
    Console.WriteLine(alldata);
}

Transliterate() method

public static void Transliterate()
{
    var tatarToCyrillicMap = new Dictionary<string, string>();
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u0075').ToString(CultureInfo.InvariantCulture), '\u044e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u0075').ToString(CultureInfo.InvariantCulture), '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YU", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("yü", '\u044e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("Yü", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YÜ", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u0061').ToString(CultureInfo.InvariantCulture), '\u044f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YA", '\u042f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u0061').ToString(CultureInfo.InvariantCulture), '\u042f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YO", '\u0451'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u006f').ToString(CultureInfo.InvariantCulture), '\u0451'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u006f').ToString(CultureInfo.InvariantCulture), '\u0401'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u00e4').ToString(CultureInfo.InvariantCulture), '\u044f'.ToString(CultureInfo.InvariantCulture));
    .......................
    ...................
    ...............
    .........

    TransliterationMaps.Add("tatartocyrillic", tatarToCyrillicMap.OrderByDescending(m => m.Key));
}

Can this code be optimized?

\$\endgroup\$
1
  • \$\begingroup\$ You can put cyrillic or other unicode literals in double quoted strings. \$\endgroup\$ Commented Nov 19, 2014 at 13:18

2 Answers 2

3
\$\begingroup\$

Consider moving what Transliterate() does to an external dictionary in a file that can be parsed by your program at runtime. You don't want to recompile your program every time you want to add something or change something in that map.

\$\endgroup\$
2
\$\begingroup\$

I am puzzled by the complicated expressions such as

('\u0079' + "" + '\u0075').ToString(CultureInfo.InvariantCulture)

Why not just write Unicode character escape sequences directly in string literals?

"\u0079\u0075"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.