Search code examples
c#.netstring-interpolation

Break up a string with HTML embedded in for translations purpose


I am on a .NET Framework project and on my C# code I am getting a value which is as follow:

object.value = "In total <B>85</B> courses.<br/><ul><li><B>13</B> have been started</li><li><B>42</B> have not been started</li><li><B>30</B> have been completed"

I am supposed to translate the text based on the language, in the above case:

  • In total
  • courses
  • have been started
  • have not been started
  • have been completed

and maintain numbers and HTML. As a outcomes I would like exactly the same layout but with the text translated.

I was gonna try something like this:

var headers = $"{0} <B>{1}</B> {2}.<br/><ul><li><B>{3}</B> {4}</li><li><B>{5}</B> {6}</li><li><B>{7}</B> {8}";

but then I am not sure how to create the arrays correctly.

Any suggestions? Thanks


Solution

  • Maybe something like this?

    var str = "In total <B>85</B> courses.<br/><ul><li><B>13</B> have been started</li>" +
        "<li><B>42</B> have not been started</li><li><B>30</B> have been completed";
    
    var parts = Regex.Split(str, @"<[A-Za-z\/]+>").Where(x => !string.IsNullOrEmpty(x));
    
    foreach(var p in parts)
        str = str.Replace(p, MyTranslateMethod(p));