static public string Process(string language, string txpath, int searchdepth,
                                     string language2,
                                     string options
                                     ) // overall index of items
        {
            BaseUtils.Translator primary = BaseUtils.Translator.Instance;
            primary.LoadTranslation(language, System.Globalization.CultureInfo.CurrentCulture, new string[] { txpath }, searchdepth, Path.GetTempPath(), loadorgenglish: true, loadfile: true);

            if (options == null)
            {
                options = "";
            }

            if (!primary.Translating)
            {
                Console.WriteLine("Primary translation did not load " + language);
                return("");
            }

            BaseUtils.Translator secondary = new BaseUtils.Translator();
            if (language2 != null)
            {
                secondary.LoadTranslation(language2, System.Globalization.CultureInfo.CurrentCulture, new string[] { txpath }, searchdepth, @"c:\code", loadorgenglish: true, loadfile: true);

                if (!secondary.Translating)
                {
                    Console.WriteLine("Secondary translation did not load " + language2);
                    return("");
                }
            }

            string totalret = "";

            string section = "";

            string filetowrite = "";

            List <StreamWriter> filelist  = new List <StreamWriter>();
            StreamWriter        batchfile = null;

            bool hasdotted = false;

            foreach (string id in primary.EnumerateKeys)
            {
                string ret = "";

                string   orgfile = primary.GetOriginalFile(id);
                FileInfo fi      = new FileInfo(orgfile);
                if (filetowrite == null || !filetowrite.Equals(fi.Name))
                {
                    //ret += Environment.NewLine + "WRITETOFILE " + filetowrite + Environment.NewLine;

                    filetowrite = fi.Name;

                    if (secondary.Translating)
                    {
                        string txname = filetowrite.Replace(language, language2);

                        if (txname.Equals(filetowrite))
                        {
                            txname = filetowrite.Replace(language.Left(language.IndexOf('-')), language2.Left(language2.IndexOf('-')));
                        }

                        filelist.Add(new StreamWriter(Path.Combine(".", txname), false, Encoding.UTF8));

                        if (filelist.Count > 1)
                        {
                            filelist[0].WriteLine(Environment.NewLine + "include " + txname);
                        }

                        string txorgfile = orgfile.Replace(language, language2);
                        if (txorgfile.Equals(orgfile))
                        {
                            txorgfile = orgfile.Replace(language.Left(language.IndexOf('-')), language2.Left(language2.IndexOf('-')));
                        }

                        if (batchfile == null)
                        {
                            batchfile = new StreamWriter("copyback.bat");
                        }

                        batchfile.WriteLine("copy " + txname + " " + txorgfile);
                    }
                    else
                    {
                        filelist.Add(new StreamWriter(Path.Combine(".", filetowrite), false, Encoding.UTF8));

                        if (filelist.Count > 1)
                        {
                            filelist[0].WriteLine(Environment.NewLine + "include " + filetowrite);
                        }
                    }
                }

                string idtouse = id;

                StringParser sp    = new StringParser(id);
                string       front = sp.NextWord(".:");
                if (front != section)
                {
                    if (hasdotted || sp.IsChar('.'))
                    {
                        ret += Environment.NewLine + "SECTION " + front + Environment.NewLine + Environment.NewLine;
                    }
                    if (sp.IsChar('.'))
                    {
                        hasdotted = true;
                    }
                    section = front;
                }

                if (hasdotted && sp.IsChar('.'))
                {
                    idtouse = sp.LineLeft;
                }
                ;

                string primaryorgeng = primary.GetOriginalEnglish(id);
                string translation   = primary.GetTranslation(id);          // default is from the first file
                System.Diagnostics.Debug.WriteLine($"Org {id} = `{primaryorgeng}` `{translation}`");
                bool secondarydef = false;

                if (secondary.Translating)
                {
                    if (secondary.IsDefined(id))
                    {
                        secondarydef = true;                                // we have a secondary def of the id
                        translation  = secondary.GetTranslation(id);
                        if (translation != null)
                        {
                            VerifyFormatting(secondary.GetOriginalFile(id), secondary.GetOriginalLine(id), primaryorgeng, translation, id);
                        }
                        secondary.UnDefine(id);                             // remove id so its not reported as missing
                        System.Diagnostics.Debug.WriteLine($"Found {id} removed");
                    }
                    else
                    {
                        translation = null;                                 // meaning not present
                    }
                }

                ret += idtouse + ": " + primaryorgeng.EscapeControlChars().AlwaysQuoteString(); // output is id : orgeng

                if (translation == null ||                                                      // no translation
                    (translation.Equals(primaryorgeng) && !secondarydef) ||                     // or same text and its not secondary def
                    translation.IsEmpty() ||                                                    // or empty
                    (translation[0] == '<' && translation[translation.Length - 1] == '>'))      // or has those <> around it from the very first go at this parlava
                {
                    bool containsnonspacedigits = false;
                    foreach (var c in primaryorgeng)
                    {
                        if (!(char.IsPunctuation(c) || char.IsWhiteSpace(c) || char.IsNumber(c)))
                        {
                            containsnonspacedigits = true;
                            break;
                        }
                    }

                    if (secondary.Translating && containsnonspacedigits && !options.Contains("NS"))
                    {
                        totalret += "Not defined by secondary: " + id + " in " + primary.GetOriginalFile(id) + ":" + primary.GetOriginalLine(id) + Environment.NewLine;
                    }

                    ret += " @";
                }
                else
                {
                    ret += " => " + translation.EscapeControlChars().AlwaysQuoteString();       // place translated string into file again
                }

                ret += Environment.NewLine;


                //  totalret += ret;
                filelist.Last().Write(ret);
            }

            if (secondary.Translating)
            {
                foreach (string id in secondary.EnumerateKeys)
                {
                    totalret += "In secondary but not in primary: " + id + " in " + secondary.GetOriginalFile(id) + ":" + secondary.GetOriginalLine(id) + Environment.NewLine;
                }
            }

            foreach (var f in filelist)
            {
                f.Close();
            }

            if (batchfile != null)
            {
                batchfile.Close();
            }

            return(totalret);
        }
