Skip to content

Commit

Permalink
Work on xml content translator
Browse files Browse the repository at this point in the history
  • Loading branch information
niksedk committed Dec 21, 2018
1 parent 6a03d34 commit a754488
Show file tree
Hide file tree
Showing 16 changed files with 2,322 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin
obj
/src/XmlContentTranslator/XmlContentTranslator.csproj.DotSettings
/src/XmlContentTranslator/XmlContentTranslator.zip
/src/.vs/XmlContentTranslator/v15
10 changes: 5 additions & 5 deletions src/XmlContentTranslator/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 26 additions & 107 deletions src/XmlContentTranslator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;
using System.Xml;
using XmlContentTranslator.Translator;

namespace XmlContentTranslator
{
Expand Down Expand Up @@ -601,35 +603,30 @@ private void GoogleTranslateSelectedLines()
string oldText = string.Empty;
string newText = string.Empty;

if (listViewLanguageTags.SelectedItems.Count > 10)
{
toolStripStatusLabel1.Text = "Translating via Google Translate. Please wait...";
Refresh();
}
toolStripStatusLabel1.Text = "Translating via Google Translate. Please wait...";
Refresh();

var translator = new GoogleTranslator1();
Cursor = Cursors.WaitCursor;
var sb = new StringBuilder();
var res = new StringBuilder();
var oldLines = new List<string>();
var list = new List<string>();
foreach (ListViewItem item in listViewLanguageTags.SelectedItems)
{
oldText = item.SubItems[1].Text;
oldLines.Add(oldText);
var urlEncode = HttpUtility.UrlEncode(sb + newText);
if (urlEncode != null && urlEncode.Length >= 1000)
if (urlEncode.Length >= 1000)
{
res.Append(TranslateTextViaScreenScraping(sb.ToString(), (comboBoxFrom.SelectedItem as ComboBoxItem).Value + "|" + (comboBoxTo.SelectedItem as ComboBoxItem).Value));
sb = new StringBuilder();
}
list.Add(oldText);
sb.Append("== " + oldText + " ");
}
res.Append(TranslateTextViaScreenScraping(sb.ToString(), (comboBoxFrom.SelectedItem as ComboBoxItem).Value + "|" + (comboBoxTo.SelectedItem as ComboBoxItem).Value));

var lines = new List<string>();
foreach (string s in res.ToString().Split(new string[] { "==" }, StringSplitOptions.None))
lines.Add(s.Trim());
lines.RemoveAt(0);

var log = new StringBuilder();
var lines = translator.Translate(((ComboBoxItem)comboBoxFrom.SelectedItem).Value, ((ComboBoxItem)comboBoxTo.SelectedItem).Value, list, log).ToList();
if (listViewLanguageTags.SelectedItems.Count != lines.Count)
{
MessageBox.Show("Error getting/decoding translation from google!");
Expand Down Expand Up @@ -731,97 +728,10 @@ private void setValueFromMasterToolStripMenuItem_Click(object sender, EventArgs

public static void FillComboWithLanguages(ComboBox comboBox)
{
comboBox.Items.Add(new ComboBoxItem("AFRIKAANS", "af"));
comboBox.Items.Add(new ComboBoxItem("ALBANIAN", "sq"));
comboBox.Items.Add(new ComboBoxItem("AMHARIC", "am"));
comboBox.Items.Add(new ComboBoxItem("ARABIC", "ar"));
comboBox.Items.Add(new ComboBoxItem("ARMENIAN", "hy"));
comboBox.Items.Add(new ComboBoxItem("AZERBAIJANI", "az"));
comboBox.Items.Add(new ComboBoxItem("BASQUE", "eu"));
comboBox.Items.Add(new ComboBoxItem("BELARUSIAN", "be"));
comboBox.Items.Add(new ComboBoxItem("BENGALI", "bn"));
comboBox.Items.Add(new ComboBoxItem("BIHARI", "bh"));
comboBox.Items.Add(new ComboBoxItem("BULGARIAN", "bg"));
comboBox.Items.Add(new ComboBoxItem("BURMESE", "my"));
comboBox.Items.Add(new ComboBoxItem("CATALAN", "ca"));
comboBox.Items.Add(new ComboBoxItem("CHEROKEE", "chr"));
comboBox.Items.Add(new ComboBoxItem("CHINESE", "zh"));
comboBox.Items.Add(new ComboBoxItem("CHINESE_SIMPLIFIED", "zh-CN"));
comboBox.Items.Add(new ComboBoxItem("CHINESE_TRADITIONAL", "zh-TW"));
comboBox.Items.Add(new ComboBoxItem("CROATIAN", "hr"));
comboBox.Items.Add(new ComboBoxItem("CZECH", "cs"));
comboBox.Items.Add(new ComboBoxItem("DANISH", "da"));
comboBox.Items.Add(new ComboBoxItem("DHIVEHI", "dv"));
comboBox.Items.Add(new ComboBoxItem("DUTCH", "nl"));
comboBox.Items.Add(new ComboBoxItem("ENGLISH", "en"));
comboBox.Items.Add(new ComboBoxItem("ESPERANTO", "eo"));
comboBox.Items.Add(new ComboBoxItem("ESTONIAN", "et"));
comboBox.Items.Add(new ComboBoxItem("FILIPINO", "tl"));
comboBox.Items.Add(new ComboBoxItem("FINNISH", "fi"));
comboBox.Items.Add(new ComboBoxItem("FRENCH", "fr"));
comboBox.Items.Add(new ComboBoxItem("GALICIAN", "gl"));
comboBox.Items.Add(new ComboBoxItem("GEORGIAN", "ka"));
comboBox.Items.Add(new ComboBoxItem("GERMAN", "de"));
comboBox.Items.Add(new ComboBoxItem("GREEK", "el"));
comboBox.Items.Add(new ComboBoxItem("GUARANI", "gn"));
comboBox.Items.Add(new ComboBoxItem("GUJARATI", "gu"));
comboBox.Items.Add(new ComboBoxItem("HEBREW", "iw"));
comboBox.Items.Add(new ComboBoxItem("HINDI", "hi"));
comboBox.Items.Add(new ComboBoxItem("HUNGARIAN", "hu"));
comboBox.Items.Add(new ComboBoxItem("ICELANDIC", "is"));
comboBox.Items.Add(new ComboBoxItem("IRISH", "ga"));
comboBox.Items.Add(new ComboBoxItem("INDONESIAN", "id"));
comboBox.Items.Add(new ComboBoxItem("INUKTITUT", "iu"));
comboBox.Items.Add(new ComboBoxItem("ITALIAN", "it"));
comboBox.Items.Add(new ComboBoxItem("JAPANESE", "ja"));
comboBox.Items.Add(new ComboBoxItem("KANNADA", "kn"));
comboBox.Items.Add(new ComboBoxItem("KAZAKH", "kk"));
comboBox.Items.Add(new ComboBoxItem("KHMER", "km"));
comboBox.Items.Add(new ComboBoxItem("KOREAN", "ko"));
comboBox.Items.Add(new ComboBoxItem("KURDISH", "ku"));
comboBox.Items.Add(new ComboBoxItem("KYRGYZ", "ky"));
comboBox.Items.Add(new ComboBoxItem("LAOTHIAN", "lo"));
comboBox.Items.Add(new ComboBoxItem("LATVIAN", "lv"));
comboBox.Items.Add(new ComboBoxItem("LITHUANIAN", "lt"));
comboBox.Items.Add(new ComboBoxItem("MACEDONIAN", "mk"));
comboBox.Items.Add(new ComboBoxItem("MALAY", "ms"));
comboBox.Items.Add(new ComboBoxItem("MALAYALAM", "ml"));
comboBox.Items.Add(new ComboBoxItem("MALTESE", "mt"));
comboBox.Items.Add(new ComboBoxItem("MARATHI", "mr"));
comboBox.Items.Add(new ComboBoxItem("MONGOLIAN", "mn"));
comboBox.Items.Add(new ComboBoxItem("NEPALI", "ne"));
comboBox.Items.Add(new ComboBoxItem("NORWEGIAN", "no"));
comboBox.Items.Add(new ComboBoxItem("ORIYA", "or"));
comboBox.Items.Add(new ComboBoxItem("PASHTO", "ps"));
comboBox.Items.Add(new ComboBoxItem("PERSIAN", "fa"));
comboBox.Items.Add(new ComboBoxItem("POLISH", "pl"));
comboBox.Items.Add(new ComboBoxItem("PORTUGUESE", "pt-PT"));
comboBox.Items.Add(new ComboBoxItem("PUNJABI", "pa"));
comboBox.Items.Add(new ComboBoxItem("ROMANIAN", "ro"));
comboBox.Items.Add(new ComboBoxItem("RUSSIAN", "ru"));
comboBox.Items.Add(new ComboBoxItem("SANSKRIT", "sa"));
comboBox.Items.Add(new ComboBoxItem("SERBIAN", "sr"));
comboBox.Items.Add(new ComboBoxItem("SINDHI", "sd"));
comboBox.Items.Add(new ComboBoxItem("SINHALESE", "si"));
comboBox.Items.Add(new ComboBoxItem("SLOVAK", "sk"));
comboBox.Items.Add(new ComboBoxItem("SLOVENIAN", "sl"));
comboBox.Items.Add(new ComboBoxItem("SPANISH", "es"));
comboBox.Items.Add(new ComboBoxItem("SWAHILI", "sw"));
comboBox.Items.Add(new ComboBoxItem("SWEDISH", "sv"));
comboBox.Items.Add(new ComboBoxItem("TAJIK", "tg"));
comboBox.Items.Add(new ComboBoxItem("TAMIL", "ta"));
comboBox.Items.Add(new ComboBoxItem("TAGALOG", "tl"));
comboBox.Items.Add(new ComboBoxItem("TELUGU", "te"));
comboBox.Items.Add(new ComboBoxItem("THAI", "th"));
comboBox.Items.Add(new ComboBoxItem("TIBETAN", "bo"));
comboBox.Items.Add(new ComboBoxItem("TURKISH", "tr"));
comboBox.Items.Add(new ComboBoxItem("UKRAINIAN", "uk"));
comboBox.Items.Add(new ComboBoxItem("URDU", "ur"));
comboBox.Items.Add(new ComboBoxItem("UZBEK", "uz"));
comboBox.Items.Add(new ComboBoxItem("UIGHUR", "ug"));
comboBox.Items.Add(new ComboBoxItem("VIETNAMESE", "vi"));
comboBox.Items.Add(new ComboBoxItem("WELSH", "cy"));
comboBox.Items.Add(new ComboBoxItem("YIDDISH", "yi"));
foreach (var pair in new GoogleTranslator1().GetTranslationPairs())
{
comboBox.Items.Add(new ComboBoxItem(pair.Name, pair.Code));
}
}

private void ToolStripMenuItem1Click(object sender, EventArgs e)
Expand Down Expand Up @@ -868,10 +778,19 @@ private void SaveToolStripMenuItemClick(object sender, EventArgs e)
{
FillOriginalDocumentFromSecondLanguage();

var settings = new XmlWriterSettings { Indent = true };
using (var writer = XmlWriter.Create(_secondLanguageFileName, settings))
using (var sw = new StringWriter())
{
_originalDocument.Save(writer);
using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }))
{
_originalDocument.Save(xw);
var s = sw.ToString();
if (s.Contains("Subtitle Edit"))
{
s = s.Replace("<HelpFile></HelpFile>", "<HelpFile />");
}
s = s.Replace("encoding=\"utf-16\"?", "encoding=\"utf-8\"?");
File.WriteAllText(_secondLanguageFileName, s, Encoding.UTF8);
}
}
_change = false;
toolStripStatusLabel1.Text = "File saved - " + _secondLanguageFileName;
Expand Down
4 changes: 2 additions & 2 deletions src/XmlContentTranslator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.6.0.0")]
[assembly: AssemblyVersion("1.7.0.0")]
[assembly: AssemblyFileVersion("1.7.0.0")]
8 changes: 8 additions & 0 deletions src/XmlContentTranslator/Translator/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace XmlContentTranslator.Translator
{
internal static class Configuration
{
public static double CurrentFrameRate = 23.976;
public static string ListViewLineSeparatorString = "<br />";
}
}
128 changes: 128 additions & 0 deletions src/XmlContentTranslator/Translator/Formatting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace XmlContentTranslator.Translator
{
public class Formatting
{
private bool Italic { get; set; }
private bool ItalicTwoLines { get; set; }
private string StartTags { get; set; }
private bool AutoBreak { get; set; }
private bool SquareBrackets { get; set; }
private bool SquareBracketsUppercase { get; set; }

public string SetTagsAndReturnTrimmed(string text, string source)
{
text = text.Trim();

// SSA/ASS tags
if (text.StartsWith("{\\"))
{
var endIndex = text.IndexOf('}');
if (endIndex > 0)
{
StartTags = text.Substring(0, endIndex + 1);
text = text.Remove(0, endIndex + 1).Trim();
}
}

// Italic tags
if (text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && text.Contains("</i>" + Environment.NewLine + "<i>") && Utilities.GetNumberOfLines(text) == 2 && Utilities.CountTagInText(text, "<i>") == 1)
{
ItalicTwoLines = true;
text = HtmlUtil.RemoveOpenCloseTags(text, HtmlUtil.TagItalic);
}
else if (text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && Utilities.CountTagInText(text, "<i>") == 1)
{
Italic = true;
text = text.Substring(3, text.Length - 7);
}

// Un-break line
var allowedLanguages = new List<string> { "en", "da", "nl", "de", "sv", "nb", "fr", "it" };
if (allowedLanguages.Contains(source))
{
var lines = HtmlUtil.RemoveHtmlTags(text).SplitToLines();
if (lines.Length == 2 && !string.IsNullOrEmpty(lines[0]) && !string.IsNullOrEmpty(lines[1]) &&
char.IsLetterOrDigit(lines[0][lines[0].Length - 1]) &&
char.IsLower(lines[1][0]))
{
text = text.Replace(Environment.NewLine, " ").Replace(" ", " ");
AutoBreak = true;
}
}

// Square brackets
if (text.StartsWith("[", StringComparison.Ordinal) && text.EndsWith("]", StringComparison.Ordinal) &&
Utilities.GetNumberOfLines(text) == 1 && Utilities.CountTagInText(text, "[") == 1 &&
Utilities.GetNumberOfLines(text) == 1 && Utilities.CountTagInText(text, "]") == 1)
{
if (text == text.ToUpperInvariant())
SquareBracketsUppercase = true;
else
SquareBrackets = true;
text = text.Replace("[", string.Empty).Replace("]", string.Empty);
}

return text.Trim();
}

public string ReAddFormatting(string text)
{
// Auto-break line
if (AutoBreak)
{
text = Utilities.AutoBreakLine(text);
}

// Square brackets
if (SquareBracketsUppercase)
{
text = "[" + text.ToUpperInvariant().Trim() + "]";
}
else if (SquareBrackets)
{
text = "[" + text.Trim() + "]";
}

// Italic tags
if (ItalicTwoLines)
{
var sb = new StringBuilder();
foreach (var line in text.SplitToLines())
{
sb.AppendLine("<i>" + line + "</i>");
}
text = sb.ToString().Trim();
}
else if (Italic)
{
text = "<i>" + text + "</i>";
}

// SSA/ASS tags
text = StartTags + text;

return text;
}


private int NumberOfLines { get; set; }

public string Unbreak(string text, string source)
{
NumberOfLines = source.SplitToLines().Length;
return text.Replace(Environment.NewLine, " ").Replace(" ", " ");
}

public string Rebreak(string text)
{
if (NumberOfLines == 1)
return text;
return Utilities.AutoBreakLine(text);
}

}
}
Loading

0 comments on commit a754488

Please sign in to comment.