85 lines
2.6 KiB
Plaintext
85 lines
2.6 KiB
Plaintext
<#@ template debug="false" hostspecific="false" language="C#" #>
|
|
<#@ assembly name="System.Core" #>
|
|
<#@ import namespace="System.Linq" #>
|
|
<#@ import namespace="System.Collections.Generic"#>
|
|
<#@ assembly name="$(TargetPath)" #>
|
|
<#@ import namespace="HandyControl.Properties.Langs" #>
|
|
<#@ output extension=".cs" #>
|
|
<#
|
|
var resourceType = typeof(Lang);
|
|
var propertyNameList = resourceType.GetProperties().Where(item => item.PropertyType == typeof(string)).Select(item => item.Name);
|
|
var langDic = new Dictionary<string, string>();
|
|
foreach(var item in propertyNameList)
|
|
{
|
|
langDic[item] =
|
|
@$"/// <summary>
|
|
/// {string.Format(Lang.LangComment, Lang.ResourceManager.GetString(item, Lang.Culture))}
|
|
/// </summary>";
|
|
}
|
|
#>
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using HandyControl.Tools;
|
|
|
|
namespace HandyControl.Properties.Langs
|
|
{
|
|
public class LangProvider : INotifyPropertyChanged
|
|
{
|
|
internal static LangProvider Instance { get; } = ResourceHelper.GetResourceInternal<LangProvider>("Langs");
|
|
|
|
private static string CultureInfoStr;
|
|
|
|
internal static CultureInfo Culture
|
|
{
|
|
get => Lang.Culture;
|
|
set
|
|
{
|
|
if (value == null) return;
|
|
if (Equals(CultureInfoStr, value.EnglishName)) return;
|
|
Lang.Culture = value;
|
|
CultureInfoStr = value.EnglishName;
|
|
|
|
Instance.UpdateLangs();
|
|
}
|
|
}
|
|
|
|
public static string GetLang(string key) => Lang.ResourceManager.GetString(key, Culture);
|
|
|
|
public static void SetLang(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string key) =>
|
|
BindingOperations.SetBinding(dependencyObject, dependencyProperty, new Binding(key)
|
|
{
|
|
Source = Instance,
|
|
Mode = BindingMode.OneWay
|
|
});
|
|
|
|
private void UpdateLangs()
|
|
{
|
|
<#foreach(var item in propertyNameList){#>
|
|
OnPropertyChanged(nameof(<#=item#>));
|
|
<#}#>
|
|
}
|
|
|
|
<#foreach(var item in propertyNameList){#>
|
|
<#=langDic[item]#>
|
|
public string <#=item#> => Lang.<#=item#>;
|
|
|
|
<#}#>
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName) =>
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public class LangKeys
|
|
{
|
|
<#foreach(var item in propertyNameList){#>
|
|
<#=langDic[item]#>
|
|
public static string <#=item#> = nameof(<#=item#>);
|
|
|
|
<#}#>
|
|
}
|
|
}
|