using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using ReactiveUI;
namespace System.Application.Resx
{
///
/// 提供对多语言资源的访问。
///
public class ResourceService : ReactiveObject
{
// singleton
public static ResourceService Current => new ResourceService();
///
/// 支持的语言简写
///
private readonly string[] supportedCultureNames =
{
"zh-Hans", // Resources.resx
"zh-Hant",
"en",
"ja",
"ru",
};
///
/// 支持的语言Steam平台名称
///
private readonly Dictionary supportedCultureSteamNames = new Dictionary
{
{ "zh-Hans","schinese"},
{ "zh-Hant","tchinese"},
{ "en","english"},
{ "ko","koreana"},
{ "ja","japanese"},
{ "ru","russian"},
};
///
/// 获取多语种资源。
///
public System.Application.UI.Resx.AppResources Resources { get; }
///
/// 获得受支持的文化。
///
public IReadOnlyCollection SupportedCultures { get; }
private ResourceService()
{
this.Resources = new System.Application.UI.Resx.AppResources();
#pragma warning disable CS8619 // 值中的引用类型的为 Null 性与目标类型不匹配。
this.SupportedCultures = this.supportedCultureNames
.Select(x =>
{
try
{
return CultureInfo.GetCultureInfo(x);
}
catch (CultureNotFoundException)
{
return null;
}
})
.Where(x => x != null)
.ToList();
#pragma warning restore CS8619 // 值中的引用类型的为 Null 性与目标类型不匹配。
System.Application.UI.Resx.AppResources.Culture = this.SupportedCultures.First();
}
///
/// 使用指定的区域性名称更改资源的区域性。
///
///
public void ChangeCulture(string name)
{
System.Application.UI.Resx.AppResources.Culture = this.SupportedCultures.SingleOrDefault(x => x.Name == name);
//GeneralSettings.Culture.Value = SteamTools.Properties.Resources.Culture?.Name;
this.RaisePropertyChanged(nameof(this.Resources));
}
public string GetCurrentCultureSteamLanguageName()
{
return System.Application.UI.Resx.AppResources.Culture == null ? supportedCultureSteamNames.First().Value : supportedCultureSteamNames[System.Application.UI.Resx.AppResources.Culture.Name];
}
}
}