skip to Main Content

I have been working on trying to get Azure translator to convert text stored in a database column. Here is a couple of examples of how the text is currently stored:
eg1. "Add %%objectives%% from predefined sets of %%objectives%%"
eg2. %%Risk%%
eg3. some text here %%model%%. Please refresh the page.

My goal is to translate everything but the data within the % %. The problem is with Azure translate it has to be within the syntax of <div class="notranslate">" "" which means I have to replace all of the %% with that syntax. I was able to convert this and it works with only 1 within the string but everything else seemed to go down a rabbit hole. Here is my code:

            english = "Add %%objectives%% from predefined sets of %%objectives%%";

            if (english.Contains("%%"))
            {
                Dictionary<int, int> positions = new Dictionary<int, int>(); // this is to hold the locations of where delims are in string
                ArrayList l = new ArrayList();
                char[] letters = english.ToCharArray();
                // get the first location of % 
                for (int i = 0; i < english.Length; i++)
                {
                    if (letters[i] == '%')
                    {
                        l.Add(i);
                    }

                }

                string temp = ""; 

                // only works if theres 1 % in the string
                if (l.Count == 4)
                {
                    int loc = english.IndexOf('%'); //%%Model%% = 0 
                    int lastloc = english.LastIndexOf('%');
                    temp = " <div class="notranslate">" + english.Substring(loc + 2, (lastloc - 3) - loc) + "</div>";
                    var lang = Translate(convert(english, temp), "en", "it");
                    // need to convert back to %% 
                    Console.WriteLine(lang);
                    dataNode.SelectSingleNode("value").InnerText = lang;
                }
                else if (l.Count > 4) //this means that there are more than 1 delimted 
                {
                    foreach(int i in l) // 4 , 5 ,      16 ,17,    43, 44 
                                        // %   %  text  %  % text   %   %
                    {

                    }

Any help is appreciated!

2

Answers


  1. Do not replace the ‘%%’. Insert the <div class="notranslate"> before any odd sequence number ‘%%’ and insert the </div> after any even sequence number ‘%%’. This way, the translation retains the %% markup.

    Login or Signup to reply.
  2. If you have the option to translate the string after the variables have been replaced with real, human-readable values, you will get a better translation out of it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search