using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Brutal.Logging;
namespace Brutal.Localization;
///
/// Class for managing localization of strings via LString instances. You can create multiple instances of
/// LocalizedStrings and each will act without interfering with any other instances.
/// Typically, however, you will use a single static instance which can be used simply by calling the static
/// methods in LStrings. The LStrings methods all simply redirect to a shared instance of LocalizedStrings.
///
public class LocalizedStrings
{
///
/// A delegate that takes a and return a localized version of that LString's template string.
/// In typical use, this delegate acts as a language provider.
/// Your implementation of must set localizedTemplateString to a non-null
/// string if you return true (Indicating that a string was found).
///
/// The LString whose localized string has been requested.
/// The out variable that is set if this method returns true.
/// true if the delegate found and returned a string. Otherwise, false.
public delegate bool GetLocalizedStringDelegate(
LString lString,
[NotNullWhen(true)] out string? localizedTemplateString
);
///
/// Construct a new instance of LocalizedStrings. Each instance is its own string localization environment.
///
/// The Regex to use to match replacement variables in LStrings created by this LocalizedStrings instance. If null, uses
public LocalizedStrings(Regex? replacementPattern = null)
{
_lStringProvider = null;
_stringInstances = new();
_replacementPattern = replacementPattern ?? DefaultOptions.ReplacementPattern();
}
///
/// Set the GetLocalizedStringDelegate that the Brutal localization system will use to look up localized strings. This function
/// will also localize all existing LString instances with the new provider.
///
///
/// A that the localization system will use to look up strings.
public void SetLocalizedStringProvider(GetLocalizedStringDelegate stringProvider)
{
if (stringProvider is null)
throw new ArgumentNullException(nameof(stringProvider), @"Null stringProvider");
lock (_lock)
{
_lStringProvider = stringProvider;
foreach (var (key, lString) in _stringInstances)
{
if (!_lStringProvider(lString, out var localizedString))
{
lString.RemoveLocalization();
continue;
}
lString.Localize(localizedString);
}
}
}
///
/// Returns the delegate that is currently providing localized versions of LString instances.
///
/// The or null if one is not set.
public GetLocalizedStringDelegate? LocalizedStringProvider => _lStringProvider;
///
/// Removes the provider of localized strings. After calling
/// LString instances will return their template string from calls to Format.
///
public void RemoveLocalizedStringProvider()
{
lock (_lock)
{
_lStringProvider = null;
foreach (var (_, lString) in _stringInstances)
lString.RemoveLocalization();
}
}
///
/// Instantiates a new, unique LString instance with a template string. If there is a string provider
/// currently set, the LString will also be localized to the current language.
///
/// A string representing a unique of the LString to create.
/// The string that defines the non-localized string contents
/// The newly created LString.
/// Thrown if a LString with the same id already exists.
/// Thrown if templateString is null.
public LString Create(string id, string templateString)
{
return Create(LStringId.From(id), templateString);
}
///
/// Instantiates a new, unique LString instance with a template string. If there is a GetLocalizedStringDelegate
/// currently set, the LString will also be localized to the current language with a language string with the same id.
///
/// The unique of the LString to create.
/// The string that defines the non-localized string contents
/// The newly created LString.
/// Thrown if a LString with the same id already exists.
/// Thrown if templateString is null.
public LString Create(LStringId id, string templateString)
{
if (templateString is null)
throw new ArgumentNullException(nameof(templateString), @"Null template string");
lock (_lock)
{
var lString = new LString(id, templateString, _replacementPattern);
if (!_stringInstances.TryAdd(lString.Id, lString))
throw new ArgumentException($"Duplicate LString with id {id}");
if (!HasStringProvider)
return lString;
if (!_lStringProvider!(lString, out var localizedString))
{
DefaultCategory.Log.Debug($"Missing string with id {lString.Id}");
return lString;
}
lString.Localize(localizedString);
return lString;
}
}
///
/// Attempts to get a LString with the given id. If it doesn't exist, it will be created and returned.
///
/// The of the LString to find or create.
/// The templateString to use for the new LString if a new LString is created.
/// The LString instance found or created.
public LString GetOrCreate(LStringId id, string templateString)
{
lock (_lock)
{
if (TryGet(id, out var localizedString))
return localizedString;
return Create(id, templateString);
}
}
///
public LString GetOrCreate(string id, string templateString)
{
return GetOrCreate(LStringId.From(id), templateString);
}
///
/// Removes a LString from the set of localized strings.
///
/// The identifier of the LString to remove.
/// true if the LString was successfully removed; otherwise, false.
public bool Remove(LStringId id)
{
lock (_lock)
{
return _stringInstances.Remove(id);
}
}
///
/// Removes all LString instances.
///
public void RemoveAll()
{
lock (_lock)
{
_stringInstances.Clear();
}
}
///
/// Attempt to get a LString with the given id and returns whether it was found.
///
/// The of the LString to get.
/// The LString that is set, if found. Otherwise undefined.
/// True if the LStringId was found. Otherwise, false.
public bool TryGet(LStringId id, [NotNullWhen(true)] out LString? localizedString)
{
lock (_lock)
{
return _stringInstances.TryGetValue(id, out localizedString);
}
}
///
/// Returns an IReadOnlyDictionary of all LString instances created through Brutal.Localization.
/// Instances of LString created manually (via new()) are not included in here.
///
/// This is a lightweight call and does not copy the set of strings. However, the returned reference
/// is NOT thread-safe. If the underlying collection is modified (e.g., with calls to Create methods of ) then the underlying collection
/// will be modified and will generate errors if you are iterating the collection at the time.
///
public IReadOnlyDictionary LStringInstances =>
_stringInstances.AsReadOnly();
#region Private
private bool HasStringProvider => _lStringProvider is not null;
private GetLocalizedStringDelegate? _lStringProvider;
private readonly Dictionary _stringInstances;
private readonly Lock _lock = new();
private readonly Regex _replacementPattern;
#endregion
}