LocString.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Handle to a localized string.
  7. /// </summary>
  8. public sealed class LocString : ScriptObject
  9. {
  10. /// <summary>
  11. /// Creates a new empty localized string.
  12. /// </summary>
  13. public LocString()
  14. {
  15. Internal_CreateInstance(this, "", 0);
  16. }
  17. /// <summary>
  18. /// Creates a new empty localized string.
  19. /// </summary>
  20. /// <param name="tableId">Unique identifier of the string table to retrieve the string from.</param>
  21. public LocString(int tableId)
  22. {
  23. Internal_CreateInstance(this, "", tableId);
  24. }
  25. /// <summary>
  26. /// Creates a new localized string with the specified identifier. If the identifier doesn't previously exist in
  27. /// the string table, identifier value will also be used for initializing the default language version of the string.
  28. /// </summary>
  29. /// <param name="identifier">String you can use for later referencing the localized string.</param>
  30. /// <param name="tableId">Unique identifier of the string table to retrieve the string from.</param>
  31. public LocString(string identifier, int tableId = 0)
  32. {
  33. Internal_CreateInstance(this, identifier, tableId);
  34. }
  35. /// <summary>
  36. /// Converts a normal string into a localized string by using the normal string as the identifier.
  37. /// </summary>
  38. /// <param name="identifier">Localized string identifier.</param>
  39. /// <returns>Localized string with the provided identifier, from the default string table.</returns>
  40. public static implicit operator LocString(string identifier)
  41. {
  42. return new LocString(identifier);
  43. }
  44. /// <summary>
  45. /// Retrieves localized text for the active language from the localized string.
  46. /// </summary>
  47. /// <param name="text">Localized string to retrieve the text from.</param>
  48. /// <returns>Translated text for the currently active language.</returns>
  49. public static explicit operator string(LocString text)
  50. {
  51. string value;
  52. Internal_GetValue(text.mCachedPtr, out value);
  53. return value;
  54. }
  55. /// <summary>
  56. /// Sets a value of a string parameter. Parameters are specified as bracketed values within the string itself
  57. /// (e.g. {0}, {1}) etc. Use ^ as an escape character.
  58. /// </summary>
  59. /// <param name="idx">Index of the parameter to set.</param>
  60. /// <param name="value">Value to insert at the parameter's position.</param>
  61. public void SetParameter(int idx, string value)
  62. {
  63. Internal_SetParameter(mCachedPtr, idx, value);
  64. }
  65. [MethodImpl(MethodImplOptions.InternalCall)]
  66. private static extern void Internal_CreateInstance(LocString instance, string identifier, int tableId);
  67. [MethodImpl(MethodImplOptions.InternalCall)]
  68. private static extern void Internal_SetParameter(IntPtr nativeInstance, int idx, string identifier);
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. private static extern void Internal_GetValue(IntPtr nativeInstance, out string value);
  71. }
  72. }