about summary refs log tree commit diff
path: root/src/game/localization.cpp
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2009-06-13 16:54:04 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2009-06-13 16:54:04 +0000
commit6d9ccee95dd99fecda3a6ba62c2768b4d39f69e5 (patch)
treeed06c372126df9d843b7d72c0263982923e1ccba /src/game/localization.cpp
parentc2f8d0e07ac19d4a876444d045a4f199f4d7c4d7 (diff)
downloadzcatch-6d9ccee95dd99fecda3a6ba62c2768b4d39f69e5.tar.gz
zcatch-6d9ccee95dd99fecda3a6ba62c2768b4d39f69e5.zip
base for the localization system
Diffstat (limited to 'src/game/localization.cpp')
-rw-r--r--src/game/localization.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/game/localization.cpp b/src/game/localization.cpp
new file mode 100644
index 00000000..cfc659f5
--- /dev/null
+++ b/src/game/localization.cpp
@@ -0,0 +1,56 @@
+
+#include "localization.hpp"
+
+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));
+	//dbg_msg("", "no localization for '%s'", 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);
+	
+	current_version++;
+}
+
+const char *LOCALIZATIONDATABASE::find_string(unsigned hash)
+{
+	array<STRING>::range r = ::find(strings.all(), hash);
+	if(r.empty())
+		return 0;
+	return r.front().replacement;
+}