private void loadFiles() { _resxFiles = new List <ResxFile>(); foreach (var item in _gridEditableData.GetFileInformationsSorted()) { ZlpSimpleFileAccessProtector.Protect( delegate { using (var reader = XmlReader.Create(item.File.FullName)) { var doc = new XmlDocument(); doc.Load(reader); doc.Normalize(); _resxFiles.Add( new ResxFile { FileInformation = item, FilePath = item.File, Document = doc }); } }); } }
TranslationLanguageInfo[] ITranslationEngine.GetSourceLanguages(string appID) { return(ZlpSimpleFileAccessProtector.Protect( delegate { const string uri = @"https://api.microsofttranslator.com/v2/Http.svc/GetLanguagesForTranslate"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Headers.Add(@"Authorization", getAuthToken(appID)); using (var response = httpWebRequest.GetResponse()) using (var stream = response.GetResponseStream()) { var dcs = new DataContractSerializer(typeof(List <string>)); var languagesForTranslate = ((List <string>)dcs.ReadObject(stream)).ToHashSet(); return languagesForTranslate .Select(l => new TranslationLanguageInfo { LanguageCode = l, UserReadableName = tryGetLanguageName(l) }).ToArray(); } })); }
TranslationLanguageInfo[] ITranslationEngine.GetSourceLanguages(string appID) { if (_sourceLanguages == null) { ZlpSimpleFileAccessProtector.Protect( delegate { var result = new List <TranslationLanguageInfo>(); var client = TranslationClient.Create(GoogleCredential.FromJson(appID)); var nodes = client.ListLanguages(@"en"); foreach (var language in nodes) { result.Add( new TranslationLanguageInfo { LanguageCode = language.Code, UserReadableName = language.Name, }); } _sourceLanguages = result.ToArray(); }); } return(_sourceLanguages); }
string[] ITranslationEngine.TranslateArray( string appID, string[] texts, string sourceLanguageCode, string destinationLanguageCode, string[] wordsToProtect, string[] wordsToRemove) { var protectionResults = new List <TranslationHelper.ProtectionResult>(); foreach (var text in texts) { var removed = TranslationHelper.RemoveWords( text, wordsToRemove); var preparedText = TranslationHelper.ProtectWords(new TranslationHelper.ProtectionInfo { UnprotectedText = removed, WordsToProtect = wordsToProtect }); protectionResults.Add(preparedText); } var tr = new List <TranslationResult>(); ZlpSimpleFileAccessProtector.Protect( delegate { // Das eigentliche Übersetzen. var client = TranslationClient.Create(GoogleCredential.FromJson(appID)); tr = client.TranslateText( protectionResults.Select(x => x.ProtectedText), destinationLanguageCode, sourceLanguageCode).ToList(); }); var dicDic = TranslationHelper.JoinUnprotectedToProtectedMapping(protectionResults); var result = new List <string>(); foreach (var translatedText in tr) { result.Add( TranslationHelper.UnprotectWords( new TranslationHelper.ProtectionResult { ProtectedText = translatedText.TranslatedText, WordsToProtect = wordsToProtect, UnprotectedToProtectedMapping = dicDic } ).UnprotectedText); } return(result.ToArray()); }
string ITranslationEngine.Translate( string appID, string text, string sourceLanguageCode, string destinationLanguageCode, string[] wordsToProtect, string[] wordsToRemove) { return(ZlpSimpleFileAccessProtector.Protect( delegate { var removed = TranslationHelper.RemoveWords( text, wordsToRemove); var prot = TranslationHelper.ProtectWords( new TranslationHelper.ProtectionInfo { UnprotectedText = removed, WordsToProtect = wordsToProtect }); var uri = $@"https://api.microsofttranslator.com/v2/Http.svc/Translate?text={HttpUtility.UrlEncode(prot.ProtectedText)}&from={ sourceLanguageCode }&to={destinationLanguageCode}"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Headers.Add(@"Authorization", getAuthToken(appID)); using (var response = httpWebRequest.GetResponse()) using (var stream = response.GetResponseStream()) { var dcs = new DataContractSerializer(typeof(string)); var translation = (string)dcs.ReadObject(stream); var protres = new TranslationHelper.ProtectionResult { ProtectedText = translation, UnprotectedToProtectedMapping = prot.UnprotectedToProtectedMapping, WordsToProtect = prot.WordsToProtect }; var unprot = TranslationHelper.UnprotectWords(protres); return unprot.UnprotectedText; } })); }
/// <summary> /// Store files to FileSystem /// </summary> private void storeFiles(Project project) { foreach (var resxFile in _resxFiles) { if (resxFile.FilePath.Exists) { if ((ZlpIOHelper.GetFileAttributes(resxFile.FilePath.FullName) & ZetaLongPaths.Native.FileAttributes.Readonly) != 0) { if (_gridEditableData?.Project != null) { switch (_gridEditableData.Project.ReadOnlyFileOverwriteBehaviour) { case ReadOnlyFileOverwriteBehaviour.Overwrite: // Simply continue to code below. break; case ReadOnlyFileOverwriteBehaviour.Ask: var h = CanOverwrite; resxFile.FilePath.Refresh(); if (h != null) { switch (h(resxFile.FilePath)) { case AskOverwriteResult.Overwrite: // Simply continue to code below. break; case AskOverwriteResult.Skip: continue; case AskOverwriteResult.Fail: throw new Exception( string.Format( Resources .SR_DataProcessing_storeFiles_Save_operation_was_cancelled_at_file, resxFile.FilePath.Name)); default: throw new ArgumentOutOfRangeException(); } } break; case ReadOnlyFileOverwriteBehaviour.Skip: continue; case ReadOnlyFileOverwriteBehaviour.Fail: throw new Exception( string.Format( Resources .SR_DataProcessing_storeFiles_Saving_failed_because_of_read_only_file, resxFile.FilePath.Name)); default: throw new ArgumentOutOfRangeException(); } } } removeReadOnlyAttributes(resxFile.FilePath); ZlpSafeFileOperations.SafeDeleteFile(resxFile.FilePath.FullName); } var settings = new XmlWriterSettings { Indent = true, IndentChars = project.ResXIndentChar, Encoding = Encoding.UTF8 }; ZlpSimpleFileAccessProtector.Protect( delegate { using (var sw = new StreamWriter( resxFile.FilePath.FullName, false, new UTF8Encoding(true))) // https://github.com/UweKeim/ZetaResourceEditor/issues/24 using (var write = XmlWriter.Create(sw, settings)) { var doc = resxFile.Document; doc.Save(write); } }); } }
string[] ITranslationEngine.TranslateArray( string appID, string[] texts, string sourceLanguageCode, string destinationLanguageCode, string[] wordsToProtect, string[] wordsToRemove) { return(ZlpSimpleFileAccessProtector.Protect( delegate { const string uri = @"https://api.microsofttranslator.com/v2/Http.svc/TranslateArray"; var body = @"<TranslateArrayRequest>" + @"<AppId />" + $@"<From>{sourceLanguageCode}</From>" + @"<Options>" + @" <Category xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"" />" + @"<ContentType xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"">text/plain</ContentType>" + @"<ReservedFlags xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"" />" + @"<State xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"" />" + @"<Uri xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"" />" + @"<User xmlns=""http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"" />" + @"</Options>" + @"<Texts>"; var protectionResults = new List <TranslationHelper.ProtectionResult>(); foreach (var text in texts) { var removed = TranslationHelper.RemoveWords( text, wordsToRemove); var preparedText = TranslationHelper.ProtectWords(new TranslationHelper.ProtectionInfo { UnprotectedText = removed, WordsToProtect = wordsToProtect }); protectionResults.Add(preparedText); var esc = escapeXml(preparedText.ProtectedText); body += $@"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">{esc}</string>"; } body += @"</Texts>" + $@"<To>{destinationLanguageCode}</To>" + @"</TranslateArrayRequest>"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri(uri); request.Content = new StringContent(body, Encoding.UTF8, @"text/xml"); request.Headers.Add(@"Authorization", getAuthToken(appID)); HttpResponseMessage response = null; string responseBody = null; // https://github.com/tejacques/AsyncBridge#example-usage using (var A = AsyncHelper.Wait) { A.Run(client.SendAsync(request), res => response = res); } using (var A = AsyncHelper.Wait) { A.Run(response?.Content.ReadAsStringAsync(), res => responseBody = res); } if (response == null) { throw new HttpException(); } switch (response?.StatusCode) { case HttpStatusCode.OK: var doc = XDocument.Parse(responseBody); var ns = XNamespace.Get( @"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"); var result = new List <string>(); // Sum up all mappings. var dic = TranslationHelper.JoinUnprotectedToProtectedMapping(protectionResults); foreach (var xe in doc.Descendants(ns + @"TranslateArrayResponse")) { result.AddRange(xe.Elements(ns + @"TranslatedText").Select(node => TranslationHelper .UnprotectWords( new TranslationHelper.ProtectionResult { ProtectedText = node.Value, WordsToProtect = wordsToProtect, UnprotectedToProtectedMapping = dic } ).UnprotectedText)); } return result.ToArray(); default: throw new HttpException((int)response.StatusCode, responseBody); } } })); }