MessageLookupT Class |
Namespace: KSPDev.GUIUtils
public sealed class MessageLookup<T> where T : struct, new()
The MessageLookupT type exposes the following members.
Name | Description | |
---|---|---|
![]() ![]() | MessageLookupT | Constructs a lookup from the provided dictionary. |
Name | Description | |
---|---|---|
![]() | defaultMessage | Message to return when the requested key is not found. |
![]() | messages | Mapping of the key to the messages. |
Use it when a definite set of values of the same kind needs to be mapped to the localized strings. A good example of such mapping is a localization of the enum type values. However, for this class the key type doesn't need to be enum. It can be any number, or even a struct. It cannot be a string, though.
public class MessageLookupDemo1 : PartModule { enum MyEnum { One, Two, Three } // Create the messages separately from the lookup to allow the LocalizationTool to pick them up. static readonly Message msg_1 = new Message("#msg1", defaultTemplate: "-ONE-"); static readonly Message msg_2 = new Message("#msg2", defaultTemplate: "-TWO-"); static readonly Message msg_3 = new Message("#msg3", defaultTemplate: "-THREE-"); static readonly MessageLookup<MyEnum> msg = new MessageLookup<MyEnum>( new Dictionary<MyEnum, Message>() { {MyEnum.One, msg_1}, {MyEnum.Two, msg_2}, {MyEnum.Three, msg_3}, }); // Depending on the current language in the system, this method will present the values in // different languages. void Show() { Debug.Log(msg.Lookup(MyEnum.One)); // Prints: "-ONE-" Debug.Log(msg.Lookup(MyEnum.Two)); // Prints: "-TWO-" Debug.Log(msg.Lookup(MyEnum.Three)); // Prints: "-THREE-" } }
public class MessageLookupDemo2 : PartModule { enum MyEnum { One, Two, Three, } // Create the messages separately from the lookup to allow the LocalizationTool to pick them up. static readonly Message msg_1 = new Message("#msg1", defaultTemplate: "-ONE-"); static readonly Message defMsg = new Message("#def", defaultTemplate: "-DEFAULT-"); static readonly MessageLookup<MyEnum> msg = new MessageLookup<MyEnum>( new Dictionary<MyEnum, Message>() { {MyEnum.One, msg_1}, }, defaultMessage: defMsg); void Show() { Debug.Log(msg.Lookup(MyEnum.One)); // Prints: "-ONE-" Debug.Log(msg.Lookup(MyEnum.Two)); // Prints: "-DEFAULT-" Debug.Log(msg.Lookup(MyEnum.Three)); // Prints: "-DEFAULT-" } }
public class MessageLookupDemo3 : PartModule { enum MyEnum { One, Two, Three, } // Create the messages separately from the lookup to allow the LocalizationTool to pick them up. static readonly Message msg_1 = new Message("#msg1", defaultTemplate: "-ONE-"); static readonly MessageLookup<MyEnum> msg = new MessageLookup<MyEnum>( new Dictionary<MyEnum, Message>() { {MyEnum.One, msg_1}, }); void Show() { Debug.Log(msg.Lookup(MyEnum.One)); // Prints: "-ONE-" Debug.Log(msg.Lookup(MyEnum.Two)); // Prints: "Two" } }