Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created June 2, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karljj1/4b10868766a33435c2fe34606874a274 to your computer and use it in GitHub Desktop.
Save karljj1/4b10868766a33435c2fe34606874a274 to your computer and use it in GitHub Desktop.
A workaround for 1247232
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.SmartFormat;
using UnityEngine.Localization.SmartFormat.Core.Extensions;
public class FixListFormatter : MonoBehaviour
{
void Start()
{
var formatters = LocalizationSettings.StringDatabase.SmartFormatter.FormatterExtensions;
for (int i = 0; i < formatters.Count; ++i)
{
// If the name is null then create a new instance so we can get the default names and then copy it across
if (formatters[i].Names == null)
{
// We only support 2 types of constructor. A default and one that takes a SmartFormatter.
var type = formatters[i].GetType();
var hasDefaultConstructor = type.GetConstructors().Any(c => c.GetParameters().Length == 0);
var hasSmartFormatterConstructor = type.GetConstructors().Any(c => c.GetParameters().Length == 1 && c.GetParameters()[0].ParameterType == typeof(SmartFormatter));
if (hasSmartFormatterConstructor || hasDefaultConstructor)
{
var instance = hasDefaultConstructor ? Activator.CreateInstance(type) : Activator.CreateInstance(type, LocalizationSettings.StringDatabase.SmartFormatter);
formatters[i].Names = (instance as IFormatter).Names;
Debug.Log("Fixed null Names for" + type.Name);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment