Exemplo n.º 1
0
        private List <ResXDataNode> GenerateResourceFile(
            ResourceFileData resourceFile
            , ResourceFileLanguage language
            , List <ResXDataNode> primaryEntries = null)
        {
            var fileName = $"{resourceFile.FileName}.{language.Encoding}.resx";
            var filePath = Path.Combine(ResourcesDirectory, resourceFile.ResourceFileDirectory, fileName);

            string prefix = language.IsPrimary ? null : language.NonPrimaryResourceValuePrefix;
            string suffix = language.IsPrimary ? null : language.NonPrimaryResourceValueSuffix;

            var resourceEntries = GenerateEntriesFromKeys(
                resourceFile.ResourceKeys,
                prefix,
                suffix);

            if (!language.IsPrimary && primaryEntries != null)
            {
                resourceEntries.RemoveAll(x => primaryEntries.Exists(p => p.Name == x.Name));

#pragma warning disable CS0219 // Variable is assigned but its value is never used
                System.ComponentModel.Design.ITypeResolutionService typeres = null;
#pragma warning restore CS0219 // Variable is assigned but its value is never used
                resourceEntries.AddRange(
                    primaryEntries.Select(x => new ResXDataNode(x.Name, $"{prefix}{x.GetValue(typeres)}{suffix}")
                {
                    Comment = "AUTO"
                })
                    );
            }

            if (File.Exists(filePath))
            {
                return(MergeResourceFile(filePath, resourceEntries, language));
            }
            else
            {
                var directory = new DirectoryInfo(Path.Combine(ResourcesDirectory, resourceFile.ResourceFileDirectory));
                if (!directory.Exists)
                {
                    directory.Create();
                }

                return(CreateResourceFile(filePath, resourceEntries));
            }
        }
Exemplo n.º 2
0
        private List <ResXDataNode> MergeResourceFile(string filePath, List <ResXDataNode> resourceEntries, ResourceFileLanguage language)
        {
#pragma warning disable CS0219 // Variable is assigned but its value is never used
            System.ComponentModel.Design.ITypeResolutionService typeres = null;
#pragma warning restore CS0219 // Variable is assigned but its value is never used

            List <ResXDataNode> entries = new List <ResXDataNode>();
            using (ResXResourceReader resx = new ResXResourceReader(filePath))
            {
                resx.UseResXDataNodes = true;
                foreach (DictionaryEntry entry in resx)
                {
                    ResXDataNode node = (ResXDataNode)entry.Value;
                    entries.Add(node);
                }
            }
            bool hasChanges = false;
            // Add the Resources Entries from the parse that don't already exist.
            foreach (var entry in resourceEntries)
            {
                if (!entries.Exists(x => x.Name == entry.Name))
                {
                    entries.Add(entry);
                    hasChanges = true;
                }
            }

            // Update Entries that are not found from the regex.
            foreach (var entry in entries)
            {
                if (!resourceEntries.Exists(x => x.Name == entry.Name))
                {
                    const string resourceText = "Resource Key not found from Auto Generation";
                    if (entry.Comment != "AUTO_IGNORE" && entry.Comment != resourceText)
                    {
                        entry.Comment = resourceText;
                        hasChanges    = true;
                    }
                }
            }

            if (hasChanges)
            {
                using (ResXResourceWriter resx = new ResXResourceWriter(filePath))
                {
                    foreach (var entry in entries)
                    {
                        resx.AddResource(entry);
                    }
                }
            }

            return(entries);
        }