public static void GetListBoxToolStripTop(ToolStrip toolWorkItemTopBar)
        {
            foreach (ToolStripItem item in toolWorkItemTopBar.Items)
            {
                System.Diagnostics.Trace.WriteLine(item.Name);
            }

            ToolStripComboBox ctrlList = (ToolStripComboBox)toolWorkItemTopBar.Items["ctlQuery"];
            ToolStripLabel    test     = (ToolStripLabel)toolWorkItemTopBar.Items["labelShowQuery"];

            test.Text = "coucou";
            test.Invalidate();
            ctrlList.PerformClick();
            //        ctrlLst.PerformClick();
            System.Diagnostics.Trace.WriteLine("Selected item : " + ctrlList.SelectedItem + " Nombre item " + ctrlList.Items.Count);
            ctrlList.SelectedIndexChanged += new EventHandler(ctrlList_SelectedIndexChanged);
        }
예제 #2
0
        public static string[] Translate(string[] text, string sourceLanguageCode, string destLanguageCode, ToolStripProgressBar tspb, ToolStripLabel tsl)
        {
            List <List <string> > textChunks = new List <List <string> >();
            List <string>         currChunk  = new List <string>();
            int currChunkLength = 0;

            foreach (string str in text)
            {
                if (currChunkLength + str.Length > CHAR_LIMIT)
                {
                    textChunks.Add(currChunk);
                    currChunk = new List <string>();
                }
                currChunk.Add(str);
                currChunkLength += str.Length;
            }
            if (currChunk.Count > 0)
            {
                textChunks.Add(currChunk);
            }
            List <string> result = new List <string>();
            int           cpt    = 0;
            int           max    = textChunks.Sum((x) => x.Count);
            Stopwatch     sw     = new Stopwatch();

            sw.Start();
            foreach (List <string> chunk in textChunks)
            {
                tspb.Value = (cpt / max) * 100;
                tsl.Text   = $"Translated {cpt}/{max}";
                tspb.Invalidate();
                tsl.Invalidate();
                string chunkText = Uri.EscapeDataString(chunk.Aggregate((left, right) => left += DELIMITER.ToString() + right));
                Console.WriteLine(GetTranslationURL(sourceLanguageCode, destLanguageCode, chunkText));
                if (CurrentRequestCount >= REQUEST_LIMIT - 1)
                {
                    int timeToSleep = Convert.ToInt32((REQUEST_LIMIT_RESET - (sw.ElapsedMilliseconds / 1000)) * 1000);
                    MessageBox.Show($"Request limit reached, sleeping for {timeToSleep / 1000} seconds");
                    Thread.Sleep(timeToSleep + 1000);
                    CurrentRequestCount = 0;
                    sw.Restart();
                }
                WebRequest  rq = WebRequest.Create(GetTranslationURL(sourceLanguageCode, destLanguageCode, chunkText));
                WebResponse wr = rq.GetResponse();
                CurrentRequestCount++;
                string tmpTxt = wr.GetResponseStream().ReadToEnd();

                /**
                 * Sample return :
                 * [[["Hello","hola",null,null,1]],null,"es",null,null,null,0.91015625,null,[["es"],null,[0.91015625],["es"]]]
                 *     ^^^^^ What we need
                 */
                IEnumerable <char> tmp            = tmpTxt.Split(',')[0].Skip(4);
                string             resultTxt      = new string(tmp.Take(tmp.Count() - 1).ToArray());
                string[]           splitDelimiter = new[] { "" };
                foreach (char c in DELIMITER)
                {
                    splitDelimiter[0] += c;
                }
                result.AddRange(resultTxt.Split(splitDelimiter, StringSplitOptions.None));
                cpt += chunk.Count;
            }
            return(result.ToArray());
        }