-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalizationProviderBase.cs
45 lines (34 loc) · 1.38 KB
/
LocalizationProviderBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Globalization;
using TekDeq.Localization.Core.Options;
using TekDeq.Localization.Core.Providers.Interfaces;
namespace TekDeq.Localization.Core.Providers.Abstract;
public abstract class LocalizationProviderBase : ILocalizationProvider
{
private CultureInfo? _currentCulture;
protected LocalizationProviderBase(LocalizationOptions options)
{
Options = options;
ValidateOptions(Options);
CurrentCulture = Options.CurrentCulture;
}
protected LocalizationOptions Options { get; }
public CultureInfo CurrentCulture
{
get => _currentCulture!;
set => ChangeCulture(value);
}
public abstract string GetResourceByKey(string key);
public IEnumerable<CultureInfo> AvailableCultures => Options.Cultures;
protected abstract Stream? GetResourceStream(CultureInfo culture);
protected virtual void ValidateOptions(LocalizationOptions options)
{
if (!options.Cultures.Any()) throw new ArgumentException("The culture list is empty.");
if (options.Cultures.All(x => x.IetfLanguageTag != options.DefaultCulture.IetfLanguageTag))
throw new ArgumentException(
$"The default culture {options.DefaultCulture.IetfLanguageTag} is not present in the culture list.");
}
protected virtual void ChangeCulture(CultureInfo culture)
{
_currentCulture = culture;
}
}