private string FormatTranslation(string translatedText, List <string> maps = null, List <string> particles = null) { // Add prefixes, trim whitespace, and capitalise words, etc. string outText = ""; switch (Type) { case TokenType.HiraganaKanji: translatedText = TextTranslator.FixYouon(translatedText); goto case TokenType.Katakana; case TokenType.Katakana: // Maps translatedText = TextTranslator.MapPhrases(translatedText, maps); // Capitalise translatedText = new CultureInfo("en").TextInfo.ToTitleCase(translatedText); // Decapitalise particles translatedText = TextTranslator.LowercaseParticles(translatedText, particles); // Attach suffixes translatedText = TextTranslator.AttachSuffixes(translatedText); // Trim and join outText = Prefix + translatedText.Trim(); break; case TokenType.Latin: default: // Replace japanese punctuation foreach (string s in PunctuationMap.Keys) { string sVal; if (PunctuationMap.TryGetValue(s, out sVal)) { translatedText = translatedText.Replace(s, sVal); } } // Join outText = Prefix + translatedText; break; } return(outText); }
// 1. Latin - Don't translate // 2. Katakana - Translate to output language // 3. Hiragana / Kanji - Translate to phonetic public string Translate(List <string> maps = null, List <string> particles = null, string languagePair = TextTranslator.LanguagePair) { string translation = ""; switch (Type) { case TokenType.HiraganaKanji: { // Get phoentic text string url = TextTranslator.GetTranslatorUrl(Text, languagePair); HtmlDocument doc = new HtmlWeb().Load(url); var webClient = new WebClient { Encoding = System.Text.Encoding.UTF8 }; var result = webClient.DownloadString(url); try { result = result.Substring(4, result.IndexOf("\"", 4, StringComparison.Ordinal) - 4); string phoneticText = result; Console.WriteLine(phoneticText); translation = FormatTranslation(phoneticText, maps, particles); break; } catch { return("Error"); } break; } case TokenType.Katakana: { // Get translated text string url = TextTranslator.GetTranslatorUrl(Text, languagePair); HtmlDocument doc = new HtmlWeb().Load(url); var webClient = new WebClient { Encoding = System.Text.Encoding.UTF8 }; var result = webClient.DownloadString(url); try { result = result.Substring(4, result.IndexOf("\"", 4, StringComparison.Ordinal) - 4); string translatedText = result; translation = FormatTranslation(translatedText, maps, particles); break; } catch { return("Error"); } break; } case TokenType.Latin: default: { translation = FormatTranslation(Text, maps, particles); break; } } return(translation); }
public void Convert(IEnumerable <string> files, CancellationToken ct) { // Convert each file foreach (string filePath in files) { // Check if function has been cancelled if called asynchronously if (ct != CancellationToken.None) { ct.ThrowIfCancellationRequested(); } if (!File.Exists(filePath)) { ConversionData nonExistentData = new ConversionData(filePath); ConversionItem item = new ConversionItem(nonExistentData, null); OnProgressEvent(ProgressEvent.FileDoesNotExist, item); continue; } // Get file details string directoryPath = Path.GetDirectoryName(filePath); string extension = Path.GetExtension(filePath); string fileName = Path.GetFileNameWithoutExtension(filePath); // Get tags TagLib.File tagFile = null; try { tagFile = TagLib.File.Create(filePath); } catch (Exception) { // ignored } string title = tagFile?.Tag.Title ?? ""; string album = tagFile?.Tag.Album ?? ""; string[] performers = tagFile?.Tag.Performers ?? new string[] {}; string[] albumArtists = tagFile?.Tag.AlbumArtists ?? new string[] {}; // Store old conversion data ConversionData oldData = new ConversionData(filePath, title, album, // Ensure values remain the same even if array is modified (string[])performers.Clone(), (string[])albumArtists.Clone()); // Check if function has been cancelled if called asynchronously if (ct != CancellationToken.None) { ct.ThrowIfCancellationRequested(); } // Translate string newFileName = TextTranslator.Translate(fileName); title = TextTranslator.Translate(title); album = TextTranslator.Translate(album); for (int i = 0; i < performers.Length; i++) { performers[i] = TextTranslator.Translate(performers[i]); } for (int i = 0; i < albumArtists.Length; i++) { albumArtists[i] = TextTranslator.Translate(albumArtists[i]); } // Check if function has been cancelled if called asynchronously if (ct != CancellationToken.None) { ct.ThrowIfCancellationRequested(); } // Replace illegal filename characters from the new filename foreach (string s in IllegalFilenameMap.Keys) { string sVal; if (IllegalFilenameMap.TryGetValue(s, out sVal)) { newFileName = newFileName.Replace(s, sVal); } } string newFilePath = directoryPath + Path.DirectorySeparatorChar + newFileName + extension; if (File.Exists(newFilePath)) { ConversionData existingData = new ConversionData(newFilePath); ConversionItem item = new ConversionItem(oldData, existingData); OnProgressEvent(ProgressEvent.FileAlreadyExists, item); continue; } // Set new tags if (tagFile != null) { tagFile.Tag.Title = title; tagFile.Tag.Album = album; tagFile.Tag.Performers = performers; tagFile.Tag.AlbumArtists = albumArtists; tagFile.Save(); } File.Move(filePath, newFilePath); // Store new conversion data ConversionData newData = new ConversionData(newFilePath, title, album, // Ensure values remain the same even if array is modified (string[])performers.Clone(), (string[])albumArtists.Clone()); // Check if function has been cancelled if called asynchronously if (ct != CancellationToken.None) { ct.ThrowIfCancellationRequested(); } // Update progress ConversionItem conversionItem = new ConversionItem(oldData, newData); OnProgressEvent(ProgressEvent.Converted, conversionItem); } OnProgressEvent(ProgressEvent.Completed); }