2
0

LocString.cs 3.6 KB

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