Guest User

T3D localization

a guest
May 7th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. 1. Open source/i18n/lang.cpp. After this line: "#include "i18n/lang.h""
  2. just near the start, add:
  3.  
  4. //lang_ localization
  5. #include "core/fileObject.h"
  6. #include "core/util/str.h"
  7. #include "core/strings/unicode.h"
  8.  
  9. 2. Replace the whole "bool LangFile::load(Stream *s) {...}" with:
  10.  
  11. bool LangFile::load(Stream *s)
  12. {
  13. freeTable();
  14.  
  15. while(s->getStatus() == Stream::Ok)
  16. {
  17. char buf[2048];
  18. s->readLongString(2048,buf);
  19. if (s->getStatus() == Stream::Ok)
  20. addString((const UTF8*)buf);
  21. }
  22. return true;
  23. }
  24.  
  25. 3. Replace
  26.  
  27. bool LangFile::save(Stream *s)
  28. {
  29. if(!isLoaded())
  30. return false;
  31.  
  32. U32 i;
  33. for(i = 0;i < mStringTable.size();i++)
  34. {
  35. s->writeString((char*)mStringTable[i]);
  36. }
  37. return true;
  38. }
  39.  
  40. With:
  41.  
  42. bool LangFile::save(Stream *s)
  43. {
  44. if(!isLoaded())
  45. return false;
  46.  
  47. U32 i;
  48. for(i = 0;i < mStringTable.size();i++)
  49. {
  50. //s->writeString((char*)mStringTable[i]);
  51. s->writeLongString(2048, (char*)mStringTable[i]); //irei1as_ lang
  52. }
  53. return true;
  54. }
  55.  
  56.  
  57. 4. At the end of this file (source/i18n/lang.cpp) finally add:
  58.  
  59.  
  60. //lang_ localization
  61. ConsoleFunction( CompileLanguage, void, 2, 3, "(string inputFile, [bool createMap]) Compiles a LSO language file."
  62. " if createIndex is true, will also create languageMap.cs with"
  63. " the global variables for each string index."
  64. " The input file must follow this example layout:"
  65. " TXT_HELLO_WORLD = Hello world in english!" )
  66. {
  67. UTF8 scriptFilenameBuffer[1024];
  68. Con::expandScriptFilename((char*)scriptFilenameBuffer, sizeof(scriptFilenameBuffer), argv[1]);
  69.  
  70. if(!Torque::FS::IsFile(scriptFilenameBuffer))
  71. {
  72. Con::errorf("CompileLanguage - file %s not found", scriptFilenameBuffer);
  73. return;
  74. }
  75.  
  76. FileObject file;
  77. if (!file.readMemory(scriptFilenameBuffer))
  78. {
  79. Con::errorf("CompileLanguage - couldn't read file %s", scriptFilenameBuffer);
  80. return;
  81. }
  82.  
  83. bool createMap = argc > 2 ? dAtob(argv[2]) : false;
  84. FileStream *mapStream = NULL;
  85. if (createMap)
  86. {
  87. Torque::Path mapPath = scriptFilenameBuffer;
  88. mapPath.setFileName("languageMap");
  89. mapPath.setExtension("cs");
  90. if((mapStream = FileStream::createAndOpen( mapPath, Torque::FS::File::Write )) == NULL)
  91. Con::errorf("CompileLanguage - failed creating languageMap.cs");
  92. }
  93.  
  94. LangFile langFile;
  95. const U8* inLine = NULL;
  96. const char* separatorStr = " = ";
  97. S32 stringId = 0;
  98. while( (inLine = file.readLine())[0] != 0 )
  99. {
  100. char* line;
  101. chompUTF8BOM( (const char *)inLine, &line );
  102. char* div = dStrstr(line, separatorStr);
  103. if (div == NULL)
  104. {
  105. Con::errorf("Separator %s not found in line: %s", separatorStr, line);
  106. Con::errorf("Could not determine string name ID");
  107. continue;
  108. }
  109. *div = 0;
  110. char* text = div + dStrlen(separatorStr);
  111.  
  112. langFile.addString((const UTF8*)text);
  113.  
  114. if (mapStream)
  115. {
  116. String mapLine = String::ToString("$%s = %i;", line, stringId);
  117. mapStream->writeLine((const U8*)mapLine.c_str());
  118. String commentLine = String::ToString("// %s", text);
  119. mapStream->writeLine((const U8*)commentLine.c_str());
  120. }
  121.  
  122. stringId++;
  123. }
  124.  
  125. Torque::Path lsoPath = scriptFilenameBuffer;
  126. lsoPath.setExtension("lso");
  127. langFile.save(lsoPath.getFullPath());
  128.  
  129. if (mapStream)
  130. delete mapStream;
  131. }
  132. //end of the C++ changes for localization
  133.  
  134.  
  135.  
  136. 5. Now the Torquescript changes.
  137. Create a "english.txt" in game/scripts and make its text:
  138.  
  139.  
  140. txt_hello = Hello world
  141. txt_language = This is the english language
  142. txt_last = This is the last string
  143.  
  144.  
  145. You may want it to be UTF-8 without BOM
  146.  
  147. Add also a "espanyol.txt" with its contents except the "txt_..." translated like:
  148.  
  149.  
  150. txt_hello = Hola Mundo
  151. txt_language = Esto es español
  152. txt_last = Esta es la última línea
  153.  
  154.  
  155. 6. At the end of game/scripts/main.cs add:
  156.  
  157.  
  158. new LangTable(mainLangTable); //lang_ localization
  159. $I18N::default = mainLangTable.getId();
  160.  
  161. // This should only be done when the .txt is newer than the .lso
  162. compileLanguage("./english.txt", 1); //that 1 exists to create languageMap.cs
  163. compileLanguage("./espanyol.txt", 0);
  164.  
  165. mainLangTable.addLanguage("./english.lso", "English"); //language #0
  166. mainLangTable.addLanguage("./espanyol.lso", "Espanyol"); //language #1
  167.  
  168. mainLangTable.setCurrentLanguage(0);
  169. exec("./languageMap.cs"); //this file is created by a previous compileLanguage
  170.  
  171. function switchLanguage(%language) //use here the #n as it's the order of inclusion
  172. {
  173. mainLangTable.setCurrentLanguage(%language);
  174. Canvas.setContent(Canvas.getContent());
  175. }
  176.  
  177.  
  178. 7. Finally, in game/main.cs inside "function createCanvas(%windowTitle)"
  179. before "return true;" add:
  180.  
  181. Canvas.langTableMod = "default";
  182.  
  183.  
  184. Note: how to use:
  185. The order of addition in game/scripts/main.cs makes its number (first is zero, 0).
  186. Use switchLanguage(%language) where %language is that number to change the languages.
  187.  
  188. For GUI editor just change the text_id with how the variable is called (like txt_hello).
  189. For the script do "mainlangtable.getstring($txt_hello)".
  190. For C++: get the LangTable and call getString() like the GUI does. It may be convoluted to program.
  191. Probably just using something similar to: UTF8 * GuiControl::getGUIString(S32 id)
Advertisement
Add Comment
Please, Sign In to add comment