Ejemplo n.º 1
0
        /// <summary>
        /// Export resources of specified type and culture to resx
        /// </summary>
        /// <typeparam name="TResource"></typeparam>
        /// <param name="expression"></param>
        /// <param name="culture"></param>
        /// <param name="overwrite"></param>
        /// <param name="p"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public async Task <int> ExportToResx <TResource>(Expression <Func <TResource, bool> > expression, string culture, bool overwrite, int p, int s)
            where TResource : class, IXDbResource
        {
            ResxWriter manager = new ResxWriter(typeof(TResource), _options.ResourcesPath, culture, _loggerFactory);

            int totalRawData = await GetRawDataCountAsync <TResource>(expression);

            int totalExported = 0;

            for (int i = 0; i < totalRawData; i += s)
            {
                var dataLot = await GetRawDataListAsync <TResource>(p, s, expression);

                totalExported += await manager.AddRangeAsync(dataLot, overwrite);

                if (totalExported > 0)
                {
                    var saved = await manager.SaveAsync();

                    if (saved)
                    {
                        _logger.LogInformation($"Total '{totalExported}' resources exported to '{manager.ResourceFilePath}'");
                    }
                    else
                    {
                        _logger.LogError($"Exported not successful to '{manager.ResourceFilePath}'");
                    }
                }
            }

            return(totalExported);
        }
        /// <summary>
        /// Export xml data to resx
        /// </summary>
        /// <param name="type"></param>
        /// <param name="culture"></param>
        /// <param name="approvedOnly"></param>
        /// <param name="overwriteExistingKeys"></param>
        /// <returns></returns>
        public async Task <int> ExportToResxAsync(Type type, string culture, bool approvedOnly, bool overwriteExistingKeys)
        {
            string resFileName  = ResourceTypeHelper.CreateResourceName(type, _options.ResourcesPath);
            var    filePath     = Path.Combine(_options.ResourcesPath, $"{resFileName}.{culture}");
            var    xmlFilePath  = $"{filePath}.xml";
            var    resxFilePath = $"{filePath}.resx";

            var _xd = XDocument.Load(xmlFilePath);

            var elements = approvedOnly
                ? _xd.Root.Descendants("data")
                           .Where(x => x.Attribute("isActive").Value == "true")
                           .Select(x => new ResxElement
            {
                Key     = x.Element("key")?.Value,
                Value   = x.Element("value")?.Value,
                Comment = x.Element("comment")?.Value
            })
                : _xd.Root.Descendants("data")
                           .Select(x => new ResxElement
            {
                Key     = x.Element("key")?.Value,
                Value   = x.Element("value")?.Value,
                Comment = x.Element("comment")?.Value
            });

            var resxWriter    = new ResxWriter(resxFilePath, _loggerFactory);
            var totalExported = await resxWriter.AddRangeAsync(elements.Where(x => x.Key != null && x.Value != null), overwriteExistingKeys);

            if (totalExported > 0)
            {
                var saved = await resxWriter.SaveAsync();

                if (saved)
                {
                    _logger.LogInformation($"Total '{totalExported}' resources exported to '{resxFilePath}'");
                }
                else
                {
                    _logger.LogError($"Exported not successful to '{resxFilePath}'");
                }
            }

            return(totalExported);
        }