2009-06-13 16:54:04 +00:00
|
|
|
|
|
|
|
#include "localization.hpp"
|
2009-06-15 07:34:25 +00:00
|
|
|
#include <base/tl/algorithm.hpp>
|
2009-06-13 16:54:04 +00:00
|
|
|
|
2009-06-13 17:18:06 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <engine/e_linereader.h>
|
|
|
|
}
|
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
static unsigned str_hash(const char *str)
|
|
|
|
{
|
|
|
|
unsigned hash = 5381;
|
|
|
|
for(; *str; str++)
|
|
|
|
hash = ((hash << 5) + hash) + (*str); /* hash * 33 + c */
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *localize(const char *str)
|
|
|
|
{
|
|
|
|
const char *new_str = localization.find_string(str_hash(str));
|
|
|
|
return new_str ? new_str : str;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOC_CONSTSTRING::LOC_CONSTSTRING(const char *str)
|
|
|
|
{
|
|
|
|
default_str = str;
|
|
|
|
hash = str_hash(default_str);
|
|
|
|
version = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOC_CONSTSTRING::reload()
|
|
|
|
{
|
|
|
|
version = localization.version();
|
|
|
|
const char *new_str = localization.find_string(hash);
|
|
|
|
current_str = new_str;
|
|
|
|
if(!current_str)
|
|
|
|
current_str = default_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCALIZATIONDATABASE::LOCALIZATIONDATABASE()
|
|
|
|
{
|
|
|
|
current_version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOCALIZATIONDATABASE::add_string(const char *org_str, const char *new_str)
|
|
|
|
{
|
|
|
|
STRING s;
|
|
|
|
s.hash = str_hash(org_str);
|
|
|
|
s.replacement = new_str;
|
|
|
|
strings.add(s);
|
2009-06-13 17:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LOCALIZATIONDATABASE::load(const char *filename)
|
|
|
|
{
|
2009-06-15 08:15:53 +00:00
|
|
|
// empty string means unload
|
|
|
|
if(filename[0] == 0)
|
|
|
|
{
|
|
|
|
strings.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-13 17:18:06 +00:00
|
|
|
LINEREADER lr;
|
|
|
|
IOHANDLE io = io_open(filename, IOFLAG_READ);
|
|
|
|
if(!io)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dbg_msg("localization", "loaded '%s'", filename);
|
|
|
|
strings.clear();
|
|
|
|
|
|
|
|
linereader_init(&lr, io);
|
|
|
|
char *line;
|
|
|
|
while((line = linereader_get(&lr)))
|
|
|
|
{
|
|
|
|
if(!str_length(line))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(line[0] == '#') // skip comments
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char *replacement = linereader_get(&lr);
|
|
|
|
if(!replacement)
|
|
|
|
{
|
|
|
|
dbg_msg("", "unexpected end of file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(replacement[0] != '=' || replacement[1] != '=' || replacement[2] != ' ')
|
|
|
|
{
|
|
|
|
dbg_msg("", "malform replacement line for '%s'", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
replacement += 3;
|
|
|
|
localization.add_string(line, replacement);
|
|
|
|
}
|
2009-06-15 06:45:44 +00:00
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
current_version++;
|
2009-06-13 17:18:06 +00:00
|
|
|
return true;
|
2009-06-13 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LOCALIZATIONDATABASE::find_string(unsigned hash)
|
|
|
|
{
|
2009-06-15 06:45:44 +00:00
|
|
|
STRING s;
|
|
|
|
s.hash = hash;
|
|
|
|
sorted_array<STRING>::range r = ::find_binary(strings.all(), s);
|
2009-06-13 16:54:04 +00:00
|
|
|
if(r.empty())
|
|
|
|
return 0;
|
|
|
|
return r.front().replacement;
|
|
|
|
}
|
2009-06-15 06:45:44 +00:00
|
|
|
|
|
|
|
LOCALIZATIONDATABASE localization;
|