LocString.cs 3.2 KB

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