예제 #1
0
        public async Task ExportToDocumentAsync(IDocumentGenerator documentGenerator, string path, IReadOnlyCollection <string> selectedCultures, IReadOnlyCollection <Project> selectedProjects, IStatusProgress progress, CancellationToken cancellationToken)
        {
            progress.Report(StatusRes.GettingProjectsResources);
            SolutionResources solutionResources = await GetSolutionResourcesAsync(selectedCultures, selectedProjects, progress, cancellationToken);

            progress.Report(StatusRes.PreparingResourcesToExport);

            var cultures = selectedCultures.Select(CultureInfo.GetCultureInfo)
                           .ToDictionary(
                cult => cult.Name,
                cult => cult.Name == InvariantCultureId ? InvariantCultureDisplayName : cult.Name.ToUpper()
                );

            var culturesOrder = new List <string>(cultures.Count)
            {
                InvariantCultureId
            };

            culturesOrder.AddRange(cultures.Where(cult => cult.Key != InvariantCultureId).OrderBy(cult => cult.Value).Select(cult => cult.Key));

            var header = new HeaderModel
            {
                Columns = new List <ColumnModel>(1)
                {
                    new ColumnModel {
                        Title = ExcelRes.ResourceKey
                    }
                }
                .Concat(culturesOrder.Select(cultureId => cultures[cultureId]).Select(headerName => new ColumnModel {
                    Title = headerName
                }))
                .Concat(new List <ColumnModel>(1)
                {
                    new ColumnModel {
                        Title = ExcelRes.Comment
                    }
                })
                .ToList()
            };

            IReadOnlyList <ResGroupModel <ResExcelModel> > groups = solutionResources
                                                                    .ProjectResources.Select(proj => new ResGroupModel <ResExcelModel>
            {
                GroupTitle = proj.ProjectName,
                Tables     = proj.Resources.Select(res =>
                {
                    var neutralResources    = res.Value[InvariantCultureId].StringResources;
                    List <string> keysOrder = neutralResources.Keys.OrderBy(key => key).ToList();

                    List <RowModel <ResExcelModel> > rows = keysOrder.Select(
                        resKey => new RowModel <ResExcelModel>
                    {
                        Model = new ResExcelModel(resKey,
                                                  culturesOrder.Select(cultureId => res.Value[cultureId]).Select(resData => resData.StringResources[resKey].Value).ToList(),
                                                  res.Value[InvariantCultureId].StringResources[resKey].Comment)
                    })
                                                            .Where(r => r.Model.ResourceValues.Count != 0)
                                                            .ToList();

                    var tableModel = new ResTableModel <ResExcelModel>
                    {
                        TableTitle = res.Key,
                        Header     = header,
                        Rows       = rows
                    };

                    cancellationToken.ThrowIfCancellationRequested();

                    return(tableModel);
                })
                             .Where(table => table.Rows.Count != 0)
                             .ToList()
            })
                                                                    .Where(res => res.Tables.Count != 0)
                                                                    .ToList();

            await documentGenerator.ExportToDocumentAsync(path, groups, progress, cancellationToken);
        }