Skip to content

Commit

Permalink
Fix bad string resources
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Aug 31, 2024
1 parent def9b1c commit 97c4cad
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 19 deletions.
42 changes: 42 additions & 0 deletions source/iNKORE.UI.WPF.Modern.Controls/Common/ControlStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace iNKORE.UI.WPF.Modern.Common
{
internal class ControlStrings : ResourceAccessor
{
public ControlStrings(Type controlType, ModernControlCategory category) : base(GetControlBaseName(controlType, category), GetControlAssembly(controlType))
{

}


internal static string GetControlBaseName(Type controlType, ModernControlCategory category)
{
var root = controlType.Assembly.GetName().Name;

root = root + "." + category.ToString() + "." + controlType.Name;
root = root + "." + "Strings.Resources";

return root;
}

internal static Assembly GetControlAssembly(Type controlType)
{
return controlType.Assembly;
}
}

internal enum ModernControlCategory
{
Windows,
Community,
Extended
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class InfoBar : Control, IControlProtected
private const string c_iconTextBlockName ="StandardIcon";
private const string c_contentRootName = "ContentRoot";

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(InfoBar));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(InfoBar), ModernControlCategory.Windows);

static InfoBar()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public partial class NavigationView : ContentControl, IControlProtected

static readonly Size c_infSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NavigationView));
internal static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(NavigationView), ModernControlCategory.Windows);

/*
~NavigationView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NavigationViewItemAutomationPeer :
ISelectionItemProvider,
IExpandCollapseProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NavigationView));
private static ControlStrings ResourceAccessor => NavigationView.ResourceAccessor;

public NavigationViewItemAutomationPeer(NavigationViewItem owner) :
base(owner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public partial class NumberBox : Control
const double c_popupShadowDepth = 16.0;
const string c_numberBoxPopupShadowDepthName = "NumberBoxPopupShadowDepth";

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NumberBox));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(NumberBox), ModernControlCategory.Windows);

static NumberBox()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace iNKORE.UI.WPF.Modern.Controls
{
public partial class PersonPicture : Control
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(PersonPicture));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(PersonPicture), ModernControlCategory.Windows);

static PersonPicture()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public partial class PipsPager : Control, IControlProtected
private const string c_pipsPagerButtonVerticalOrientationVisualState = "VerticalOrientation";
private const string c_pipsPagerButtonHorizontalOrientationVisualState = "HorizontalOrientation";

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(PipsPager));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(PipsPager), ModernControlCategory.Windows);

static PipsPager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace iNKORE.UI.WPF.Modern.Automation.Peers
/// </summary>
public class ProgressBarAutomationPeer : RangeBaseAutomationPeer, IRangeValueProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(ProgressBar));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(ProgressBar), ModernControlCategory.Windows);

/// <summary>
/// Initializes a new instance of the ProgressBarAutomationPeer class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace iNKORE.UI.WPF.Modern.Automation.Peers
{
public class ProgressRingAutomationPeer : FrameworkElementAutomationPeer
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(ProgressRing));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(ProgressRing), ModernControlCategory.Windows);

public ProgressRingAutomationPeer(ProgressRing owner) : base(owner)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RatingControlAutomationPeer :
IValueProvider,
IRangeValueProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(RatingControl));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(RatingControl), ModernControlCategory.Windows);

public RatingControlAutomationPeer(RatingControl owner)
: base(owner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace iNKORE.UI.WPF.Modern.Controls
{
public class SplitButton : ContentControl, ICommandSource
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(SplitButton));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(SplitButton), ModernControlCategory.Windows);

static SplitButton()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class TeachingTip : ContentControl, IControlProtected

private const string c_OverlayCornerRadiusName = "OverlayCornerRadius";

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(TeachingTip));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(TeachingTip), ModernControlCategory.Windows);

static TeachingTip()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TimePicker : TimePickerBase
private Button _acceptButton;
private Button _dismissButton;

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(TimePicker));
private static readonly ControlStrings ResourceAccessor = new ControlStrings(typeof(TimePicker), ModernControlCategory.Windows);

static TimePicker()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Resources\Strings\" />
</ItemGroup>

<!--<Target Name="GetDocumentationFile">
<ItemGroup>
<BuildOutputInPackage Include="$(OutputPath)$(AssemblyName).xml" />
Expand Down
40 changes: 34 additions & 6 deletions source/iNKORE.UI.WPF.Modern/Common/ResourceAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Threading;

namespace iNKORE.UI.WPF.Modern.Common
{
Expand Down Expand Up @@ -172,25 +176,29 @@ internal class ResourceAccessor

#endregion

private readonly Type _controlType;
private readonly Type? _controlType;
private readonly string? _baseName;
private readonly Assembly? _assembly;
private ResourceManager _resourceManager;

public ResourceAccessor(Type controlType)
{
_controlType = controlType ?? throw new ArgumentNullException(nameof(controlType));
}

public ResourceAccessor(string baseName, Assembly assembly)
{
_baseName = baseName;
_assembly = assembly;
}

public string GetLocalizedStringResource(string resourceName)
{
try
{
if (_resourceManager is null)
{
var assembly = _controlType.Assembly;
var assemblyName = assembly.GetName().Name;
var controlName = _controlType.Name;
var baseName = $"{assemblyName}.{controlName}.Strings.Resources";
_resourceManager = new ResourceManager(baseName, assembly);
_resourceManager = CreateResourceManager();
}

return _resourceManager.GetString(resourceName);
Expand All @@ -200,5 +208,25 @@ public string GetLocalizedStringResource(string resourceName)
return Strings.ResourceManager.GetString(resourceName) ?? resourceName;
}
}

private ResourceManager CreateResourceManager()
{
var baseName = _baseName;
var assembly = _assembly;

if (_controlType != null)
{
assembly = _controlType.Assembly;
var assemblyName = assembly.GetName().Name;

var controlName = _controlType.Name;
if (assemblyName != null)
{
baseName = $"{assemblyName}.{controlName}.Strings.Resources";
}
}

return new ResourceManager(baseName, assembly);
}
}
}
2 changes: 1 addition & 1 deletion source/iNKORE.UI.WPF.Modern/iNKORE.UI.WPF.Modern.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<AssemblyOriginatorKeyFile>..\..\assets\others\iNKORE.Pulic.snk</AssemblyOriginatorKeyFile>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591;1701;1702;CA1416</NoWarn>
<NoWarn>$(NoWarn);CS1591;1701;1702;CA1416;CS8632</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 97c4cad

Please sign in to comment.