Skip to content

Commit

Permalink
Add GetMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygg01 committed Jan 12, 2024
1 parent 546eb8d commit a52b14f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Linguini.Bundle/IReadBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ string FormatPattern(Pattern pattern, IDictionary<string, IFluentType>? args,
bool TryGetMessage(string id, IDictionary<string, IFluentType>? args,
[NotNullWhen(false)] out IList<FluentError>? 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);
}


Expand Down Expand Up @@ -223,6 +223,28 @@ public static bool HasAttrMessage(this IReadBundle bundle, string idWithAttr)
return bundle.GetAttrMessage(msgWithAttr, args);
}


/// <summary>
/// Retrieves a localized message from the given bundle.
/// </summary>
/// <param name="bundle">The bundle to retrieve the message from.</param>
/// <param name="id">The ID of the message to retrieve.</param>
/// <param name="attribute">The optional attribute of the message. Defaults to null.</param>
/// <param name="args">The optional dictionary of arguments to be used in the message resolution. Defaults to null.</param>
/// <returns>The localized message if found, otherwise null.</returns>
/// <exception cref="LinguiniException">Thrown when there are errors retrieving the message.</exception>
public static string? GetMessage(this IReadBundle bundle, string id, string? attribute = null,
IDictionary<string, IFluentType>? args = null)
{
bundle.TryGetMessage(id, attribute, args, out var errors, out var message);
if (errors is { Count: > 0 })
{
throw new LinguiniException(errors);
}

return message;
}

/// <summary>
/// Convenience method for <see cref="IReadBundle.TryGetAttrMessage"/>
/// </summary>
Expand Down

0 comments on commit a52b14f

Please sign in to comment.