LocString.cs 3.6 KB

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