Пример #2
0
        static public string Process(FileInfo[] files, string language, string txpath, int searchdepth, bool showrepeats, bool showerrorsonly)             // overall index of items
        {
            string            locals  = "";
            List <Definition> idsdone = new List <Definition>();

            BaseUtils.Translator trans = BaseUtils.Translator.Instance;

            if (language.HasChars() && txpath.HasChars())
            {
                trans.LoadTranslation(language, System.Globalization.CultureInfo.CurrentCulture, new string[] { txpath }, searchdepth, Path.GetTempPath(), loadorgenglish: true);

                if (trans.Translating)
                {
                    Console.WriteLine("Loaded translation " + language + " to compare against");
                }
                else
                {
                    Console.WriteLine("Translation for " + language + " Not found");
                    return("");
                }
            }
            else
            {
                Console.WriteLine("No translation comparision given");
            }

            if (!trans.Translating)
            {
                locals += "********************* TRANSLATION NOT LOADED" + Environment.NewLine;
            }

            foreach (var fi in files)
            {
                var utc8nobom = new UTF8Encoding(false);                           // give it the default UTF8 no BOM encoding, it will detect BOM or UCS-2 automatically

                using (StreamReader sr = new StreamReader(fi.FullName, utc8nobom)) // read directly from file.. presume UTF8 no bom
                {
                    List <string> classes      = new List <string>();
                    List <string> baseclasses  = new List <string>();
                    List <int>    classeslevel = new List <int>();
                    Dictionary <string, DefInfo> winformdefs = new Dictionary <string, DefInfo>();

                    int bracketlevel = 0;

                    bool donelocaltitle = false;

                    string line, previoustext = "";
                    int    lineno = 0;

                    string dropdown = null;

                    while ((line = sr.ReadLine()) != null)
                    {
                        lineno++;

                        int startpos = previoustext.Length;

                        string combined = previoustext + line;

                        while (true)
                        {
                            int txpos     = combined.IndexOf(".T(", startpos);
                            int txposcond = combined.IndexOf(".TCond(", startpos);

                            if (txpos == -1 && txposcond == -1)     // if nothing, stop
                            {
                                break;
                            }

                            Tuple <string, string> ret = null;
                            string localtext           = "";

                            if (txposcond != -1 && (txpos == -1 || txposcond < txpos))                  // if txposcond set, AND txpos is not there, or its less than it
                            {
                                StringParser s0 = new StringParser(combined, txposcond);
                                if (s0.NextWordComma() != null)     // waste text up to comma
                                {
                                    string english = s0.NextQuotedWordComma();
                                    if (english != null)
                                    {
                                        string identifier = s0.NextWord(" )");

                                        if (identifier != null && identifier.StartsWith("EDTx."))
                                        {
                                            identifier = identifier.Substring(5).Replace("_", ".");
                                            ret        = new Tuple <string, string>(identifier, english);
                                        }
                                    }
                                }

                                startpos = txposcond + 7;
                            }
                            else
                            {
                                ret = ProcessTLine(combined, line, txpos, txpos + 3, false);
                                if (ret == null)
                                {
                                    localtext = fi.FullName + ":" + lineno + ":Miss formed line around " + combined.Mid(txpos, 30) + Environment.NewLine;
                                }

                                startpos = txpos + 3;     // skip over it
                            }

                            if (ret != null)
                            {
                                System.Diagnostics.Debug.WriteLine("{0} Check {1} {2}", lineno, ret.Item1, ret.Item2);
                                string id        = ret.Item1;
                                string engquoted = ret.Item2.AlwaysQuoteString();

                                Definition def = idsdone.Find(x => x.token.Equals(ret.Item1, StringComparison.InvariantCultureIgnoreCase));

                                string res = null;

                                if (def != null)        // if previously did the def..
                                {
                                    if (def.text != ret.Item2)
                                    {
                                        res = "Different Text: " + id + " at " + fi.FullName + ":" + lineno + Environment.NewLine + "   >> " + ret.Item2.AlwaysQuoteString() + " orginal " + def.text.AlwaysQuoteString() + " at " + def.firstdeflocation;
                                    }
                                    else if (showrepeats)       // if showrepeats is off, then no output, since we already done it
                                    {
                                        res = "Repeat: " + id + " at " + fi.FullName + ":" + lineno + " " + engquoted;
                                    }
                                }
                                else
                                {
                                    idsdone.Add(new Definition(id, ret.Item2, fi.FullName + ":" + lineno));

                                    if (trans.IsDefined(id))
                                    {
                                        if (!showerrorsonly)
                                        {
                                            res = id + ": " + engquoted + " @";     // list it
                                        }
                                        string foreign = trans.GetTranslation(id);
                                        string english = trans.GetOriginalEnglish(id);
                                        english = english.EscapeControlChars();

                                        if (english != ret.Item2)
                                        {
                                            res = "Translator Difference: " + id + " at " + fi.FullName + ":" + lineno + Environment.NewLine +
                                                  "     >> English differs from \"" + ret.Item2 + "\" vs \"" + english + "\"";
                                        }
                                    }
                                    else if (trans.Translating)       // if we are checking translation, do it..
                                    {
                                        res = ".T( Missing: " + id + " at " + fi.FullName + ":" + lineno + " => " + engquoted + " @";
                                    }
                                }

                                if (res != null)
                                {
                                    res      += Environment.NewLine;
                                    localtext = res;
                                }
                            }


                            if (localtext.HasChars())
                            {
                                if (!donelocaltitle)
                                {
                                    string text = "///////////////////////////////////////////////////// " + (classes.Count > 0 ? classes[0] : "?") + " in " + fi.Name + Environment.NewLine;
                                    locals        += text;
                                    donelocaltitle = true;
                                }
                                locals += localtext;
                            }
                        }

                        previoustext += line;
                        if (previoustext.Length > 20000)
                        {
                            previoustext = previoustext.Substring(10000);
                        }


                        line = line.Trim();

                        int clspos = line.IndexOf("partial class ");
                        if (clspos == -1)
                        {
                            clspos = line.IndexOf("public class ");
                        }
                        if (clspos == -1)
                        {
                            clspos = line.IndexOf("abstract class ");
                        }
                        if (clspos == -1)
                        {
                            clspos = line.IndexOf("static class ");
                        }

                        if (clspos >= 0)
                        {
                            StringParser sp = new StringParser(line, clspos);
                            sp.NextWord(" ");
                            sp.NextWord(" ");
                            classes.Add(sp.NextWord(":").Trim());
                            baseclasses.Add(sp.IsCharMoveOn(':') ? sp.NextWord(",") : null);
                            classeslevel.Add(bracketlevel);
                            // System.Diagnostics.Debug.WriteLine(lineno + " {" + bracketlevel * 4 + " Push " + classes.Last() + " " + baseclasses.Last());
                        }

                        if (line.StartsWith("{"))
                        {
                            if (line.Length == 1 || !line.Substring(1).Trim().StartsWith("}"))
                            {
                                //  System.Diagnostics.Debug.WriteLine(lineno + " {" + bracketlevel * 4);
                                bracketlevel++;
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine(lineno + " Rejected {" + bracketlevel * 4);
                            }
                        }

                        if (line.StartsWith("}"))
                        {
                            if (line.Length == 1 || line.Substring(1).Trim()[0] == '/' || line.Substring(1).Trim()[0] == ';')
                            {
                                if (classeslevel.Count > 0 && classeslevel.Last() == bracketlevel - 1)
                                {
                                    //   System.Diagnostics.Debug.WriteLine(lineno + " Pop {" + (bracketlevel-1) * 4 + " " + classes.Last());
                                    classes.RemoveAt(classes.Count - 1);
                                    classeslevel.RemoveAt(classeslevel.Count - 1);
                                    baseclasses.RemoveAt(baseclasses.Count - 1);
                                }
                                bracketlevel--;
                                //System.Diagnostics.Debug.WriteLine(lineno + " }" + bracketlevel * 4);
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine(lineno + " Rejected }" + bracketlevel * 4);
                            }
                        }

                        if (line.StartsWith("this.") && fi.Name.Contains("Designer", StringComparison.InvariantCultureIgnoreCase))
                        {
                            StringParser sp          = new StringParser(line, 5);
                            string       controlname = sp.NextWord(".=,()} ");
                            string       propname    = sp.IsCharMoveOn('.') ? sp.NextWord(".=,()} ") : null;
                            string       value       = null; // value used to indicate we have a id

                            System.Diagnostics.Debug.WriteLine(lineno + " This. " + controlname + " . " + propname);

                            if (dropdown != null)
                            {
                                System.Diagnostics.Debug.WriteLine(">> Drop down " + dropdown + " " + sp.LineLeft);

                                DefInfo di = winformdefs.ContainsKey(controlname) ? winformdefs[controlname] : null;

                                if (di != null)
                                {
                                    di.parent = dropdown;
                                }

                                if (sp.Find("});"))
                                {
                                    dropdown = null;
                                }
                            }
                            else if (propname == "DropDownItems")
                            {
                                System.Diagnostics.Debug.WriteLine("Found drop down " + sp.LineLeft);
                                dropdown = controlname;
                            }
                            else if (propname == "SetToolTip" && sp.IsCharMoveOn('('))
                            {
                                if (sp.NextWord(".") == "this" && sp.IsCharMoveOn('.'))
                                {
                                    controlname = sp.NextWord(".=,()} ") + ".ToolTip";
                                    if (sp.IsCharMoveOn(','))
                                    {
                                        value = sp.NextQuotedWord();
                                    }
                                }
                            }
                            else if (propname == "HeaderText" && sp.IsCharMoveOn('='))
                            {
                                value = sp.NextQuotedWord();
                            }
                            else if (controlname != "Text" && propname == null && sp.IsCharMoveOn('=')) // this.control =
                            {
                                if (sp.NextWord() == "new")                                             // this.name = new..
                                {
                                    winformdefs[controlname] = new DefInfo()
                                    {
                                        newtype = sp.NextWord(";")
                                    };
                                    System.Diagnostics.Debug.WriteLine("Def " + controlname + " " + winformdefs[controlname]);
                                }
                            }
                            else if ((controlname == "Text" && propname == null && sp.IsCharMoveOn('=')) || (propname != null && propname == "Text" && sp.IsCharMoveOn('=')))
                            {
                                value = sp.NextQuotedWord();

                                bool ok = true;

                                if (controlname != "Text")
                                {
                                    DefInfo di = winformdefs.ContainsKey(controlname) ? winformdefs[controlname] : null;

                                    if (di != null)
                                    {
                                        string[] excluded = new string[]
                                        {
                                            "ComboBoxCustom", "NumberBoxDouble", "NumberBoxLong", "VScrollBarCustom",         // Controls not for translation..
                                            "StatusStripCustom", "RichTextBoxScroll", "TextBoxBorder", "AutoCompleteTextBox", "DateTimePicker", "NumericUpDownCustom",
                                            "Panel", "DataGridView", "GroupBox", "SplitContainer", "LinkLabel"
                                        };

                                        ok = Array.FindIndex(excluded, (xx) => di.newtype.Contains(xx)) == -1;

                                        if (di.parent != null)
                                        {
                                            DefInfo dip = winformdefs.ContainsKey(di.parent) ? winformdefs[di.parent] : null;       // double deep ..
                                            controlname = (dip != null && dip.parent != null ? (dip.parent + ".") : "") + di.parent + "." + controlname;
                                        }
                                    }
                                }
                                else
                                {
                                    controlname = "";
                                }

                                if (!ok)
                                {
                                    value = null;
                                }
                            }

                            if (value != null && value != "<code>" && value != "")
                            {
                                string classname = (classes.Count > 0) ? classes.Last() : "ERROR NO CLASS!";
                                string id        = classname + (controlname.HasChars() ? "." + controlname : "");
                                string res       = id + ": " + value.AlwaysQuoteString() + " @";

                                if (trans.IsDefined(id))
                                {
                                    string foreign = trans.GetTranslation(id);
                                    string english = trans.GetOriginalEnglish(id);
                                    english = english.EscapeControlChars();

                                    if (english != value)
                                    {
                                        res = "Translator difference: " + id + " // English differs from \"" + value + "\" vs \"" + english + "\"";
                                    }
                                    else if (showerrorsonly)
                                    {
                                        res = null;
                                    }
                                }
                                else if (trans.Translating)       // if we are checking translation, do it..
                                {
                                    res = "Designer Missing: " + res;
                                }

                                if (res != null)
                                {
                                    if (!donelocaltitle)
                                    {
                                        string text = "///////////////////////////////////////////////////// " + (classes.Count > 0 ? classes[0] : "?") + " in " + fi.Name + Environment.NewLine;
                                        locals        += text;
                                        donelocaltitle = true;
                                    }

                                    locals += res + Environment.NewLine;
                                }
                            }
                        }
                    }
                }
            }

            return(locals);
        }