Skip to content

Commit

Permalink
Format code for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKeepCoding committed Apr 12, 2024
1 parent c5b30b5 commit dfbda6c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 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;
}
}

0 comments on commit dfbda6c

Please sign in to comment.