Click or drag to resize

MessageLookupT Class

Holds a mapping of a value to a localized message.
Inheritance Hierarchy
SystemObject
  KSPDev.GUIUtilsMessageLookupT

Namespace:  KSPDev.GUIUtils
Assembly:  KSPDev_Utils.2.0 (in KSPDev_Utils.2.0.dll) Version: 2.0 for KSP v1.8+
Syntax
C#
public sealed class MessageLookup<T>
where T : struct, new()
Request Example View Source

Type Parameters

T
Type of the key. It must be non-nullable.

The MessageLookupT type exposes the following members.

Constructors
Methods
  NameDescription
Public methodCode exampleLookup
Finds and returns a message for the provided key.
Top
Fields
  NameDescription
Public fielddefaultMessage
Message to return when the requested key is not found.
Public fieldmessages
Mapping of the key to the messages.
Top
Remarks

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.

Examples
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-"
  }
}
Examples
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-"
  }
}
Examples
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"
  }
}
See Also

Reference

KSPDev.GUIUtilsMessageLookupT