コード例 #1
0
        private void DataSetToXml(ResxData rd, string fileName)
        {
            Console.WriteLine(Messages.CreatingXml);
            using (XmlWriter xw = XmlWriter.Create(fileName, new XmlWriterSettings()
            {
                Encoding = new UTF8Encoding(false), Indent = true
            }
                                                   ))
            {
                int row = DATA_ROWS_OFFSET;

                xw.WriteStartElement("Translations");

                xw.WriteStartElement("Worksheet");
                xw.WriteAttributeString("Name", "Localize");

                DataView dw = rd.Resx.DefaultView;
                dw.Sort = "FileSource, Key";
                foreach (DataRowView drw in dw)
                {
                    ResxData.ResxRow r = (ResxData.ResxRow)drw.Row;
                    xw.WriteStartElement("Message");

                    xw.WriteElementString("ResourceFile", r.FileSource);
                    xw.WriteElementString("ResourceKey", r.Key);
                    xw.WriteElementString("Text", r.Value);
                    xw.WriteElementString("Comment", r.Comment);

                    xw.WriteStartElement("Translation");
                    foreach (ResxData.ResxLocalizedRow lr in r.GetResxLocalizedRows())
                    {
                        if (string.IsNullOrEmpty(lr.Value))
                        {
                            continue;
                        }

                        xw.WriteStartElement("Text");
                        xw.WriteAttributeString("Culture", lr.Culture);
                        xw.WriteValue(lr.Value);
                        xw.WriteEndElement();
                    }
                    xw.WriteEndElement();

                    xw.WriteEndElement();
                }

                xw.WriteEndElement();
                xw.WriteEndElement();
            }
        }
コード例 #2
0
        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();
        }
コード例 #3
0
        private void DataSetToXls(ResxData rd, string fileName)
        {
            Console.WriteLine(Messages.CreatingSpreadsheet);
            using (ExcelPackage app = new ExcelPackage(new FileInfo(fileName)))
            {
                app.Workbook.Worksheets.Add("WorkSheet1");

                ExcelWorksheets sheets = app.Workbook.Worksheets;
                ExcelWorksheet  sheet  = sheets[1];
                sheet.Name = "Localize";

                // <Define Basic Columns>
                sheet.Cells[TITLE_ROW, COL1_IDX__RESX_SRC].Value  = "Resx source";
                sheet.Cells[TITLE_ROW, COL2_IDX__RESX_DEST].Value = "Resx Name";
                sheet.Cells[TITLE_ROW, COL3_IDX__KEY].Value       = "Key";
                sheet.Cells[TITLE_ROW, COL4_IDX__VALUE].Value     = "Value";
                sheet.Cells[TITLE_ROW, COL5_IDX__COMMENT].Value   = "Comment";
                // </Define Basic Columns>

                string[] cultures = GetCulturesFromDataSet(rd);

                if (cultures == null)
                {
                    return;
                }

                int index = DATA_COLS_OFFSET;
                foreach (string cult in cultures)
                {
                    CultureInfo ci = new CultureInfo(cult);
                    sheet.Cells[TITLE_ROW, index].Value   = ci.DisplayName;
                    sheet.Cells[CULTURE_ROW, index].Value = ci.Name;
                    index++;
                }

                DataView dw = rd.Resx.DefaultView;
                dw.Sort = "FileSource, Key";

                int row = DATA_ROWS_OFFSET;
                foreach (DataRowView drw in dw)
                {
                    ResxData.ResxRow r = (ResxData.ResxRow)drw.Row;

                    if (r.Value.StartsWith("="))
                    {
                        r.Value = "!" + r.Value;
                    }

                    ResxData.ResxLocalizedRow[] rows = r.GetResxLocalizedRows();

#if EMPTYRES
                    bool hasAlreadyTranslate = false;
                    bool emptyResource       = false;
#endif

                    foreach (ResxData.ResxLocalizedRow lr in rows)
                    {
                        string culture = lr.Culture;

                        int col = Array.IndexOf(cultures, culture);
                        if (lr.Value.StartsWith("="))
                        {
                            lr.Value = "!" + lr.Value;
                        }

                        if (col >= 0 && r.Value.Length > 0 && lr.Value.Length > 0)
                        {
#if EMPTYRES
                            hasAlreadyTranslate = true;
#endif
                            sheet.Cells[row, col + DATA_COLS_OFFSET].Value = lr.Value;
                        }
                        else if (col >= 0 && r.Value.Length == 0)
                        {
                            //Nothing to translate
#if EMPTYRES
                            hasAlreadyTranslate = true;
                            emptyResource       = true;
#endif
                        }
                        else if (col >= 0)
                        {
                            sheet.Cells[row, col + DATA_COLS_OFFSET].Value = lr.Value;
                        }
                    }

#if EMPTYRES
                    if (!emptyResource)
                    {
#endif
                    sheet.Cells[row, COL1_IDX__RESX_SRC].Value  = r.FileSource;
                    sheet.Cells[row, COL2_IDX__RESX_DEST].Value = r.FileDestination;
                    sheet.Cells[row, COL3_IDX__KEY].Value       = r.Key;
                    sheet.Cells[row, COL4_IDX__VALUE].Value     = r.Value;
                    sheet.Cells[row, COL5_IDX__COMMENT].Value   = r.Comment;

                    row++;
#if EMPTYRES
                }
#endif
                }

                sheet.Cells["A1:Z1"].AutoFitColumns();

                // Save the Workbook and quit Excel.
                app.Save();
            }
        }
コード例 #4
0
        private void DataSetToXls(ResxData rd, string fileName)
        {
            Excel.Application app = new Excel.Application();
            Excel.Workbook    wb  = app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

            Excel.Sheets    sheets = wb.Worksheets;
            Excel.Worksheet sheet  = (Excel.Worksheet)sheets.get_Item(1);
            sheet.Name = "Localize";

            sheet.Cells[1, 1] = "Resx source";
            sheet.Cells[1, 2] = "Resx Name";
            sheet.Cells[1, 3] = "Key";
            sheet.Cells[1, 4] = "Value";

            string[] cultures = GetCulturesFromDataSet(rd);

            int index = 5;

            foreach (string cult in cultures)
            {
                CultureInfo ci = new CultureInfo(cult);

                sheet.Cells[1, index] = ci.DisplayName;
                sheet.Cells[2, index] = ci.Name;
                index++;
            }

            DataView dw = rd.Resx.DefaultView;

            dw.Sort = "FileSource, Key";

            int row = 3;

            foreach (DataRowView drw in dw)
            {
                ResxData.ResxRow r = (ResxData.ResxRow)drw.Row;

                sheet.Cells[row, 1] = r.FileSource;
                sheet.Cells[row, 2] = r.FileDestination;
                sheet.Cells[row, 3] = r.Key;
                sheet.Cells[row, 4] = r.Value;

                ResxData.ResxLocalizedRow[] rows = r.GetResxLocalizedRows();

                foreach (ResxData.ResxLocalizedRow lr in rows)
                {
                    string culture = lr.Culture;

                    int col = Array.IndexOf(cultures, culture);

                    if (col >= 0)
                    {
                        sheet.Cells[row, col + 5] = lr.Value;
                    }
                }

                row++;
            }

            sheet.Cells.get_Range("A1", "Z1").EntireColumn.AutoFit();

            // Save the Workbook and quit Excel.
            wb.SaveAs(fileName, m_objOpt, m_objOpt,
                      m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                      m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
            wb.Close(false, m_objOpt, m_objOpt);
            app.Quit();
        }