|
| 1 | +using Microsoft.Windows.ApplicationModel.Resources; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace WinUI3Localizer; |
| 10 | +internal class PriResourceReader |
| 11 | +{ |
| 12 | + private readonly ResourceManager resourceManager; |
| 13 | + |
| 14 | + internal PriResourceReader(ResourceManager resourceManager) |
| 15 | + { |
| 16 | + this.resourceManager = resourceManager; |
| 17 | + } |
| 18 | + |
| 19 | + public IEnumerable<LanguageDictionary.Item> GetItems(string language, string subTreeName = "Resources") |
| 20 | + { |
| 21 | + if (string.IsNullOrEmpty(subTreeName) || subTreeName == "/") |
| 22 | + { |
| 23 | + subTreeName = "Resources"; |
| 24 | + } |
| 25 | + else if (subTreeName.EndsWith('/')) |
| 26 | + { |
| 27 | + subTreeName = subTreeName[..^1]; |
| 28 | + } |
| 29 | + |
| 30 | + ResourceMap resourceMap = this.resourceManager.MainResourceMap.TryGetSubtree(subTreeName); |
| 31 | + if (resourceMap != null) |
| 32 | + { |
| 33 | + ResourceContext resourceContext = this.resourceManager.CreateResourceContext(); |
| 34 | + resourceContext.QualifierValues[KnownResourceQualifierName.Language] = language; |
| 35 | + |
| 36 | + return GetItemsCore(resourceMap, subTreeName, resourceContext); |
| 37 | + } |
| 38 | + |
| 39 | + return Enumerable.Empty<LanguageDictionary.Item>(); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + private IEnumerable<LanguageDictionary.Item> GetItemsCore(ResourceMap resourceMap, string subTreeName, ResourceContext resourceContext) |
| 44 | + { |
| 45 | + bool isResourcesSubTree = string.Equals(subTreeName, "Resources", StringComparison.OrdinalIgnoreCase); |
| 46 | + uint count = resourceMap.ResourceCount; |
| 47 | + |
| 48 | + for (uint i = 0; i < count; i++) |
| 49 | + { |
| 50 | + (string key, ResourceCandidate? candidate) = resourceMap.GetValueByIndex(i, resourceContext); |
| 51 | + |
| 52 | + if (candidate != null && candidate.Kind == ResourceCandidateKind.String) |
| 53 | + { |
| 54 | + key = key.Replace('/', '.'); |
| 55 | + if (!isResourcesSubTree) |
| 56 | + { |
| 57 | + key = $"/{subTreeName}/{key}"; |
| 58 | + } |
| 59 | + yield return LocalizerBuilder.CreateLanguageDictionaryItem(key, candidate.ValueAsString); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +internal class PriResourceReaderFactory |
| 67 | +{ |
| 68 | + private readonly Dictionary<string, PriResourceReader> readers = new Dictionary<string, PriResourceReader>(); |
| 69 | + |
| 70 | + internal PriResourceReader GetPriResourceReader(string? priFile) |
| 71 | + { |
| 72 | + string? normalizedFilePath = string.Empty; |
| 73 | + |
| 74 | + if (!string.IsNullOrEmpty(priFile)) |
| 75 | + { |
| 76 | + normalizedFilePath = System.IO.Path.GetFullPath(priFile); |
| 77 | + } |
| 78 | + |
| 79 | + if (!this.readers.TryGetValue(normalizedFilePath, out PriResourceReader? reader)) |
| 80 | + { |
| 81 | + ResourceManager manager; |
| 82 | + if (string.IsNullOrEmpty(normalizedFilePath)) |
| 83 | + { |
| 84 | + manager = new ResourceManager(); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + manager = new ResourceManager(normalizedFilePath); |
| 89 | + } |
| 90 | + reader = new PriResourceReader(manager); |
| 91 | + this.readers[normalizedFilePath] = reader; |
| 92 | + } |
| 93 | + |
| 94 | + return reader; |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments