-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTrLanguageIdConverter.cs
46 lines (39 loc) · 1.55 KB
/
TrLanguageIdConverter.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
using Avalonia.Data.Converters;
using System;
using System.Globalization;
namespace CodingSeb.Localization.Avalonia
{
/// <summary>
/// Converter to Translate a specific TextId in the Binding LanguageId.
/// If Translation don't exist return DefaultText.
/// Not usable in TwoWay Binding mode.
/// </summary>
public class TrLanguageIdConverter : IValueConverter
{
/// <summary>
/// To force the use of a specific identifier
/// </summary>
public virtual string TextId { get; set; }
/// <summary>
/// The text to return if no text correspond to textId in the current language
/// </summary>
public string DefaultText { 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)
{
return Prefix + Loc.Tr(TextId, DefaultText?.Replace("[apos]", "'"), value?.ToString()) + Suffix;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}