added common function to derive CultureInfo for formatting from the active plugins IETF Tag

This commit is contained in:
nilsk 2026-03-31 14:41:50 +02:00
parent a59e10ea14
commit 22b8bdd576

View File

@ -1,3 +1,4 @@
using System.Globalization;
using System.Text;
namespace AIStudio.Tools;
@ -19,4 +20,32 @@ public static class CommonTools
return sb.ToString();
}
}
/// <summary>
/// Resolves a <see cref="CultureInfo"/> from the active language plugin's IETF tag.
/// </summary>
/// <param name="ietfTag">The IETF language tag provided by the active language plugin.</param>
/// <returns>The matching culture when the tag is valid; otherwise <see cref="CultureInfo.InvariantCulture"/>.</returns>
public static CultureInfo DeriveActiveCultureOrInvariant(string? ietfTag)
{
if (string.IsNullOrWhiteSpace(ietfTag))
return CultureInfo.InvariantCulture;
try
{
return CultureInfo.GetCultureInfo(ietfTag);
}
catch (CultureNotFoundException)
{
return CultureInfo.InvariantCulture;
}
}
/// <summary>
/// Formats a timestamp using the short date and time pattern of the specified culture.
/// </summary>
/// <param name="timestamp">The timestamp to format.</param>
/// <param name="culture">The culture whose short date and time pattern should be used.</param>
/// <returns>The localized timestamp string.</returns>
public static string FormatTimestampToGeneral(DateTime timestamp, CultureInfo culture) => timestamp.ToString("g", culture);
}