But first of all, I'll explain what I've thought, and more specially what I've done:
To make that system, I'm going to base my code to the cvar system. We don't want to be disturbed later if someone tries to modify the text. So, After launching the game, we will allow to make modifications to the texts. Then, it will run a small function opening a file, replacing the words, and closes the ability to modify the translations.
Anyway, I also imagine the worst, like someone writing binds through this file when this shouldn't be allowed.
Bref...
____________________________________________
1) The translation system is simple, and is greatly based on cvar's system.
Code: Select all
typedef struct language_s {
char *text; // name of the basis word
char *defaulttext; // default meaning
char *translation; // translated word (if any)
struct language_s *nextword;
} language_t;
extern language_t *game_nextword;
3) After making my Init, I'll make a switch of the value obtained, and it'll run a function to open a .lng (language) file with the name of the language.
==> PROBLEM IS, IT DOESN'T OPEN ANY OF MY FILES.
Code: Select all
/*
===================
Cmd_OpenLanguageFile
Opens a language file.
===================
*/
void Cmd_OpenLanguageFile (char *lang)
{
char *f;
char cfgname[128];
int mark;
mark = Hunk_LowMark ();
// Ch0wW - All .lng files are now bound to the resource folder.
sprintf(cfgname,"resource/%s", lang);
Con_Printf(cfgname);
COM_DefaultExtension (cfgname, ".lng");
f = (char *)COM_LoadHunkFile (cfgname);
//--- 09/02/2012
if (!f)
{
Con_Printf ("No Language file found. Reverting to default.");
return;
}
Cbuf_InsertText (f);
Hunk_FreeToLowMark (mark);
Con_Printf("loaded %s language file done\n", lang);
}
(My idea : send the text name, and the variable. If there isn't any variable (like in TL_Add() ), just set the default value. Else, run the translated value.)
Code: Select all
/*
============
TL_FindText
============
*/
language_t *TL_FindText (char *tl_name)
{
language_t *txt;
for (txt=game_nextword ; txt ; txt=txt->nextword)
if (!Q_strcmp (tl_name, txt->text))
return txt;
return NULL;
}
/*
============
TL_Translate
Translates the text according to the language file.
============
*/
void TL_Translate (char *text, char *value)
{
language_t *tl;
tl = TL_FindText (text);
if (!tl) return;
//Z_Free (tl->translation); // free the old value string
//tl->translation = Z_Malloc (Q_strlen(value)+1);
tl->translation = value;
}
/*
===========
TL_Cmd
Checks if my text can be translated through console inputs.
===========
*/
qboolean TL_Cmd (void)
{
language_t *tl;
tl = TL_FindText (Cmd_Argv(0));
if (!tl) return false;
if (Cmd_Argc() == 1)return false;
TL_Translate (tl->text, Cmd_Argv(1));
return true;
}
/*
============
TL_Add
Like as Cvar_RegisterVariable
============
*/
void TL_Add (language_t *tl_text)
{
char value[512];
qboolean changed;
// link the variable in
tl_text->nextword = game_nextword;
game_nextword = tl_text;
// copy the value off, because future sets will Z_Free it
strcpy (value, tl_text->defaulttext);
tl_text->translation = Z_Malloc (1);
TL_Translate(tl_text->text, tl_text->defaulttext);5) Well, Cvar-related, add a condition :
Code: Select all
// check cvars && translation commands
if (!Cvar_Command () && !TL_Cmd () && cl_warncmd.value)
Con_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0));yet, it does work, but only the steps 3 & 4 are slowing me down.
I had some ideas of how to fix that like adding a temporary char in language_t , but I just don't get it.
Or, if someone gets a better idea, please tell it.
Thanks in advance!
(btw, yeah, I thought of this while watching FTE code, just to get some fixes in QuakeC [which are hard to notice