fork download
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Xml.Linq;
  13. using System.Web;
  14. using System.Windows.Controls;
  15. using System.Linq;
  16. using System.Diagnostics;
  17.  
  18. namespace Label_Translator
  19. {
  20.  
  21. /// <summary>
  22. /// Логика взаимодействия для MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. private string _content;
  27. private const string key = "SECRET_KEY";
  28.  
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. }
  33.  
  34. private void OpenFileButton (TextBox path, TextBox content)
  35. {
  36. var ofd = new OpenFileDialog();
  37. ofd.Filter = "Текстовый файл (*.txt) | *.txt";
  38.  
  39. if ((bool)!ofd.ShowDialog()) return;
  40.  
  41. path.Text = ShowFilePath(ofd);
  42. content.Text = ShowFile(ofd);
  43.  
  44.  
  45. }
  46.  
  47. private string ShowFilePath(OpenFileDialog ofd) => Path.GetFullPath(ofd.FileName);
  48.  
  49. private string ShowFile(OpenFileDialog ofd)
  50. {
  51. _content = File.ReadAllText(ofd.FileName);
  52. return _content;
  53.  
  54. }
  55.  
  56. private void OpenFileButtonRu_Click(object sender, RoutedEventArgs e) => OpenFileButton(ShowFilePathRu, ShowFileRu);
  57.  
  58. private void OpenFileButtonEn_Click(object sender, RoutedEventArgs e) => OpenFileButton(ShowFilePathEn, ShowFileEn);
  59.  
  60. private async void TranslateButton_Click(object sender, RoutedEventArgs e)
  61. {
  62. var russianWords = new List<string>();
  63. var textWithoutRu = Regex.Replace(_content, "[а-яА-ЯёЁ ]+", m => {
  64. if (m.Value == " ") return " ";
  65. russianWords.Add(m.Value);
  66. return $"{{{russianWords.Count - 1}}}";
  67. });
  68. var translatedRussianWords = await TranslateRussianList(russianWords);
  69. ShowFileMod.Text = string.Format(textWithoutRu, translatedRussianWords.ToArray());
  70. }
  71.  
  72. private void SaveButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. SaveFileDialog sfd = new SaveFileDialog();
  75. sfd.Filter = "Текстовый файл (*.txt) | *.txt";
  76.  
  77. if (sfd.ShowDialog() == true)
  78. {
  79. File.WriteAllText(sfd.FileName, ShowFileMod.Text);
  80. }
  81. }
  82.  
  83. private async Task<List<string>> TranslateRussianList(List<string> words)
  84. {
  85.  
  86. var uri = $"https://a...content-available-to-author-only...r.com/translate?api-version=3.0&from=ru&to=en";
  87. object[] body = words.Select(t => new { Text = t }).ToArray();
  88. Debug.WriteLine(body);
  89.  
  90. var RequestBody = JsonConvert.SerializeObject(body);
  91. Debug.WriteLine(RequestBody);
  92.  
  93.  
  94.  
  95. using (var client = new HttpClient())
  96. using (var request = new HttpRequestMessage())
  97. {
  98. request.Method = HttpMethod.Post;
  99. request.RequestUri = new Uri(uri);
  100. request.Content = new StringContent(RequestBody, Encoding.UTF8, "application/json");
  101. request.Headers.Add("Ocp-Apim-Subscription-Key", key);
  102. request.Headers.Add("Ocp-Apim-Subscription-Region", "northeurope");
  103.  
  104. var response = await client.SendAsync(request);
  105. var ResponseBody = await response.Content.ReadAsStringAsync();
  106. Debug.WriteLine(ResponseBody);
  107.  
  108. var result = JsonConvert.DeserializeObject<List<Dictionary<string, List<Dictionary<string, string>>>>>(ResponseBody);
  109.  
  110. var TranslatedWords = result.Select(t => t["translations"][0]["text"]);
  111.  
  112. return TranslatedWords.ToList();
  113. }
  114.  
  115. }
  116. }
  117. }
  118.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(2,7): error CS0246: The type or namespace name `Newtonsoft' could not be found. Are you missing an assembly reference?
prog.cs(12,18): error CS0234: The type or namespace name `Linq' does not exist in the namespace `System.Xml'. Are you missing `System.Xml.Linq' assembly reference?
prog.cs(14,22): error CS0234: The type or namespace name `Controls' does not exist in the namespace `System.Windows'. Are you missing an assembly reference?
prog.cs(24,39): error CS0246: The type or namespace name `Window' could not be found. Are you missing an assembly reference?
prog.cs(34,38): error CS0246: The type or namespace name `TextBox' could not be found. Are you missing an assembly reference?
prog.cs(34,52): error CS0246: The type or namespace name `TextBox' could not be found. Are you missing an assembly reference?
prog.cs(47,37): error CS0246: The type or namespace name `OpenFileDialog' could not be found. Are you missing an assembly reference?
prog.cs(49,33): error CS0246: The type or namespace name `OpenFileDialog' could not be found. Are you missing an assembly reference?
prog.cs(56,60): error CS0246: The type or namespace name `RoutedEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(58,60): error CS0246: The type or namespace name `RoutedEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(60,65): error CS0246: The type or namespace name `RoutedEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(72,54): error CS0246: The type or namespace name `RoutedEventArgs' could not be found. Are you missing an assembly reference?
Compilation failed: 12 error(s), 0 warnings
stdout
Standard output is empty