Skip to content

Commit

Permalink
Adjust LocaliedString for better usability and function parity;
Browse files Browse the repository at this point in the history
  • Loading branch information
onepiecefreak3 committed Sep 30, 2024
1 parent e2a57fc commit 5436ec8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ImGui.Forms/ImGui.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.1.4</version>
<version>1.1.5</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
Expand Down
45 changes: 29 additions & 16 deletions ImGui.Forms/Localization/LocalizedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ public struct LocalizedString
/// </summary>
public bool IsEmpty => string.IsNullOrEmpty(_fixedText) && (string.IsNullOrEmpty(_id) || _args == null);

/// <summary>
/// Set up a localized string, based on a localization ID.
/// </summary>
/// <param name="id">The ID of the localization to return.</param>
public LocalizedString(string id) : this(id, null, Array.Empty<Func<object>>())
{ }

/// <summary>
/// Set up a localized string, based on a localization ID and formatting arguments.
/// </summary>
/// <param name="id">The ID of the localization to return.</param>
/// <param name="args">The arguments of the localization to return.</param>
public LocalizedString(string id, params Func<object>[] args) : this(id, null, args)
{ }

private LocalizedString(string id, string fixedText, Func<object>[] args)
{
_locale = null;
Expand All @@ -56,6 +41,34 @@ private LocalizedString(string id, string fixedText, Func<object>[] args)
_fixedText = null;
}

/// <summary>
/// Set up a localized string, based on a localization ID.
/// </summary>
/// <param name="localizationId">The ID of the localization to represent.</param>
public static LocalizedString FromId(string localizationId)
{
return new LocalizedString(localizationId, null, Array.Empty<Func<object>>());
}

/// <summary>
/// Set up a localized string, based on a localization ID and formatting arguments.
/// </summary>
/// <param name="localizationId">The ID of the localization to represent.</param>
/// <param name="args">The arguments of the localization to represent.</param>
public static LocalizedString FromId(string localizationId, params Func<object>[] args)
{
return new LocalizedString(localizationId, null, args);
}

/// <summary>
/// Set up a localized string, based on a fixed text.
/// </summary>
/// <param name="fixedText">The fixed text to represent.</param>
public static LocalizedString FromText(string fixedText)
{
return new LocalizedString(null, fixedText ?? string.Empty, Array.Empty<Func<object>>());
}

public override string ToString()
{
if (_fixedText != null)
Expand Down Expand Up @@ -95,7 +108,7 @@ public override bool Equals(object obj)
return _id == locStr._id;
}

public static implicit operator LocalizedString(string s) => new(null, s ?? string.Empty, Array.Empty<Func<object>>());
public static implicit operator LocalizedString(string s) => FromText(s);
public static implicit operator string(LocalizedString s) => s.ToString();
}
}

0 comments on commit 5436ec8

Please sign in to comment.