Skip to content

Commit

Permalink
Fix localization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MidoriKami committed Jul 21, 2024
1 parent b70b65c commit 00b2809
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 3 additions & 4 deletions Classes/ImGuiTweaks.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Drawing;
using System.Numerics;
using System.Resources;
using Dalamud.Interface;
using Dalamud.Interface.Components;
using Dalamud.Interface.ManagedFontAtlas;
Expand Down Expand Up @@ -52,12 +51,12 @@ public static void TextColoredUnformatted(Vector4 color, string text) {
ImGui.TextUnformatted(text);
}

public static bool EnumCombo<T>(string label, ref T refValue, ResourceManager? resourceManager = null) where T : Enum {
using var combo = ImRaii.Combo(label, refValue.GetDescription(resourceManager));
public static bool EnumCombo<T>(string label, ref T refValue) where T : Enum {
using var combo = ImRaii.Combo(label, refValue.GetDescription());
if (!combo) return false;

foreach (Enum enumValue in Enum.GetValues(refValue.GetType())) {
if (!ImGui.Selectable(enumValue.GetDescription(resourceManager), enumValue.Equals(refValue))) continue;
if (!ImGui.Selectable(enumValue.GetDescription(), enumValue.Equals(refValue))) continue;

refValue = (T)enumValue;
return true;
Expand Down
14 changes: 11 additions & 3 deletions Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Resources;

namespace KamiLib.Extensions;

public static class EnumExtensions {
public static string GetDescription(this Enum value, ResourceManager? resourceManager = null) {

public static Func<ResourceManager?>? GetResourceManagerFunc { get; set; }

public static Func<CultureInfo?>? GetCultureInfoFunc { get; set; }

public static string GetDescription(this Enum value) {
var type = value.GetType();
if (Enum.GetName(type, value) is { } name) {
if (type.GetField(name) is { } field) {
if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attr) {
if (resourceManager?.GetString(attr.Description) is { } localized) {
return localized;
if (GetResourceManagerFunc?.Invoke() is { } resourceManager && GetCultureInfoFunc?.Invoke() is { } cultureInfo) {
if (resourceManager.GetString(attr.Description, cultureInfo) is { } localizedString) {
return localizedString;
}
}

return attr.Description;
Expand Down

0 comments on commit 00b2809

Please sign in to comment.