Skip to content

Commit

Permalink
Merge pull request #42 from AndrewKeepCoding/v2.2.0
Browse files Browse the repository at this point in the history
V2.2.0
  • Loading branch information
AndrewKeepCoding authored Apr 12, 2024
2 parents fefc9a0 + dfbda6c commit 8068353
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
1 change: 0 additions & 1 deletion WinUI3Localizer.SampleApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Serilog;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.Storage;

Expand Down
6 changes: 6 additions & 0 deletions WinUI3Localizer.lutconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<LUTConfig Version="1.0">
<Repository />
<ParallelBuilds>true</ParallelBuilds>
<ParallelTestRuns>true</ParallelTestRuns>
<TestCaseTimeout>180000</TestCaseTimeout>
</LUTConfig>
56 changes: 12 additions & 44 deletions WinUI3Localizer/PriResourceReader.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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;
Expand All @@ -18,29 +16,30 @@ internal PriResourceReader(ResourceManager resourceManager)

public IEnumerable<LanguageDictionary.Item> GetItems(string language, string subTreeName = "Resources")
{
if (string.IsNullOrEmpty(subTreeName) || subTreeName == "/")
if (string.IsNullOrEmpty(subTreeName) is true ||
subTreeName == "/")
{
subTreeName = "Resources";
}
else if (subTreeName.EndsWith('/'))
else if (subTreeName.EndsWith('/') is true)
{
subTreeName = subTreeName[..^1];
}

ResourceMap resourceMap = this.resourceManager.MainResourceMap.TryGetSubtree(subTreeName);
if (resourceMap != null)

if (resourceMap is not null)
{
ResourceContext resourceContext = this.resourceManager.CreateResourceContext();
resourceContext.QualifierValues[KnownResourceQualifierName.Language] = language;

return GetItemsCore(resourceMap, subTreeName, resourceContext);
return PriResourceReader.GetItemsCore(resourceMap, subTreeName, resourceContext);
}

return Enumerable.Empty<LanguageDictionary.Item>();
}


private IEnumerable<LanguageDictionary.Item> GetItemsCore(ResourceMap resourceMap, string subTreeName, ResourceContext resourceContext)
private static IEnumerable<LanguageDictionary.Item> GetItemsCore(ResourceMap resourceMap, string subTreeName, ResourceContext resourceContext)
{
bool isResourcesSubTree = string.Equals(subTreeName, "Resources", StringComparison.OrdinalIgnoreCase);
uint count = resourceMap.ResourceCount;
Expand All @@ -49,49 +48,18 @@ internal PriResourceReader(ResourceManager resourceManager)
{
(string key, ResourceCandidate? candidate) = resourceMap.GetValueByIndex(i, resourceContext);

if (candidate != null && candidate.Kind == ResourceCandidateKind.String)
if (candidate is not null &&
candidate.Kind is 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);
yield return LocalizerBuilder.CreateLanguageDictionaryItem(key, candidate.ValueAsString);
}
reader = new PriResourceReader(manager);
this.readers[normalizedFilePath] = reader;
}

return reader;
}
}

38 changes: 38 additions & 0 deletions WinUI3Localizer/PriResourceReaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Windows.ApplicationModel.Resources;
using System.Collections.Generic;

namespace WinUI3Localizer;

internal class PriResourceReaderFactory
{
private readonly Dictionary<string, PriResourceReader> readers = new();

internal PriResourceReader GetPriResourceReader(string? priFile)
{
string normalizedFilePath = string.Empty;

if (string.IsNullOrEmpty(priFile) is false)
{
normalizedFilePath = System.IO.Path.GetFullPath(priFile);
}

if (this.readers.TryGetValue(normalizedFilePath, out PriResourceReader? reader) is false)
{
ResourceManager manager;

if (string.IsNullOrEmpty(normalizedFilePath) is false)
{
manager = new ResourceManager(normalizedFilePath);
}
else
{
manager = new ResourceManager();
}

reader = new PriResourceReader(manager);
this.readers[normalizedFilePath] = reader;
}

return reader;
}
}
4 changes: 2 additions & 2 deletions WinUI3Localizer/WinUI3Localizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- You/users can add new languages even after deployment
- Use standard Resources.resw
</Description>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<PackageTags>winui3;winappsdk;localization;localize;language;multilanguage</PackageTags>
<PackageProjectUrl>https://github.com/AndrewKeepCoding/WinUI3Localizer</PackageProjectUrl>
<RepositoryUrl>https://github.com/AndrewKeepCoding/WinUI3Localizer</RepositoryUrl>
Expand All @@ -27,7 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>WinUI3Localizer</PackageId>
<Product>WinUI3Localizer</Product>
<Copyright>Copyright (c) Andrew KeepCoding 2023</Copyright>
<Copyright>Copyright (c) Andrew KeepCoding 2024</Copyright>
<Authors>Andrew KeepCoding</Authors>
<PackageReadMeFile>README.md</PackageReadMeFile>
<PackageIcon>winui.png</PackageIcon>
Expand Down

0 comments on commit 8068353

Please sign in to comment.