|
| 1 | +using DevExpress.XtraRichEdit; |
| 2 | +using DevExpress.XtraRichEdit.API.Native; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using RichEditOpenAIWebApi.BusinessObjects; |
| 5 | +using Swashbuckle.AspNetCore.Annotations; |
| 6 | +using System.Globalization; |
| 7 | +using System.Net; |
| 8 | + |
| 9 | +namespace RichEditOpenAIWebApi.Controllers |
| 10 | +{ |
| 11 | + [ApiController] |
| 12 | + [Route("[controller]/[action]")] |
| 13 | + public class LanguageController : ControllerBase |
| 14 | + { |
| 15 | + // Insert your Azure key and end point for the Language Service |
| 16 | + string languageAzureKey = ""; |
| 17 | + Uri languageEndPoint = new Uri(""); |
| 18 | + // Insert your Azure key and end point for the Translation Service |
| 19 | + string translationAzureKey = ""; |
| 20 | + Uri translationEndPoint = new Uri(""); |
| 21 | + |
| 22 | + [HttpPost] |
| 23 | + [SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))] |
| 24 | + public async Task<IActionResult> GenerateLanguageSettingsForParagraphs(IFormFile documentWithHyperlinks, [FromQuery] RichEditFormat outputFormat) |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + var languageHelper = new AzureAILanguageHelper(languageAzureKey, languageEndPoint); |
| 29 | + var translationHelper = new AzureAITranslationHelper(translationAzureKey, translationEndPoint, "westeurope"); |
| 30 | + using (var server = new RichEditDocumentServer()) |
| 31 | + { |
| 32 | + await RichEditHelper.LoadFile(server, documentWithHyperlinks); |
| 33 | + |
| 34 | + server.IterateSubDocuments(async (document) => |
| 35 | + { |
| 36 | + foreach (var paragraph in document.Paragraphs) |
| 37 | + { |
| 38 | + CharacterProperties cp = document.BeginUpdateCharacters(paragraph.Range); |
| 39 | + string paragraphText = document.GetText(paragraph.Range); |
| 40 | + if (cp.Language.Value.Latin == null && !string.IsNullOrWhiteSpace(paragraphText)) |
| 41 | + { |
| 42 | + CultureInfo? culture = null; |
| 43 | + string language = languageHelper.DetectTextLanguage(paragraphText).Result; |
| 44 | + try |
| 45 | + { |
| 46 | + culture = new CultureInfo((language)); |
| 47 | + } |
| 48 | + catch { } |
| 49 | + finally |
| 50 | + { |
| 51 | + if (culture != null) |
| 52 | + { |
| 53 | + cp.Language = new DevExpress.XtraRichEdit.Model.LangInfo(culture, null, null); |
| 54 | + if (language != "en") |
| 55 | + { |
| 56 | + Comment comment = document.Comments.Create(paragraph.Range, "Translator"); |
| 57 | + SubDocument commentDoc = comment.BeginUpdate(); |
| 58 | + string translatedText = translationHelper.TranslateText(paragraphText, language, "en").Result; |
| 59 | + commentDoc.InsertText(commentDoc.Range.Start, $"Delected Language: {language}\r\nTranslation (en): {translatedText}"); |
| 60 | + comment.EndUpdate(commentDoc); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + document.EndUpdateCharacters(cp); |
| 66 | + } |
| 67 | + }); |
| 68 | + Stream result = RichEditHelper.SaveDocument(server, outputFormat); |
| 69 | + string contentType = RichEditHelper.GetContentType(outputFormat); |
| 70 | + string outputStringFormat = outputFormat.ToString().ToLower(); |
| 71 | + return File(result, contentType, $"result.{outputStringFormat}"); |
| 72 | + } |
| 73 | + } |
| 74 | + catch (Exception e) |
| 75 | + { |
| 76 | + return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments