-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTrTextIdConverter.cs
81 lines (68 loc) · 3.16 KB
/
TrTextIdConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Avalonia.Data.Converters;
using System;
using System.Globalization;
namespace CodingSeb.Localization.Avalonia
{
/// <summary>
/// Converter to Translate a the binding textId (In CurrentLanguage or if specified in LanguageId)
/// If Translation don't exist return DefaultText
/// Not usable in TwoWay Binding mode.
/// </summary>
public class TrTextIdConverter : IValueConverter
{
public TrTextIdConverter()
{
//WeakEventManager<Loc, CurrentLanguageChangedEventArgs>.AddHandler(Loc.Instance, nameof(Loc.Instance.CurrentLanguageChanged), CurrentLanguageChanged);
}
~TrTextIdConverter()
{
//WeakEventManager<Loc, CurrentLanguageChangedEventArgs>.RemoveHandler(Loc.Instance, nameof(Loc.Instance.CurrentLanguageChanged), CurrentLanguageChanged);
}
/// <summary>
/// The text to return if no text correspond to textId in the current language
/// </summary>
public string DefaultText { get; set; }
/// <summary>
/// The language id in which to get the translation. To Specify if not CurrentLanguage
/// </summary>
public string LanguageId { get; set; }
/// <summary>
/// A string format where will be injected the binding
/// by Default => {0}
/// </summary>
public string TextIdStringFormat { get; set; } = "{0}";
/// <summary>
/// AllowTo use a converter on the binded value before to inject it in the TextId
/// </summary>
public IValueConverter PreConverter { get; set; }
/// <summary>
/// To provide a prefix to add at the begining of the translated text.
/// </summary>
public string Prefix { get; set; } = string.Empty;
/// <summary>
/// To provide a suffix to add at the end of the translated text.
/// </summary>
public string Suffix { get; set; } = string.Empty;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string textId = PreConverter?.Convert(value, null, null, null)?.ToString() ?? value?.ToString();
return Prefix + (string.IsNullOrEmpty(textId) ? "" : Loc.Tr(string.Format(TextIdStringFormat, textId), DefaultText?.Replace("[apos]", "'"), LanguageId)) + Suffix;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
public object ProvideValue(IServiceProvider serviceProvider)
{
//SetXamlObjects(serviceProvider);
return this;
}
private void CurrentLanguageChanged(object sender, CurrentLanguageChangedEventArgs e)
{
//if (xamlTargetObject != null && xamlDependencyProperty != null)
//{
// if(IsInAMultiBinding)
// BindingOperations.GetMultiBindingExpression(xamlTargetObject, xamlDependencyProperty)?.UpdateTarget();
// else
// BindingOperations.GetBindingExpression(xamlTargetObject, xamlDependencyProperty)?.UpdateTarget();
//}
}
}
}