Beispiel #1
0
        public void UpdateXmlFiles(List <FileInfo> filesByNameInDirectory, List <ModInfoContainer> lstModInfos)
        {
            foreach (FileInfo currentFileInfo in filesByNameInDirectory)
            {
                ModInfoContainer foundModInfo = lstModInfos.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentFileInfo.Directory.Name.ToLowerInvariant());

                if (foundModInfo == null)
                {
                    continue;
                }

                //get file
                XDocument xdoc = XDocument.Load(currentFileInfo.FullName);

                bool changed = false;
                //for each id in excel
                //Values(ID)(LANGUAGE)
                foreach (var currentID in foundModInfo.Values)
                {
                    //for each language
                    foreach (var currentLanguage in currentID.Value)
                    {
                        if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value))
                        {
                            changed = true;
                        }
                    }
                }


                // Now check if someone deleted a row
                IEnumerable <XElement> keysInXml = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
                foreach (XElement currentKeyElement in keysInXml.ToList())
                {
                    string currentKeyId = currentKeyElement.Attribute(ID_NAME).Value;
                    if (foundModInfo.Values.Keys.Contains(currentKeyId))
                    {
                        // hmm, forgot what this was for
                        var a = true;
                    }
                    else
                    {
                        currentKeyElement.Remove();
                        changed = true;
                    }
                }

                if (changed)
                {
                    XComment comment = (from node in xdoc.Nodes() where node.NodeType == XmlNodeType.Comment select node as XComment).FirstOrDefault();

                    string commentText = String.Format(" Edited with tabler. ");

                    if (comment == null)
                    {
                        xdoc.AddFirst(new XComment(commentText));
                    }
                    else
                    {
                        comment.Value = commentText;
                    }

                    var settings = new XmlWriterSettings();
                    settings.Indent      = true;
                    settings.IndentChars = "    "; // Indent 3 Spaces

                    using (XmlWriter writer = XmlWriter.Create(currentFileInfo.FullName, settings))
                    {
                        xdoc.Save(writer);
                    }
                }
            }
        }
Beispiel #2
0
        public List <ModInfoContainer> ParseAllTables()
        {
            var allModInfo = new List <ModInfoContainer>();

            // iterating through the modules
            foreach (TabPage tabPage in _gridUi.tabControl1.TabPages)
            {
                string currentModuleName = tabPage.Text;

                // it has to be there
                var gridView = (DataGridView)tabPage.Controls[0];

                var modInfo = new ModInfoContainer();

                var translationsWithKeys = new Dictionary <string, Dictionary <string, string> >();

                // iterating through the keys
                foreach (DataGridViewRow row in gridView.Rows)
                {
                    string keyName         = string.Empty;
                    var    dicTranslations = new Dictionary <string, string>();

                    // iterating through the languages
                    foreach (DataGridViewTextBoxColumn dgvc in gridView.Columns)
                    {
                        if (dgvc.HeaderText == TranslationManager.COLUMN_IDNAME)
                        {
                            if (row.Cells[dgvc.Index] == null || row.Cells[dgvc.Index].Value == null)
                            {
                                continue;
                            }

                            keyName = row.Cells[dgvc.Index].Value.ToString();
                            continue;
                        }

                        string value = string.Empty;

                        if (row.Cells[dgvc.Index] != null && row.Cells[dgvc.Index].Value != null)
                        {
                            value = row.Cells[dgvc.Index].Value.ToString();
                        }

                        dicTranslations.Add(dgvc.HeaderText, value);
                    }

                    if (string.IsNullOrEmpty(keyName))
                    {
                        continue;
                    }

                    translationsWithKeys.Add(keyName, dicTranslations);
                }

                modInfo.Values = translationsWithKeys;
                modInfo.Name   = currentModuleName;


                allModInfo.Add(modInfo);
            }

            return(allModInfo);
        }
