Ejemplo n.º 1
0
        async Task <string> GetTranslatedWEBVTT(string uri, string inputLanguage, string outputLanguage)
        {
            string translatedContent = string.Empty;

            if (!string.IsNullOrEmpty(uri) && !string.IsNullOrEmpty(inputLanguage) && !string.IsNullOrEmpty(outputLanguage))
            {
                string content = await GetContent(uri);

                if (!string.IsNullOrEmpty(content))
                {
                    TextBoxLogWriteLine("Original Subtitles downloaded");
                    List <SubtitileItem> SubtitleList = ParseWEBVTT(content);
                    if (SubtitleList.Count > 0)
                    {
                        TextBoxLogWriteLine("Original Subtitles parsed");
                        translatedContent += "\xFEFF";
                        translatedContent += "WEBVTT\r\n\r\n";
                        SubtitileItem newItem = new SubtitileItem("", "", "");
                        bool          bError  = false;
                        foreach (SubtitileItem item in SubtitleList)
                        {
                            newItem.startTime = item.startTime;
                            newItem.endTime   = item.endTime;
                            if (!string.IsNullOrEmpty(item.subtitle))
                            {
                                newItem.subtitle = await _ttc.Translate(item.subtitle, inputLanguage, outputLanguage);

                                if (!string.IsNullOrEmpty(newItem.subtitle))
                                {
                                    translatedContent += newItem.ToString();
                                }
                                else
                                {
                                    bError = true;
                                    break;
                                }
                            }
                        }
                        if (bError == true)
                        {
                            TextBoxLogWriteLine("Error while translating subtitles at " + newItem.startTime);
                        }
                        else
                        {
                            TextBoxLogWriteLine("Translating subtitles done:" + translatedContent);
                        }
                    }
                }
                else
                {
                    TextBoxLogWriteLine("Error while downloading subtitles: subtitle string empty");
                }
            }
            return(translatedContent);
        }