将Feature/XP.Common和Feature/XP.Hardware分支合并至Develop/XP.forHardwareAndCommon,完善XPapp注册和相关硬件类库通用类库功能。
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Windows.Markup;
|
||||
using XP.Common.Localization.Interfaces;
|
||||
|
||||
namespace XP.Common.Localization.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 本地化标记扩展 | Localization markup extension
|
||||
/// 用法: {loc:Localization Key=ResourceKey} | Usage: {loc:Localization Key=ResourceKey}
|
||||
/// </summary>
|
||||
[MarkupExtensionReturnType(typeof(string))]
|
||||
public class LocalizationExtension : MarkupExtension
|
||||
{
|
||||
private static ILocalizationService? _localizationService;
|
||||
private string _key = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 资源键 | Resource key
|
||||
/// </summary>
|
||||
[ConstructorArgument("key")]
|
||||
public string Key
|
||||
{
|
||||
get => _key;
|
||||
set => _key = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 默认构造函数 | Default constructor
|
||||
/// </summary>
|
||||
public LocalizationExtension()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带资源键的构造函数 | Constructor with resource key
|
||||
/// </summary>
|
||||
/// <param name="key">资源键 | Resource key</param>
|
||||
public LocalizationExtension(string key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化本地化服务(由 CommonModule 调用)| Initialize localization service (called by CommonModule)
|
||||
/// </summary>
|
||||
/// <param name="localizationService">本地化服务实例 | Localization service instance</param>
|
||||
public static void Initialize(ILocalizationService localizationService)
|
||||
{
|
||||
_localizationService = localizationService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提供本地化字符串值 | Provide localized string value
|
||||
/// </summary>
|
||||
/// <param name="serviceProvider">服务提供者 | Service provider</param>
|
||||
/// <returns>本地化字符串 | Localized string</returns>
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_key))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (_localizationService == null)
|
||||
{
|
||||
// 设计时回退 | Design-time fallback
|
||||
return $"[{_key}]";
|
||||
}
|
||||
|
||||
// 直接返回当前语言的翻译字符串 | Return translated text for current language
|
||||
return _localizationService.GetString(_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user