server/src/util/language.c

265 lines
6.9 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
2010-08-08 10:06:34 +02:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include "language.h"
#include "language_struct.h"
#include "log.h"
#include "goodies.h"
#include "umlaut.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/** importing **/
2011-03-07 08:02:35 +01:00
locale *default_locale;
locale *locales;
unsigned int locale_index(const locale * lang)
2010-08-08 10:06:34 +02:00
{
assert(lang);
return lang->index;
2010-08-08 10:06:34 +02:00
}
locale *get_locale(const char *name)
2010-08-08 10:06:34 +02:00
{
unsigned int hkey = hashstring(name);
locale *l = locales;
while (l && l->hashkey != hkey)
l = l->next;
return l;
2010-08-08 10:06:34 +02:00
}
static unsigned int nextlocaleindex = 0;
2010-08-08 10:06:34 +02:00
locale *get_or_create_locale(const char *name)
2010-08-08 10:06:34 +02:00
{
locale *l;
unsigned int hkey = hashstring(name);
locale **lp = &locales;
if (!locales) {
nextlocaleindex = 0;
}
else {
while (*lp && (*lp)->hashkey != hkey) lp = &(*lp)->next;
if (*lp) {
return *lp;
}
}
*lp = l = (locale *)calloc(sizeof(locale), 1);
l->hashkey = hkey;
l->name = _strdup(name);
l->index = nextlocaleindex++;
assert(nextlocaleindex <= MAXLOCALES);
if (default_locale == NULL) default_locale = l;
return l;
2010-08-08 10:06:34 +02:00
}
/** creates a list of locales
* This function takes a comma-delimited list of locale-names and creates
* the locales using the make_locale function (useful for ini-files).
* For maximum performance, locales should be created in order of popularity.
*/
2011-03-07 08:02:35 +01:00
void make_locales(const char *str)
2010-08-08 10:06:34 +02:00
{
const char *tok = str;
while (*tok) {
char zText[32];
while (*tok && *tok != ',')
++tok;
strncpy(zText, str, tok - str);
zText[tok - str] = 0;
get_or_create_locale(zText);
if (*tok) {
str = ++tok;
}
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
const char *locale_getstring(const locale * lang, const char *key)
2010-08-08 10:06:34 +02:00
{
unsigned int hkey = hashstring(key);
unsigned int id = hkey & (SMAXHASH - 1);
const struct locale_str *find;
2010-08-08 10:06:34 +02:00
assert(lang);
if (key == NULL || *key == 0)
return NULL;
find = lang->strings[id];
while (find) {
if (find->hashkey == hkey) {
if (find->nexthash == NULL) {
/* if this is the only entry with this hash, fine. */
assert(strcmp(key, find->key) == 0);
return find->str;
}
if (strcmp(key, find->key) == 0) {
return find->str;
}
}
find = find->nexthash;
2010-08-08 10:06:34 +02:00
}
return NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
const char *locale_string(const locale * lang, const char *key)
2010-08-08 10:06:34 +02:00
{
assert(lang);
assert(key);
if (key != NULL) {
unsigned int hkey = hashstring(key);
unsigned int id = hkey & (SMAXHASH - 1);
struct locale_str *find;
if (*key == 0) return 0;
find = lang->strings[id];
while (find) {
if (find->hashkey == hkey) {
if (!find->nexthash) {
/* if this is the only entry with this hash, fine. */
assert(strcmp(key, find->key) == 0);
break;
}
if (strcmp(key, find->key) == 0)
break;
}
find = find->nexthash;
}
if (!find) {
log_warning("missing translation for \"%s\" in locale %s\n", key, lang->name);
if (lang->fallback) {
return locale_string(lang->fallback, key);
}
return 0;
}
return find->str;
}
return 0;
}
2010-08-08 10:06:34 +02:00
void locale_setstring(locale * lang, const char *key, const char *value)
{
2010-08-08 10:06:34 +02:00
unsigned int hkey = hashstring(key);
2011-03-07 08:02:35 +01:00
unsigned int id = hkey & (SMAXHASH - 1);
struct locale_str *find;
if (!lang) {
lang = default_locale;
}
assert(lang);
2010-08-08 10:06:34 +02:00
find = lang->strings[id];
while (find) {
if (find->hashkey == hkey && strcmp(key, find->key) == 0)
break;
find = find->nexthash;
2010-08-08 10:06:34 +02:00
}
if (!find) {
find = calloc(1, sizeof(struct locale_str));
find->nexthash = lang->strings[id];
lang->strings[id] = find;
find->hashkey = hkey;
find->key = _strdup(key);
find->str = _strdup(value);
2010-08-08 10:06:34 +02:00
}
else {
if (strcmp(find->str, value) != 0) {
log_warning("multiple translations for key %s\n", key);
}
free(find->str);
find->str = _strdup(value);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
const char *locale_name(const locale * lang)
2010-08-08 10:06:34 +02:00
{
return lang ? lang->name : "(null)";
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
char *mkname_buf(const char *space, const char *name, char *buffer)
2010-08-08 10:06:34 +02:00
{
if (space && *space) {
sprintf(buffer, "%s::%s", space, name);
}
else {
strcpy(buffer, name);
}
return buffer;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
const char *mkname(const char *space, const char *name)
2010-08-08 10:06:34 +02:00
{
static char zBuffer[128]; // FIXME: static return value
return mkname_buf(space, name, zBuffer);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
locale *nextlocale(const struct locale * lang)
2010-08-08 10:06:34 +02:00
{
return lang->next;
2010-08-08 10:06:34 +02:00
}
typedef struct lstr {
void * tokens[UT_MAX];
2010-08-08 10:06:34 +02:00
} lstr;
static lstr lstrs[MAXLOCALES];
void ** get_translations(const struct locale *lang, int index)
2010-08-08 10:06:34 +02:00
{
assert(lang);
assert(lang->index < MAXLOCALES
|| "you have to increase MAXLOCALES and recompile");
if (lang->index < MAXLOCALES) {
return lstrs[lang->index].tokens + index;
}
return lstrs[0].tokens + index;
2010-08-08 10:06:34 +02:00
}
void *get_translation(const struct locale *lang, const char *str, int index) {
void **tokens = get_translations(lang, index);
variant var;
if (findtoken(*tokens, str, &var) == E_TOK_SUCCESS) {
return var.v;
}
return NULL;
}
void free_locales(void)
{
while (locales) {
int i;
locale * next = locales->next;
for (i = 0; i != SMAXHASH; ++i) {
while (locales->strings[i]) {
struct locale_str * strings = locales->strings[i];
free(strings->key);
free(strings->str);
locales->strings[i] = strings->nexthash;
free(strings);
}
}
free(locales);
locales = next;
}
memset(lstrs, 0, sizeof(lstrs)); // TODO: does this data need to be free'd?
}