mirror of
https://github.com/ppy/osu
synced 2024-12-24 15:53:37 +00:00
Enable localisation for SettingSourceAttribute
This commit is contained in:
parent
a42b8092af
commit
7cbe2fa522
@ -9,6 +9,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using JetBrains.Annotations;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Extensions.ObjectExtensions;
|
||||
using osu.Framework.Extensions.TypeExtensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Localisation;
|
||||
@ -43,12 +44,47 @@ namespace osu.Game.Configuration
|
||||
/// </remarks>
|
||||
public Type? SettingControlType { get; set; }
|
||||
|
||||
public SettingSourceAttribute(Type declaringType, string label, string? description = null)
|
||||
{
|
||||
Label = getLocalisableStringFromMember(label) ?? string.Empty;
|
||||
Description = getLocalisableStringFromMember(description) ?? string.Empty;
|
||||
|
||||
LocalisableString? getLocalisableStringFromMember(string? member)
|
||||
{
|
||||
if (member == null)
|
||||
return null;
|
||||
|
||||
var property = declaringType.GetMember(member, BindingFlags.Static | BindingFlags.Public).FirstOrDefault();
|
||||
|
||||
if (property == null)
|
||||
return null;
|
||||
|
||||
switch (property)
|
||||
{
|
||||
case FieldInfo f:
|
||||
return (LocalisableString)f.GetValue(null).AsNonNull();
|
||||
|
||||
case PropertyInfo p:
|
||||
return (LocalisableString)p.GetValue(null).AsNonNull();
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException($"Member \"{member}\" was not found in type {declaringType} (must be a static field or property)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SettingSourceAttribute(string? label, string? description = null)
|
||||
{
|
||||
Label = label ?? string.Empty;
|
||||
Description = description ?? string.Empty;
|
||||
}
|
||||
|
||||
public SettingSourceAttribute(Type declaringType, string label, string description, int orderPosition)
|
||||
: this(declaringType, label, description)
|
||||
{
|
||||
OrderPosition = orderPosition;
|
||||
}
|
||||
|
||||
public SettingSourceAttribute(string label, string description, int orderPosition)
|
||||
: this(label, description)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user