Beispiel #3
0
        public TranslationComponents ParseXmlFiles(List <FileInfo> allStringTablePaths)
        {
            var lstXDocuments = new List <XDocument>();

            var lstHeader = new List <string>();

            var allModInfos = new List <ModInfoContainer>();

            var transComp = new TranslationComponents();

            foreach (FileInfo currentFile in allStringTablePaths)
            {
                var modInfo = new ModInfoContainer();
                modInfo.FileInfoStringTable = currentFile;
                modInfo.Name = currentFile.Directory.Name;


                XDocument xdoc = XDocument.Load(currentFile.FullName);
                lstXDocuments.Add(xdoc);

                IEnumerable <XElement> keys = xdoc.Descendants().Where(x => x.Name == KEY_NAME);

                var dicKeyWithTranslations = new Dictionary <string, Dictionary <string, string> >();

                // all keys
                foreach (XElement key in keys)
                {
                    string currentKeyId = key.Attribute(ID_NAME).Value;

                    var dicTranslations = new Dictionary <string, string>();

                    // all languages of a key
                    foreach (XElement language in key.Descendants())
                    {
                        string languageName = language.Name.ToString();

                        if (dicTranslations.ContainsKey(languageName))
                        {
                            throw new DuplicateKeyException(languageName, currentFile.FullName);
                        }

                        dicTranslations.Add(languageName, language.Value);

                        // save all the languages
                        if (lstHeader.Contains(languageName) == false)
                        {
                            lstHeader.Add(languageName);
                        }
                    }

                    if (dicKeyWithTranslations.ContainsKey(currentKeyId))
                    {
                        throw new DuplicateKeyException(currentKeyId, currentFile.FullName);
                    }
                    dicKeyWithTranslations.Add(currentKeyId, dicTranslations);
                }


                modInfo.Values = dicKeyWithTranslations;
                allModInfos.Add(modInfo);
            }

            transComp.AllModInfo = allModInfos;
            transComp.Headers    = lstHeader;

            return(transComp);
        }
Beispiel #4
0
        private DataGridView CreateGridViewAndFillWithData(TranslationComponents tc, string currentModule)
        {
            var gridView = new DataGridView();

            gridView.Dock = DockStyle.Fill;

            gridView.EditMode = DataGridViewEditMode.EditOnKeystroke;


            gridView.CellValueChanged       += gridView_CellValueChanged;
            gridView.CellBeginEdit          += gridView_CellBeginEdit;
            gridView.KeyUp                  += gridView_KeyUp;
            gridView.KeyDown                += gridView_KeyDown;
            gridView.UserDeletedRow         += gridView_UserDeletedRow;
            gridView.ColumnHeaderMouseClick += gridView_ColumnHeaderMouseClick;

            foreach (string header in tc.Headers)
            {
                var dgvc = new DataGridViewTextBoxColumn();
                dgvc.HeaderText = header;
                dgvc.SortMode   = DataGridViewColumnSortMode.NotSortable;
                dgvc.Resizable  = DataGridViewTriState.True;
                gridView.Columns.Add(dgvc);
            }

            ModInfoContainer modInfoContainer = tc.AllModInfo.FirstOrDefault(mi => mi.Name == currentModule);

            if (modInfoContainer != null)
            {
                foreach (var translationsWithKey in modInfoContainer.Values)
                {
                    var row = new DataGridViewRow();
                    row.CreateCells(gridView);

                    int index = 1;

                    row.Cells[0].Value = translationsWithKey.Key;


                    foreach (string header in tc.Headers)
                    {
                        if (header == TranslationManager.COLUMN_IDNAME || header == TranslationManager.COLUMN_MODNAME)
                        {
                            continue;
                        }

                        if (header == "English")
                        {
                            row.Cells[index].Style.BackColor = Color.FromKnownColor(COLOR_BASELANGUAGE);
                        }

                        if (!translationsWithKey.Value.ContainsKey(header))
                        {
                            row.Cells[index].Style.BackColor = Color.FromKnownColor(COLOR_EMPTYCELL);
                            AddMissingTranslationToStatistics(tc.Statistics, header, currentModule);
                        }
                        else
                        {
                            string trans = translationsWithKey.Value[header];
                            row.Cells[index].Value          = trans;
                            row.Cells[index].Style.WrapMode = DataGridViewTriState.True;
                        }

                        index += 1;
                    }

                    gridView.Rows.Add(row);
                }
            }


            gridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            //gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            return(gridView);
        }
