Ejemplo n.º 1
0
        private void ExportResources()
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Title      = "Export resources as...";
            sfd.DefaultExt = "xlsx";
            sfd.Filter     = "Excel Worksheet|*.xlsx|CSV file|*.csv";
            bool?result = sfd.ShowDialog();

            if (result == true)
            {
                FileInfo fi    = new FileInfo(sfd.FileName);
                string   error = null;
                if (fi.Extension.ToLower() == ".xlsx")
                {
                    error = LocalizationItem.ExportAsXlsx(sfd.FileName, this.CurrentModel.LocalizationList);
                }
                else if (fi.Extension.ToLower() == ".csv")
                {
                    error = LocalizationItem.ExportAsCsv(sfd.FileName, this.CurrentModel.LocalizationList);
                }

                if (error != null)
                {
                    this.CurrentModel.Status = error;
                    MessageBox.Show("The resources could not be exported:\n" + error, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    this.CurrentModel.Status = "The resources were exported successfully";
                    MessageBox.Show("The resources were exported successfully", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }
Ejemplo n.º 2
0
        private void ImportResources()
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title       = "Import resources from...";
            ofd.DefaultExt  = "xlsx";
            ofd.Multiselect = false;
            ofd.Filter      = "Excel Worksheet|*.xlsx|CSV file|*.csv";
            bool?result = ofd.ShowDialog();

            if (result == true)
            {
                FileInfo fi    = new FileInfo(ofd.FileName);
                string   error = null;
                IList <LocalizationItem> list = null;
                if (fi.Extension.ToLower() == ".xlsx")
                {
                    list = LocalizationItem.ImportFromXlsx(ofd.FileName, out error);
                }
                else if (fi.Extension.ToLower() == ".csv")
                {
                    list = LocalizationItem.ImportFromCsv(ofd.FileName, out error);
                }

                if (error != null)
                {
                    CurrentModel.ClearLocalizationList();
                    this.CurrentModel.Status = error;
                    CurrentModel.Loaded      = false;
                    MessageBox.Show("The resources could not be imported:\n" + error, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    CurrentModel.SetLocalizationList(list);
                    CurrentModel.Loaded      = true;
                    this.CurrentModel.Status = "The resources were imported successfully";
                    MessageBox.Show("The resources were imported successfully", "Import", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }
        public bool Save()
        {
            //FileInfo fi = new FileInfo(this.InputPath);
            LocBamlOptions options = new LocBamlOptions();

            options.CultureInfo          = this.CurrentCultureInfo;
            options.Input                = this.InputPath;
            options.Output               = this.OutputPath;
            options.ToGenerate           = true;
            options.TranslationFileType  = FileType.CSV;
            options.ToGenerateWithStream = true; // Get input by stream
            MemoryStream ms     = new MemoryStream();
            string       errors = LocalizationItem.ExportAsStream(CurrentViewModel.LocalizationList, false, ms, false);

            if (string.IsNullOrEmpty(errors) == false)
            {
                CurrentViewModel.Status = errors;
                MessageBox.Show(errors, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return(false);
            }
            errors = options.CheckAndSetDefault();
            if (string.IsNullOrEmpty(errors) == false)
            {
                CurrentViewModel.Status = errors;
                MessageBox.Show(errors, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return(false);
            }
            errors = LocBaml.GenerateBamlResourcesFromStream(options, ms);
            if (string.IsNullOrEmpty(errors) == false)
            {
                CurrentViewModel.Status = errors;
                MessageBox.Show(errors, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return(false);
            }
            return(true);
        }
        private void SetTable(MemoryStream input, char cellDelimeiter)
        {
            //this.CurrentViewModel.LocalizationList.Clear();
            List <LocalizationItem> items = new List <LocalizationItem>();
            LocalizationItem        item;
            string text = "";

            using (StreamReader sr = new StreamReader(input))
            {
                text = sr.ReadToEnd();
            }
            input.Close();
            string[] cells;
            string[] lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            char[]   delimiters = new char[] { cellDelimeiter };
            bool     bValue, check;

            foreach (string line in lines)
            {
                item  = new LocalizationItem();
                cells = line.Split(delimiters, StringSplitOptions.None);
                for (int i = 0; i < cells.Length; i++)
                {
                    switch (i)
                    {
                    case 0:
                        item.StreamName = cells[i];
                        break;

                    case 1:
                        item.ResourceKey = cells[i];
                        break;

                    case 2:
                        item.ResourceCategory = cells[i];
                        break;

                    case 3:
                        if (bool.TryParse(cells[i], out bValue) == true)
                        {
                            item.IsReadable = bValue;
                        }
                        else
                        {
                            item.IsReadable = false;
                        }
                        break;

                    case 4:
                        if (bool.TryParse(cells[i], out bValue) == true)
                        {
                            item.IsModifieable = bValue;
                        }
                        else
                        {
                            item.IsModifieable = false;
                        }
                        break;

                    case 5:
                        item.Comment = cells[i];
                        break;

                    case 6:
                        item.Content = cells[i];
                        break;
                    }
                }
                //this.CurrentViewModel.LocalizationList.Add(item);
                items.Add(item);
            }
            //this.CurrentViewModel.UpdateList();
            this.CurrentViewModel.SetLocalizationList(items);
            this.CurrentViewModel.Status = "Resource was loaded";
        }
Ejemplo n.º 5
0
        public static IList <LocalizationItem> ImportFromXlsx(string fileName, out string errors)
        {
            int minRowCount;

            if (Properties.Settings.Default.ImportContainsHeader == true)
            {
                minRowCount = 2;
            }
            else
            {
                minRowCount = 1;
            }
            errors = null;
            FemtoXLSX.XlsxReader reader = new XlsxReader(fileName);
            try
            {
                reader.Read();
            }
            catch (Exception e)
            {
                errors = e.Message;
                return(null);
            }

            WorksheetReader worksheet = reader.GetWorksheet(0);

            if (worksheet == null)
            {
                errors = "The worksheet could not be loaded";
                return(null);
            }
            if (worksheet.HasColumn("G") == false)
            {
                errors = "Wrong number of columns. The worksheet must have at least 7 columns.";
                return(null);
            }

            int rowCount = worksheet.GetRowCount();

            if (rowCount < minRowCount)
            {
                if (minRowCount == 1)
                {
                    errors = "No data found. The worksheet must have at least 1 data row (no header expected).";
                }
                else
                {
                    errors = "No data found. The worksheet must have at least 2 rows (header + 1 data row).";
                }
                return(null);
            }
            Dictionary <string, Cell> data  = worksheet.Data;
            List <LocalizationItem>   items = new List <LocalizationItem>();
            LocalizationItem          item;
            List <Cell> row;

            int[] mandatory = new int[] { 0, 1, 2, 3, 4, 6 };
            for (int i = minRowCount - 1; i < rowCount; i++) // Start with row 2( index 1) or 1 (index 0)
            {
                row = worksheet.GetRow(i);
                if (worksheet.RowHasColumns(ref row, mandatory) == false)
                {
                    errors = "Row " + (i + 1).ToString() + " has too few values. At least 6 columns are expected.";
                    return(null);
                }
                item = new LocalizationItem();
                foreach (Cell cell in row)
                {
                    if (cell.ColumnNumber == 0)
                    {
                        item.StreamName = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 1)
                    {
                        item.ResourceKey = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 2)
                    {
                        item.ResourceCategory = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 3 && cell.Type == Cell.DataType.Boolean)
                    {
                        item.IsReadable = cell.GetBoolValue();
                    }
                    else if (cell.ColumnNumber == 4 && cell.Type == Cell.DataType.Boolean)
                    {
                        item.IsModifieable = cell.GetBoolValue();
                    }
                    else if (cell.ColumnNumber == 5)
                    {
                        item.Comment = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber == 6)
                    {
                        item.Content = cell.GetStringValue();
                    }
                    else if (cell.ColumnNumber < 7) // Error fall-back; higher numbers are ignored
                    {
                        errors = "Unexpected data occurred. Check the columns A to G in Row " + (i + 1).ToString();
                        return(null);
                    }
                }
                items.Add(item);
            }
            return(items);
        }
Ejemplo n.º 6
0
        public static IList <LocalizationItem> ImportFromCsv(string fileName, out string errors)
        {
            CsvOptions options = new CsvOptions();

            options.Separator = Properties.Settings.Default.ImportCsvDelimiter[0];
            if (Properties.Settings.Default.ImportContainsHeader == true)
            {
                options.HeaderMode = HeaderMode.HeaderPresent;
            }
            else
            {
                options.HeaderMode = HeaderMode.HeaderAbsent;
            }
            errors = null;
            try
            {
                string rawText = "";
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    TextReader tr = new StreamReader(fs);
                    rawText = tr.ReadToEnd();
                    tr.Close();
                }
                IEnumerable <ICsvLine>  lines = CsvReader.ReadFromText(rawText, options);
                List <LocalizationItem> items = new List <LocalizationItem>();
                LocalizationItem        item;
                int    i = 1;
                string temp;
                bool   state, boolValue;
                foreach (var line in lines)
                {
                    if (line.ColumnCount < 7)
                    {
                        errors = "Row " + i.ToString() + " has too few values. 7 columns are expected.";
                        return(null);
                    }
                    item = new LocalizationItem();
                    for (int j = 0; j < line.ColumnCount; j++)
                    {
                        if (j == 0)
                        {
                            item.StreamName = line[j];
                        }
                        else if (j == 1)
                        {
                            item.resourceKey = line[j];
                        }
                        else if (j == 2)
                        {
                            item.ResourceCategory = line[j];
                        }
                        else if (j == 3)
                        {
                            temp  = line[j];
                            state = FemtoXLSX.WorksheetReader.GetBooleanValue(temp, out boolValue);
                            if (state == false)
                            {
                                errors = "The boolean value of column " + (j + 1).ToString() + " in row " + i.ToString() + "could not be resolved";
                                return(null);
                            }
                            else
                            {
                                item.IsReadable = boolValue;
                            }
                        }
                        else if (j == 4)
                        {
                            temp  = line[j];
                            state = FemtoXLSX.WorksheetReader.GetBooleanValue(temp, out boolValue);
                            if (state == false)
                            {
                                errors = "The boolean value of column " + (j + 1).ToString() + " in row " + i.ToString() + "could not be resolved";
                                return(null);
                            }
                            else
                            {
                                item.IsModifieable = boolValue;
                            }
                        }
                        else if (j == 5)
                        {
                            item.comment = line[j];
                        }
                        else if (j == 6)
                        {
                            item.content = line[j];
                        }
                    }
                    items.Add(item);
                    i++;
                }
                return(items);
            }
            catch (Exception e)
            {
                errors = e.Message;
                return(null);
            }
        }