Create localization helper classes

This commit is contained in:
SysC0mp 2020-02-25 17:38:47 +01:00
parent d204df2b18
commit 2e714a8d7b
3 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,37 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;
namespace Wox.Core
{
public class LocalizationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(string) && value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
string localizedDescription = string.Empty;
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description)))
{
localizedDescription = attributes[0].Description;
}
return (!String.IsNullOrEmpty(localizedDescription)) ? localizedDescription : value.ToString();
}
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,27 @@
using System.ComponentModel;
using Wox.Core.Resource;
namespace Wox.Core
{
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
private readonly Internationalization _translator;
private readonly string _resourceKey;
public LocalizedDescriptionAttribute(string resourceKey)
{
_translator = InternationalizationManager.Instance;
_resourceKey = resourceKey;
}
public override string Description
{
get
{
string description = _translator.GetTranslation(_resourceKey);
return string.IsNullOrWhiteSpace(description) ?
string.Format("[[{0}]]", _resourceKey) : description;
}
}
}
}

View file

@ -109,6 +109,8 @@
</Compile>
<Compile Include="Plugin\ExecutablePlugin.cs" />
<Compile Include="Plugin\PluginsLoader.cs" />
<Compile Include="Resource\LocalizationConverter.cs" />
<Compile Include="Resource\LocalizedDescriptionAttribute.cs" />
<Compile Include="Updater.cs" />
<Compile Include="Resource\AvailableLanguages.cs" />
<Compile Include="Resource\Internationalization.cs" />