Beispiel #5
0
        public List <ModInfoContainer> LoadModInfos(ExcelWorksheet ws)
        {
            var lstModInfos = new List <ModInfoContainer>();


            Dictionary <string, int> dicHeader = GetHeaderIndexes(ws);

            int modColumn = dicHeader[TranslationManager.COLUMN_MODNAME];
            int idColumn  = dicHeader[TranslationManager.COLUMN_IDNAME];

            ModInfoContainer currentMod = null;


            List <KeyValuePair <string, int> > allHeaderOrdered = dicHeader.OrderBy(x => x.Value).ToList();

            //each row
            for (int currentRow = 2; currentRow < ws.Dimension.End.Row + 1; currentRow++)
            {
                //<language,value>
                Dictionary <string, string> dicLanguagesOfCurrentID = null;


                //each col
                //for each language in xlsx
                foreach (var currentColumn in allHeaderOrdered)
                {
                    bool   isEmpty = ws.Cells[currentRow, currentColumn.Value].IsEmpty();
                    object value   = ws.Cells[currentRow, currentColumn.Value].Value;


                    //if column mod
                    if (currentColumn.Value == modColumn)
                    {
                        //check if new mod starts
                        if (isEmpty == false)
                        {
                            currentMod = new ModInfoContainer();

                            currentMod.Name = value.ToString();

                            lstModInfos.Add(currentMod);
                        }
                        continue;
                    }

                    //if column id
                    if (currentColumn.Value == idColumn)
                    {
                        //ids can never be empty
                        if (isEmpty)
                        {
                            break;
                        }

                        dicLanguagesOfCurrentID = new Dictionary <string, string>();

                        currentMod.Values.Add(value.ToString(), dicLanguagesOfCurrentID);
                        continue;
                    }


                    if (isEmpty)
                    {
                        continue;
                    }


                    dicLanguagesOfCurrentID.Add(currentColumn.Key, value.ToString());
                }
            }
            return(lstModInfos);
        }
Beispiel #6
0
        public void UpdateXmlFiles(List <FileInfo> filesByNameInDirectory, List <ModInfoContainer> lstModInfos)
        {
            Parallel.ForEach(filesByNameInDirectory, (currentFileInfo) => {
                ModInfoContainer foundModInfo = lstModInfos.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentFileInfo.Directory.Name.ToLowerInvariant());

                if (foundModInfo == null)
                {
                    return;
                }

                //get file
                var xdoc = XDocument.Load(currentFileInfo.FullName);

                bool changed = false;
                //for each id in excel
                //Values(ID)(LANGUAGE)
                foreach (var currentID in foundModInfo.Values)
                {
                    //for each language
                    foreach (var currentLanguage in currentID.Value)
                    {
                        if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value))
                        {
                            changed = true;
                        }
                    }
                }


                // Now check if someone deleted a row
                IEnumerable <XElement> keysInXml = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
                foreach (XElement currentKeyElement in keysInXml.ToList())
                {
                    string currentKeyId = currentKeyElement.Attribute(ID_NAME).Value;
                    if (foundModInfo.Values.Keys.Contains(currentKeyId))
                    {
                        // hmm, forgot what this was for
                        var a = true;
                    }
                    else
                    {
                        currentKeyElement.Remove();
                        changed = true;
                    }
                }

                if (changed)
                {
                    var xmlSettings = new XmlWriterSettings {
                        Indent      = true,
                        IndentChars = "    "
                    };

                    var configHelper = new ConfigHelper();
                    var settings     = configHelper.GetSettings();

                    if (settings != null)
                    {
                        if (settings.IndentationSettings == IndentationSettings.Spaces)
                        {
                            var indentChars = "";

                            for (int i = 0; i < settings.TabSize; i++)
                            {
                                indentChars += " ";
                            }

                            xmlSettings.IndentChars = indentChars;
                        }
                        if (settings.IndentationSettings == IndentationSettings.Tabs)
                        {
                            xmlSettings.IndentChars = "\t";
                        }


                        if (settings.RemoveEmptyNodes)
                        {
                            xdoc.Descendants().Where(d => d.IsEmpty || String.IsNullOrWhiteSpace(d.Value)).Remove();
                        }
                    }


                    using (var writer = XmlWriter.Create(currentFileInfo.FullName, xmlSettings)) {
                        xdoc.Save(writer);
                    }
                    File.AppendAllText(currentFileInfo.FullName, Environment.NewLine);
                }
            });
        }
