localized_npc.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===== rAthena Script =======================================
  2. //= Sample: Localized NPC
  3. //===== By: ==================================================
  4. //= rAthena Dev Team
  5. //===== Last Updated: ========================================
  6. //= 20140208
  7. //===== Description: =========================================
  8. //= Example of a localized NPC.
  9. //=
  10. //= There are many ways to do it, this is just one option.
  11. //= The player has a global account variable ##_langid_ that
  12. //= identifies the it's language.
  13. //=
  14. //= The default language should always have langid 0.
  15. //= When a message isn't found for the player's langid
  16. //= (strlen = 0), the message from langid 0 is used instead.
  17. //=
  18. //= Each message is identified by a string that must only
  19. //= contain valid variable name characters.
  20. //=
  21. //= void setlang(int langid)
  22. //= - sets the player's language
  23. //= int getlang(void)
  24. //= - returns the player's language
  25. //= void setmes2(string name,int langid,string text)
  26. //= - sets the localized text for name
  27. //= string getmes2(string name,int langid)
  28. //= - returns the localized text of name
  29. //= void mes2(string name)
  30. //= - displays the localized text of name
  31. //=
  32. //===== Additional Comments: =================================
  33. //= To use this globally, just put the functions in Global_Functions.txt
  34. //============================================================
  35. //////////////////////////////////////////////////////////////
  36. /// Sets the language of the player account.
  37. /// @param langid Languange identifier (0 for default)
  38. function script setlang {
  39. ##_langid_ = getarg(0);
  40. return;
  41. }
  42. //////////////////////////////////////////////////////////////
  43. /// Returns the language identifier of the player
  44. function script getlang {
  45. return ##_langid_;
  46. }
  47. //////////////////////////////////////////////////////////////
  48. /// Sets a localized text entry.
  49. /// Does not need a RID attached.
  50. /// @param name Message identifier
  51. /// @param langid Language identifier (0 for default)
  52. /// @param text Text message
  53. function script setmes2 {
  54. .@mes2_name$ = getarg(0);
  55. .@mes2_langid = getarg(1);
  56. .@mes2_text$ = getarg(2);
  57. .@mes2_var$ = "$@__"+ .@mes2_name$ +"_"+ .@mes2_langid +"$";
  58. //debugmes "setmes2 \""+ .@mes2_var$ +"\", \""+ .@mes2_text$ +"\";";
  59. // set the localized text
  60. setd .@mes2_var$, .@mes2_text$;
  61. return;
  62. }
  63. //////////////////////////////////////////////////////////////
  64. /// Sets a localized text entry.
  65. /// Does not need a RID attached.
  66. /// @param name Message identifier
  67. /// @param langid Language identifier (0 for default)
  68. /// @return Text message
  69. function script getmes2 {
  70. .@mes2_name$ = getarg(0);
  71. .@mes2_langid = getarg(1);
  72. .@mes2_var$ = "$@__"+ .@mes2_name$ +"_"+ .@mes2_langid +"$";
  73. .@mes2_text$ = getd(.@mes2_var$);
  74. //debugmes "getmes2(\""+ .@mes2_var$ +"\")=\""+ .@mes2_text$ +"\"";
  75. return .@mes2_text$;
  76. }
  77. //////////////////////////////////////////////////////////////
  78. /// mes for localized text.
  79. /// index should be a unique string, made up only of characters
  80. /// that are valis as a variable name
  81. /// @param index Message identifier
  82. function script mes2 {
  83. .@mes2_index$ = getarg(0);
  84. if( getstrlen(.@mes2_index$) == 0 )
  85. return; // invalid index
  86. // print localized text
  87. .@mes2_text$ = callfunc("getmes2",.@mes2_index$,##_langid_);
  88. if( getstrlen(.@mes2_text$) == 0 ) {
  89. if( ##_langid_ != 0 ) {
  90. // revert to default language
  91. .@mes2_text$ = callfunc("getmes2",.@mes2_index$,0);
  92. if( getstrlen(.@mes2_text$) != 0 )
  93. mes .@mes2_text$; // default text
  94. }
  95. } else
  96. mes .@mes2_text$; // localized text
  97. return;
  98. }
  99. //////////////////////////////////////////////////////////////
  100. /// Sample localized NPC
  101. prontera,155,183,4 script LocalizedNPC 705,{
  102. // Get text for specific languages
  103. .@menu1$ = callfunc("getmes2","LNPC_lang",0);
  104. .@menu2$ = callfunc("getmes2","LNPC_lang",1);
  105. do {
  106. // get text that fallbacks to language 0
  107. callfunc "mes2", "LNPC_name";
  108. // localized mes
  109. callfunc "mes2", "LNPC_lang";
  110. callfunc "mes2", "LNPC_text";
  111. next;
  112. switch(select(.@menu1$,.@menu2$,"Cancel"))
  113. {
  114. case 1:
  115. case 2:
  116. // Set player language
  117. callfunc "setlang",@menu-1;
  118. break;
  119. }
  120. } while( @menu != 3 );
  121. close;
  122. end;
  123. OnInterIfInitOnce:
  124. // Load the localized text.
  125. // This can be anywhere, as long as it's executed before the coresponding getmes2/mes2 calls
  126. // 0 - English (default)
  127. // 1 - Portuguese
  128. callfunc "setmes2", "LNPC_name", 0, "[LocalizedNPC]";
  129. callfunc "setmes2", "LNPC_lang", 0, "EN";
  130. callfunc "setmes2", "LNPC_lang", 1, "PT";
  131. callfunc "setmes2", "LNPC_text", 0, "Something in english";
  132. callfunc "setmes2", "LNPC_text", 1, "Algo em português";
  133. end;
  134. }