You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.8 KiB
128 lines
4.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Resources;
|
|
using System.Reflection;
|
|
|
|
namespace MyAvaloniaApp.Services
|
|
{
|
|
/// <summary>
|
|
/// 资源服务实现,用于管理多语言资源
|
|
/// </summary>
|
|
public class ResourceService : IResourceService
|
|
{
|
|
private readonly Dictionary<string, string> _resources;
|
|
private CultureInfo _currentCulture;
|
|
|
|
public event EventHandler<CultureInfo>? CultureChanged;
|
|
|
|
public ResourceService()
|
|
{
|
|
_resources = new Dictionary<string, string>();
|
|
_currentCulture = CultureInfo.CurrentCulture;
|
|
LoadResources();
|
|
}
|
|
|
|
public string GetString(string key)
|
|
{
|
|
return _resources.TryGetValue(key, out var value) ? value : key;
|
|
}
|
|
|
|
public string GetString(string key, params object[] args)
|
|
{
|
|
var format = GetString(key);
|
|
try
|
|
{
|
|
return string.Format(format, args);
|
|
}
|
|
catch
|
|
{
|
|
return format;
|
|
}
|
|
}
|
|
|
|
public void SetCulture(CultureInfo culture)
|
|
{
|
|
if (_currentCulture != culture)
|
|
{
|
|
_currentCulture = culture;
|
|
LoadResources();
|
|
CultureChanged?.Invoke(this, culture);
|
|
}
|
|
}
|
|
|
|
public CultureInfo GetCurrentCulture()
|
|
{
|
|
return _currentCulture;
|
|
}
|
|
|
|
private void LoadResources()
|
|
{
|
|
_resources.Clear();
|
|
|
|
// 根据当前语言加载对应的资源
|
|
var resourceFileName = _currentCulture.Name.StartsWith("zh") ? "Strings.zh-CN" : "Strings.en";
|
|
|
|
// 这里我们使用硬编码的方式,实际项目中可以使用ResourceManager
|
|
LoadHardcodedResources(resourceFileName);
|
|
}
|
|
|
|
private void LoadHardcodedResources(string resourceSet)
|
|
{
|
|
if (resourceSet == "Strings.zh-CN")
|
|
{
|
|
// 中文资源
|
|
_resources["AppTitle"] = "系统管理";
|
|
_resources["AppSubtitle"] = "Management System";
|
|
_resources["NavDashboard"] = "仪表板";
|
|
_resources["NavUsers"] = "用户管理";
|
|
_resources["NavSettings"] = "系统设置";
|
|
_resources["NavReports"] = "报表统计";
|
|
_resources["NavHelp"] = "帮助中心";
|
|
_resources["HeaderTitle"] = "仪表板";
|
|
_resources["HeaderSubtitle"] = "系统概览";
|
|
_resources["BtnRefresh"] = "刷新";
|
|
_resources["BtnSettings"] = "设置";
|
|
_resources["BtnMinimize"] = "最小化";
|
|
_resources["BtnMaximize"] = "最大化";
|
|
_resources["BtnRestore"] = "还原";
|
|
_resources["BtnClose"] = "关闭";
|
|
_resources["BtnCloseTab"] = "关闭标签页";
|
|
_resources["VersionInfo"] = "版本信息";
|
|
_resources["VersionNumber"] = "v1.0.0";
|
|
_resources["PageDashboard"] = "仪表板";
|
|
_resources["PageUsers"] = "用户管理";
|
|
_resources["PageSettings"] = "系统设置";
|
|
_resources["PageReports"] = "报表统计";
|
|
_resources["PageHelp"] = "帮助中心";
|
|
}
|
|
else
|
|
{
|
|
// 英文资源
|
|
_resources["AppTitle"] = "Management System";
|
|
_resources["AppSubtitle"] = "Management System";
|
|
_resources["NavDashboard"] = "Dashboard";
|
|
_resources["NavUsers"] = "User Management";
|
|
_resources["NavSettings"] = "System Settings";
|
|
_resources["NavReports"] = "Reports";
|
|
_resources["NavHelp"] = "Help Center";
|
|
_resources["HeaderTitle"] = "Dashboard";
|
|
_resources["HeaderSubtitle"] = "System Overview";
|
|
_resources["BtnRefresh"] = "Refresh";
|
|
_resources["BtnSettings"] = "Settings";
|
|
_resources["BtnMinimize"] = "Minimize";
|
|
_resources["BtnMaximize"] = "Maximize";
|
|
_resources["BtnRestore"] = "Restore";
|
|
_resources["BtnClose"] = "Close";
|
|
_resources["BtnCloseTab"] = "Close Tab";
|
|
_resources["VersionInfo"] = "Version Information";
|
|
_resources["VersionNumber"] = "v1.0.0";
|
|
_resources["PageDashboard"] = "Dashboard";
|
|
_resources["PageUsers"] = "User Management";
|
|
_resources["PageSettings"] = "System Settings";
|
|
_resources["PageReports"] = "Reports";
|
|
_resources["PageHelp"] = "Help Center";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|