private void ReadResxCult(string fileName, string projectRoot, ResxData rd, string[] cultureList, string[] excludeList, bool useFolderNamespacePrefix)
        {
            FileInfo    fi               = new FileInfo(fileName);
            CultureInfo cultureInfo      = GetResxCultureSpecific(fileName);
            string      fileRelativePath = fi.FullName.Remove(0, AddBS(projectRoot).Length);
            string      fileDestination;

            if (useFolderNamespacePrefix)
            {
                fileDestination = GetNamespacePrefix(AddBS(projectRoot), AddBS(fi.DirectoryName)) + fi.Name;
            }
            else
            {
                fileDestination = fi.Name;
            }
            ResXResourceReader reader = new ResXResourceReader(fileName);

            reader.BasePath = fi.DirectoryName;

            try
            {
                #region read
                foreach (DictionaryEntry de in reader)
                {
                    if (de.Value is string)
                    {
                        string key     = (string)de.Key;
                        bool   exclude = false;
                        foreach (string e in excludeList)
                        {
                            if (key.EndsWith(e))
                            {
                                exclude = true;
                                break;
                            }
                        }
                        if (!exclude)
                        {
                            string             strWhere = String.Format("FileSource ='{0}' AND Key='{1}'", fileRelativePath.Replace("." + cultureInfo.Name, ""), de.Key.ToString());
                            ResxData.ResxRow[] rows     = (ResxData.ResxRow[])rd.Resx.Select(strWhere);
                            if ((rows == null) || (rows.Length == 0))
                            {
                                continue;
                            }

                            ResxData.ResxRow row = rows[0];
                            foreach (ResxData.ResxLocalizedRow lr in row.GetResxLocalizedRows())
                            {
                                if (lr.Culture == cultureInfo.Name)
                                {
                                    row.BeginEdit();
                                    string value = de.Value.ToString();
                                    // update row
                                    if (value.Contains("\r") || value.Contains("\n"))
                                    {
                                        value = value.Replace("\r", "\\r").Replace("\n", "\\n");
                                    }
                                    lr.Value = value;
                                    row.EndEdit();
                                }
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format(Messages.ErrorReadingFileDetailException, fileName, ex.Message), "Information");
            }
            reader.Close();
        }
Exemplo n.º 2
0
        private void UpdateXls(string xlsFile, string projectRoot, bool deepSearch, string[] excludeList, bool useFolderNamespacePrefix)
        {
            if (!File.Exists(xlsFile))
            {
                return;
            }

            string path = new FileInfo(xlsFile).DirectoryName;

            string[] files;

            if (deepSearch)
            {
                files = System.IO.Directory.GetFiles(projectRoot, "*.resx", SearchOption.AllDirectories);
            }
            else
            {
                files = System.IO.Directory.GetFiles(projectRoot, "*.resx", SearchOption.TopDirectoryOnly);
            }


            ResxData rd = XlsToDataSet(xlsFile);

            foreach (string f in files)
            {
                FileInfo fi = new FileInfo(f);

                string fileRelativePath = fi.FullName.Remove(0, AddBS(projectRoot).Length);

                string fileDestination;
                if (useFolderNamespacePrefix)
                {
                    fileDestination = GetNamespacePrefix(AddBS(projectRoot), AddBS(fi.DirectoryName)) + fi.Name;
                }
                else
                {
                    fileDestination = fi.Name;
                }

                ResXResourceReader reader = new ResXResourceReader(f);
                reader.BasePath = fi.DirectoryName;

                foreach (DictionaryEntry d in reader)
                {
                    if (d.Value is string)
                    {
                        bool exclude = false;
                        foreach (string e in excludeList)
                        {
                            if (d.Key.ToString().EndsWith(e))
                            {
                                exclude = true;
                                break;
                            }
                        }

                        if (!exclude)
                        {
                            string             strWhere = String.Format("FileSource ='{0}' AND Key='{1}'", fileRelativePath, d.Key.ToString());
                            ResxData.ResxRow[] rows     = (ResxData.ResxRow[])rd.Resx.Select(strWhere);

                            ResxData.ResxRow row = null;
                            if ((rows == null) | (rows.Length == 0))
                            {
                                // add row
                                row = rd.Resx.NewResxRow();

                                row.FileSource      = fileRelativePath;
                                row.FileDestination = fileDestination;
                                // I update the neutral value
                                row.Key = d.Key.ToString();

                                rd.Resx.AddResxRow(row);
                            }
                            else
                            {
                                row = rows[0];
                            }

                            // update row
                            row.BeginEdit();

                            string value = d.Value.ToString();
                            value     = value.Replace("\r", "\\r");
                            value     = value.Replace("\n", "\\n");
                            row.Value = value;

                            row.EndEdit();
                        }
                    }
                }
            }

            //delete unchenged rows
            foreach (ResxData.ResxRow r in rd.Resx.Rows)
            {
                if (r.RowState == DataRowState.Unchanged)
                {
                    r.Delete();
                }
            }
            rd.AcceptChanges();

            DataSetToXls(rd, xlsFile);
        }