Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Open source/i18n/lang.cpp. After this line: "#include "i18n/lang.h""
- just near the start, add:
- //lang_ localization
- #include "core/fileObject.h"
- #include "core/util/str.h"
- #include "core/strings/unicode.h"
- 2. Replace the whole "bool LangFile::load(Stream *s) {...}" with:
- bool LangFile::load(Stream *s)
- {
- freeTable();
- while(s->getStatus() == Stream::Ok)
- {
- char buf[2048];
- s->readLongString(2048,buf);
- if (s->getStatus() == Stream::Ok)
- addString((const UTF8*)buf);
- }
- return true;
- }
- 3. Replace
- bool LangFile::save(Stream *s)
- {
- if(!isLoaded())
- return false;
- U32 i;
- for(i = 0;i < mStringTable.size();i++)
- {
- s->writeString((char*)mStringTable[i]);
- }
- return true;
- }
- With:
- bool LangFile::save(Stream *s)
- {
- if(!isLoaded())
- return false;
- U32 i;
- for(i = 0;i < mStringTable.size();i++)
- {
- //s->writeString((char*)mStringTable[i]);
- s->writeLongString(2048, (char*)mStringTable[i]); //irei1as_ lang
- }
- return true;
- }
- 4. At the end of this file (source/i18n/lang.cpp) finally add:
- //lang_ localization
- ConsoleFunction( CompileLanguage, void, 2, 3, "(string inputFile, [bool createMap]) Compiles a LSO language file."
- " if createIndex is true, will also create languageMap.cs with"
- " the global variables for each string index."
- " The input file must follow this example layout:"
- " TXT_HELLO_WORLD = Hello world in english!" )
- {
- UTF8 scriptFilenameBuffer[1024];
- Con::expandScriptFilename((char*)scriptFilenameBuffer, sizeof(scriptFilenameBuffer), argv[1]);
- if(!Torque::FS::IsFile(scriptFilenameBuffer))
- {
- Con::errorf("CompileLanguage - file %s not found", scriptFilenameBuffer);
- return;
- }
- FileObject file;
- if (!file.readMemory(scriptFilenameBuffer))
- {
- Con::errorf("CompileLanguage - couldn't read file %s", scriptFilenameBuffer);
- return;
- }
- bool createMap = argc > 2 ? dAtob(argv[2]) : false;
- FileStream *mapStream = NULL;
- if (createMap)
- {
- Torque::Path mapPath = scriptFilenameBuffer;
- mapPath.setFileName("languageMap");
- mapPath.setExtension("cs");
- if((mapStream = FileStream::createAndOpen( mapPath, Torque::FS::File::Write )) == NULL)
- Con::errorf("CompileLanguage - failed creating languageMap.cs");
- }
- LangFile langFile;
- const U8* inLine = NULL;
- const char* separatorStr = " = ";
- S32 stringId = 0;
- while( (inLine = file.readLine())[0] != 0 )
- {
- char* line;
- chompUTF8BOM( (const char *)inLine, &line );
- char* div = dStrstr(line, separatorStr);
- if (div == NULL)
- {
- Con::errorf("Separator %s not found in line: %s", separatorStr, line);
- Con::errorf("Could not determine string name ID");
- continue;
- }
- *div = 0;
- char* text = div + dStrlen(separatorStr);
- langFile.addString((const UTF8*)text);
- if (mapStream)
- {
- String mapLine = String::ToString("$%s = %i;", line, stringId);
- mapStream->writeLine((const U8*)mapLine.c_str());
- String commentLine = String::ToString("// %s", text);
- mapStream->writeLine((const U8*)commentLine.c_str());
- }
- stringId++;
- }
- Torque::Path lsoPath = scriptFilenameBuffer;
- lsoPath.setExtension("lso");
- langFile.save(lsoPath.getFullPath());
- if (mapStream)
- delete mapStream;
- }
- //end of the C++ changes for localization
- 5. Now the Torquescript changes.
- Create a "english.txt" in game/scripts and make its text:
- txt_hello = Hello world
- txt_language = This is the english language
- txt_last = This is the last string
- You may want it to be UTF-8 without BOM
- Add also a "espanyol.txt" with its contents except the "txt_..." translated like:
- txt_hello = Hola Mundo
- txt_language = Esto es español
- txt_last = Esta es la última línea
- 6. At the end of game/scripts/main.cs add:
- new LangTable(mainLangTable); //lang_ localization
- $I18N::default = mainLangTable.getId();
- // This should only be done when the .txt is newer than the .lso
- compileLanguage("./english.txt", 1); //that 1 exists to create languageMap.cs
- compileLanguage("./espanyol.txt", 0);
- mainLangTable.addLanguage("./english.lso", "English"); //language #0
- mainLangTable.addLanguage("./espanyol.lso", "Espanyol"); //language #1
- mainLangTable.setCurrentLanguage(0);
- exec("./languageMap.cs"); //this file is created by a previous compileLanguage
- function switchLanguage(%language) //use here the #n as it's the order of inclusion
- {
- mainLangTable.setCurrentLanguage(%language);
- Canvas.setContent(Canvas.getContent());
- }
- 7. Finally, in game/main.cs inside "function createCanvas(%windowTitle)"
- before "return true;" add:
- Canvas.langTableMod = "default";
- Note: how to use:
- The order of addition in game/scripts/main.cs makes its number (first is zero, 0).
- Use switchLanguage(%language) where %language is that number to change the languages.
- For GUI editor just change the text_id with how the variable is called (like txt_hello).
- For the script do "mainlangtable.getstring($txt_hello)".
- For C++: get the LangTable and call getString() like the GUI does. It may be convoluted to program.
- Probably just using something similar to: UTF8 * GuiControl::getGUIString(S32 id)
Advertisement
Add Comment
Please, Sign In to add comment