-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from cnbluefire/main
Add support for pri files
- Loading branch information
Showing
7 changed files
with
210 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using FluentAssertions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace WinUI3Localizer.Tests; | ||
|
||
public class PriResourceReaderTest | ||
{ | ||
[Fact] | ||
public void GetItems_ReturnsAllItems() | ||
{ | ||
LanguageDictionary.Item[]? resourcesItems = null; | ||
LanguageDictionary.Item[]? errorMessagesItems = null; | ||
|
||
Thread? thread = new Thread(() => | ||
{ | ||
Microsoft.Windows.ApplicationModel.DynamicDependency.Bootstrap.Initialize(0x00010005); | ||
|
||
string? priFile = Path.Combine(AppContext.BaseDirectory, "resources.pri"); | ||
PriResourceReaderFactory? factory = new(); | ||
|
||
PriResourceReader? reader = factory.GetPriResourceReader(priFile); | ||
|
||
resourcesItems = reader.GetItems("en-US").ToArray(); | ||
errorMessagesItems = reader.GetItems("es-ES", "ErrorMessages").ToArray(); | ||
|
||
Microsoft.Windows.ApplicationModel.DynamicDependency.Bootstrap.Shutdown(); | ||
}); | ||
|
||
thread.SetApartmentState(ApartmentState.STA); | ||
thread.Start(); | ||
thread.Join(); | ||
|
||
LanguageDictionary.Item item1 = new LanguageDictionary.Item("ControlsPage_Button", "ContentProperty", "Click", "ControlsPage_Button.Content"); | ||
LanguageDictionary.Item item2 = new LanguageDictionary.Item("StylesPage_Top", "TextProperty", "Top", "StylesPage_Top.Text"); | ||
LanguageDictionary.Item item3 = new LanguageDictionary.Item("/ErrorMessages/ErrorMessageExample", "TextProperty", "Ejemplo de mensajes de error", "/ErrorMessages/ErrorMessageExample.Text"); | ||
|
||
resourcesItems.Should().HaveCount(55); | ||
resourcesItems.Should().Contain(item1); | ||
resourcesItems.Should().Contain(item2); | ||
resourcesItems.Should().NotContain(item3); | ||
|
||
errorMessagesItems.Should().HaveCount(1); | ||
errorMessagesItems.Should().NotContain(item1); | ||
errorMessagesItems.Should().NotContain(item2); | ||
errorMessagesItems.Should().Contain(item3); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using Microsoft.Windows.ApplicationModel.Resources; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace WinUI3Localizer; | ||
internal class PriResourceReader | ||
{ | ||
private readonly ResourceManager resourceManager; | ||
|
||
internal PriResourceReader(ResourceManager resourceManager) | ||
{ | ||
this.resourceManager = resourceManager; | ||
} | ||
|
||
public IEnumerable<LanguageDictionary.Item> GetItems(string language, string subTreeName = "Resources") | ||
{ | ||
if (string.IsNullOrEmpty(subTreeName) || subTreeName == "/") | ||
{ | ||
subTreeName = "Resources"; | ||
} | ||
else if (subTreeName.EndsWith('/')) | ||
{ | ||
subTreeName = subTreeName[..^1]; | ||
} | ||
|
||
ResourceMap resourceMap = this.resourceManager.MainResourceMap.TryGetSubtree(subTreeName); | ||
if (resourceMap != null) | ||
{ | ||
ResourceContext resourceContext = this.resourceManager.CreateResourceContext(); | ||
resourceContext.QualifierValues[KnownResourceQualifierName.Language] = language; | ||
|
||
return GetItemsCore(resourceMap, subTreeName, resourceContext); | ||
} | ||
|
||
return Enumerable.Empty<LanguageDictionary.Item>(); | ||
} | ||
|
||
|
||
private IEnumerable<LanguageDictionary.Item> GetItemsCore(ResourceMap resourceMap, string subTreeName, ResourceContext resourceContext) | ||
{ | ||
bool isResourcesSubTree = string.Equals(subTreeName, "Resources", StringComparison.OrdinalIgnoreCase); | ||
uint count = resourceMap.ResourceCount; | ||
|
||
for (uint i = 0; i < count; i++) | ||
{ | ||
(string key, ResourceCandidate? candidate) = resourceMap.GetValueByIndex(i, resourceContext); | ||
|
||
if (candidate != null && candidate.Kind == ResourceCandidateKind.String) | ||
{ | ||
key = key.Replace('/', '.'); | ||
if (!isResourcesSubTree) | ||
{ | ||
key = $"/{subTreeName}/{key}"; | ||
} | ||
yield return LocalizerBuilder.CreateLanguageDictionaryItem(key, candidate.ValueAsString); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
internal class PriResourceReaderFactory | ||
{ | ||
private readonly Dictionary<string, PriResourceReader> readers = new Dictionary<string, PriResourceReader>(); | ||
|
||
internal PriResourceReader GetPriResourceReader(string? priFile) | ||
{ | ||
string? normalizedFilePath = string.Empty; | ||
|
||
if (!string.IsNullOrEmpty(priFile)) | ||
{ | ||
normalizedFilePath = System.IO.Path.GetFullPath(priFile); | ||
} | ||
|
||
if (!this.readers.TryGetValue(normalizedFilePath, out PriResourceReader? reader)) | ||
{ | ||
ResourceManager manager; | ||
if (string.IsNullOrEmpty(normalizedFilePath)) | ||
{ | ||
manager = new ResourceManager(); | ||
} | ||
else | ||
{ | ||
manager = new ResourceManager(normalizedFilePath); | ||
} | ||
reader = new PriResourceReader(manager); | ||
this.readers[normalizedFilePath] = reader; | ||
} | ||
|
||
return reader; | ||
} | ||
} | ||
|