From a52b14fd77241e01f0de7d6ceac813cd8a88a167 Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Sat, 13 Jan 2024 00:27:30 +0100 Subject: [PATCH] Add GetMessage --- Linguini.Bundle/IReadBundle.cs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Linguini.Bundle/IReadBundle.cs b/Linguini.Bundle/IReadBundle.cs index e4415f5..6aed34f 100644 --- a/Linguini.Bundle/IReadBundle.cs +++ b/Linguini.Bundle/IReadBundle.cs @@ -87,7 +87,7 @@ string FormatPattern(Pattern pattern, IDictionary? args, bool TryGetMessage(string id, IDictionary? args, [NotNullWhen(false)] out IList? errors, [NotNullWhen(true)] out string? message) { - return this.TryGetMessage(id, null, args, out errors, out message); + return TryGetMessage(id, null, args, out errors, out message); } @@ -223,6 +223,28 @@ public static bool HasAttrMessage(this IReadBundle bundle, string idWithAttr) return bundle.GetAttrMessage(msgWithAttr, args); } + + /// + /// Retrieves a localized message from the given bundle. + /// + /// The bundle to retrieve the message from. + /// The ID of the message to retrieve. + /// The optional attribute of the message. Defaults to null. + /// The optional dictionary of arguments to be used in the message resolution. Defaults to null. + /// The localized message if found, otherwise null. + /// Thrown when there are errors retrieving the message. + public static string? GetMessage(this IReadBundle bundle, string id, string? attribute = null, + IDictionary? args = null) + { + bundle.TryGetMessage(id, attribute, args, out var errors, out var message); + if (errors is { Count: > 0 }) + { + throw new LinguiniException(errors); + } + + return message; + } + /// /// Convenience method for ///