mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-06 05:57:39 +08:00
dcbaecc919
* fix(module:datepicker): incorrect date format strings fix culture set takes effect fallback to default local when locale json does not exist * fix(module:datepicker): do not replace Locale if is set in attribute
78 lines
2.8 KiB
C#
78 lines
2.8 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
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()
|
|
{
|
|
var currentCulture = CultureInfo.CurrentUICulture?.Name;
|
|
if (string.IsNullOrWhiteSpace(currentCulture) || !_availableResources.ContainsKey(currentCulture))
|
|
{
|
|
currentCulture = DefaultLanguage;
|
|
}
|
|
|
|
return GetLocale(currentCulture);
|
|
}
|
|
|
|
public static Locale GetLocale(string cultureName)
|
|
{
|
|
return _localeCache.GetOrAdd(cultureName, key =>
|
|
{
|
|
string fileName;
|
|
//fallback to default language if not found
|
|
if (!_availableResources.TryGetValue(key, out fileName))
|
|
fileName = _availableResources[DefaultLanguage];
|
|
using var fileStream = _resourcesAssembly.GetManifestResourceStream(fileName);
|
|
if (fileStream == null) return null;
|
|
using var streamReader = new StreamReader(fileStream);
|
|
var content = streamReader.ReadToEnd();
|
|
|
|
var serializerOptions = new JsonSerializerOptions()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
};
|
|
serializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
|
|
return JsonSerializer.Deserialize<Locale>(content, serializerOptions);
|
|
});
|
|
}
|
|
|
|
public static void SetLocale(string cultureName, Locale locale = null)
|
|
{
|
|
var culture = CultureInfo.GetCultureInfo(cultureName);
|
|
|
|
if (culture != null)
|
|
{
|
|
CultureInfo.DefaultThreadCurrentUICulture = culture;
|
|
}
|
|
|
|
if (locale != null)
|
|
{
|
|
_localeCache.AddOrUpdate(cultureName, locale, (name, original) => locale);
|
|
}
|
|
}
|
|
}
|
|
}
|