Beispiel #7
0
        public TranslationComponents ParseXmlFiles(List <FileInfo> allStringTablePaths)
        {
            var lstHeader = new System.Collections.Concurrent.ConcurrentBag <string>();

            var allModInfos = new System.Collections.Concurrent.ConcurrentBag <ModInfoContainer>();

            var transComp = new TranslationComponents();

            Parallel.ForEach(allStringTablePaths, (currentFile) => {
                var modInfo = new ModInfoContainer {
                    FileInfoStringTable = currentFile,
                    Name = currentFile.Directory.Name
                };

                XDocument xdoc;

                try {
                    xdoc = XDocument.Load(currentFile.FullName);
                } catch (XmlException xmlException) {
                    throw new GenericXmlException("", currentFile.FullName, xmlException.Message);
                }

                IEnumerable <XElement> keys = xdoc.Descendants().Where(x => x.Name == KEY_NAME);

                var dicKeyWithTranslations = new Dictionary <string, Dictionary <string, string> >();

                // all keys
                foreach (XElement key in keys)
                {
                    string currentKeyId = key.Attribute(ID_NAME).Value;

                    var dicTranslations = new Dictionary <string, string>();

                    // all languages of a key
                    foreach (XElement language in key.Descendants())
                    {
                        string languageName = language.Name.ToString();

                        if (dicTranslations.ContainsKey(languageName))
                        {
                            throw new DuplicateKeyException(languageName, currentFile.FullName, currentKeyId);
                        }

                        dicTranslations.Add(languageName, language.Value);

                        // save all the languages
                        if (lstHeader.Contains(languageName) == false)
                        {
                            lstHeader.Add(languageName);
                        }
                    }

                    if (dicKeyWithTranslations.ContainsKey(currentKeyId))
                    {
                        throw new DuplicateKeyException(currentKeyId, currentFile.FullName, currentKeyId);
                    }
                    dicKeyWithTranslations.Add(currentKeyId, dicTranslations);
                }


                modInfo.Values = dicKeyWithTranslations;
                allModInfos.Add(modInfo);
            });

            transComp.AllModInfo = allModInfos.OrderBy(mic => mic.Name).ToList();
            transComp.Headers    = lstHeader.OrderBy(h => h).ToList();

            return(transComp);
        }
Beispiel #8
0
        public void UpdateXmlFiles(List <FileInfo> filesByNameInDirectory, List <ModInfoContainer> lstModInfos)
        {
            foreach (FileInfo currentFileInfo in filesByNameInDirectory)
            {
                ModInfoContainer foundModInfo = lstModInfos.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentFileInfo.Directory.Name.ToLowerInvariant());

                if (foundModInfo == null)
                {
                    continue;
                }

                //get file
                XDocument xdoc = XDocument.Load(currentFileInfo.FullName);

                bool changed = false;
                //for each id in excel
                //Values(ID)(LANGUAGE)
                foreach (var currentID in foundModInfo.Values)
                {
                    //for each language
                    foreach (var currentLanguage in currentID.Value)
                    {
                        if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value))
                        {
                            changed = true;
                        }
                    }
                }


                // Now check if someone deleted a row
                IEnumerable <XElement> keysInXml = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
                foreach (XElement currentKeyElement in keysInXml.ToList())
                {
                    string currentKeyId = currentKeyElement.Attribute(ID_NAME).Value;
                    if (foundModInfo.Values.Keys.Contains(currentKeyId))
                    {
                        // hmm, forgot what this was for
                        var a = true;
                    }
                    else
                    {
                        currentKeyElement.Remove();
                        changed = true;
                    }
                }

                if (changed)
                {
                    // remove comment due to public request

                    //XComment comment = (from node in xdoc.Nodes() where node.NodeType == XmlNodeType.Comment select node as XComment).FirstOrDefault();

                    //string commentText = String.Format(" Edited with tabler. ");

                    //if (comment == null) {
                    //    xdoc.AddFirst(new XComment(commentText));
                    //} else {
                    //    comment.Value = commentText;
                    //}

                    var xmlSettings = new XmlWriterSettings {
                        Indent      = true,
                        IndentChars = "    "
                    };

                    var configHelper = new ConfigHelper();
                    var settings     = configHelper.GetSettings();

                    if (settings != null)
                    {
                        if (settings.IndentationSettings == IndentationSettings.Spaces)
                        {
                            var indentChars = "";

                            for (int i = 0; i < settings.TabSize; i++)
                            {
                                indentChars += " ";
                            }

                            xmlSettings.IndentChars = indentChars;
                        }
                        if (settings.IndentationSettings == IndentationSettings.Tabs)
                        {
                            xmlSettings.IndentChars = "\t";
                        }


                        if (settings.RemoveEmptyNodes)
                        {
                            xdoc.Descendants().Where(d => d.IsEmpty || String.IsNullOrWhiteSpace(d.Value)).Remove();
                        }
                    }


                    using (var writer = XmlWriter.Create(currentFileInfo.FullName, xmlSettings)) {
                        xdoc.Save(writer);
                    }
                }
            }
        }