2020-09-07 22:46:50 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.Json;
|
2020-11-25 11:08:12 +08:00
|
|
|
|
using System.Text.Json.Serialization;
|
2020-09-07 22:46:50 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using AntDesign.Locales;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public class LocaleProvider
|
|
|
|
|
{
|
|
|
|
|
public static Locale CurrentLocale => GetCurrentLocale();
|
|
|
|
|
|
|
|
|
|
public static string DefaultLanguage = "en-US";
|
|
|
|
|
|
|
|
|
|
private static readonly ConcurrentDictionary<string, Locale> _localeCache = new ConcurrentDictionary<string, Locale>();
|
|
|
|
|
private static Assembly _resourcesAssembly = typeof(LocaleProvider).Assembly;
|
|
|
|
|
|
|
|
|
|
private static readonly IDictionary<string, string> _availableResources = _resourcesAssembly
|
|
|
|
|
.GetManifestResourceNames()
|
|
|
|
|
.Select(x => Regex.Match(x, @"^.*locales\.(.+)\.json"))
|
|
|
|
|
.Where(x => x.Success)
|
|
|
|
|
.ToDictionary(x => x.Groups[1].Value, x => x.Value);
|
|
|
|
|
|
|
|
|
|
public static Locale GetCurrentLocale()
|
|
|
|
|
{
|
2021-02-05 16:50:12 +08:00
|
|
|
|
var currentCulture = CultureInfo.CurrentUICulture?.Name;
|
2020-09-07 22:46:50 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(currentCulture) || !_availableResources.ContainsKey(currentCulture))
|
|
|
|
|
{
|
|
|
|
|
currentCulture = DefaultLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 16:43:44 +08:00
|
|
|
|
return GetLocale(currentCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Locale GetLocale(string cultureName)
|
|
|
|
|
{
|
|
|
|
|
return _localeCache.GetOrAdd(cultureName, key =>
|
2020-09-07 22:46:50 +08:00
|
|
|
|
{
|
2021-02-05 16:50:12 +08:00
|
|
|
|
string fileName;
|
|
|
|
|
//fallback to default language if not found
|
|
|
|
|
if (!_availableResources.TryGetValue(key, out fileName))
|
|
|
|
|
fileName = _availableResources[DefaultLanguage];
|
2020-09-07 22:46:50 +08:00
|
|
|
|
using var fileStream = _resourcesAssembly.GetManifestResourceStream(fileName);
|
|
|
|
|
if (fileStream == null) return null;
|
|
|
|
|
using var streamReader = new StreamReader(fileStream);
|
|
|
|
|
var content = streamReader.ReadToEnd();
|
|
|
|
|
|
2020-11-25 11:08:12 +08:00
|
|
|
|
var serializerOptions = new JsonSerializerOptions()
|
2020-09-07 22:46:50 +08:00
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
2020-11-25 11:08:12 +08:00
|
|
|
|
};
|
|
|
|
|
serializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
|
|
|
|
|
return JsonSerializer.Deserialize<Locale>(content, serializerOptions);
|
2020-09-07 22:46:50 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-08 12:40:58 +08:00
|
|
|
|
|
2020-11-28 17:53:06 +08:00
|
|
|
|
public static void SetLocale(string cultureName, Locale locale = null)
|
2020-09-08 12:40:58 +08:00
|
|
|
|
{
|
2020-09-08 16:43:44 +08:00
|
|
|
|
var culture = CultureInfo.GetCultureInfo(cultureName);
|
|
|
|
|
|
|
|
|
|
if (culture != null)
|
|
|
|
|
{
|
|
|
|
|
CultureInfo.DefaultThreadCurrentUICulture = culture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (locale != null)
|
|
|
|
|
{
|
|
|
|
|
_localeCache.AddOrUpdate(cultureName, locale, (name, original) => locale);
|
|
|
|
|
}
|
2020-09-08 12:40:58 +08:00
|
|
|
|
}
|
2020-09-07 22:46:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|