server/src/util/language.c

252 lines
6.1 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>
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
{
2011-03-07 08:02:35 +01: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
{
2011-03-07 08:02:35 +01: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
{
2011-03-07 08:02:35 +01:00
const char *tok = str;
2010-08-08 10:06:34 +02:00
while (*tok) {
char zText[32];
2011-03-07 08:02:35 +01:00
while (*tok && *tok != ',')
++tok;
strncpy(zText, str, tok - str);
zText[tok - str] = 0;
get_or_create_locale(zText);
2010-08-08 10:06:34 +02:00
if (*tok) {
str = ++tok;
}
}
}
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);
2011-03-07 08:02:35 +01:00
unsigned int id = hkey & (SMAXHASH - 1);
const struct locale_str *find;
2010-08-08 10:06:34 +02:00
assert(lang);
2011-03-07 08:02:35 +01:00
if (key == NULL || *key == 0)
return NULL;
2010-08-08 10:06:34 +02:00
find = lang->strings[id];
while (find) {
if (find->hashkey == hkey) {
2011-03-07 08:02:35 +01:00
if (find->nexthash == NULL) {
2010-08-08 10:06:34 +02:00
/* if this is the only entry with this hash, fine. */
2011-03-07 08:02:35 +01:00
assert(strcmp(key, find->key) == 0);
2010-08-08 10:06:34 +02:00
return find->str;
}
2011-03-07 08:02:35 +01:00
if (strcmp(key, find->key) == 0) {
2010-08-08 10:06:34 +02:00
return find->str;
}
}
find = find->nexthash;
}
return NULL;
}
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);
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
if (key != NULL) {
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 (*key == 0) return 0;
2010-08-08 10:06:34 +02:00
find = lang->strings[id];
while (find) {
if (find->hashkey == hkey) {
if (!find->nexthash) {
2010-08-08 10:06:34 +02:00
/* if this is the only entry with this hash, fine. */
2011-03-07 08:02:35 +01:00
assert(strcmp(key, find->key) == 0);
2010-08-08 10:06:34 +02:00
break;
}
2011-03-07 08:02:35 +01:00
if (strcmp(key, find->key) == 0)
break;
2010-08-08 10:06:34 +02:00
}
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);
2010-08-08 10:06:34 +02:00
}
return 0;
2010-08-08 10:06:34 +02:00
}
return find->str;
}
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01: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) {
2011-03-07 08:02:35 +01:00
lang = default_locale;
}
assert(lang);
2010-08-08 10:06:34 +02:00
find = lang->strings[id];
while (find) {
2011-03-07 08:02:35 +01:00
if (find->hashkey == hkey && strcmp(key, find->key) == 0)
break;
2010-08-08 10:06:34 +02:00
find = find->nexthash;
}
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);
2011-03-07 08:02:35 +01:00
} else {
if (strcmp(find->str, value) != 0) {
log_error("duplicate translation '%s' for key %s\n", value, key);
2010-08-08 10:06:34 +02:00
}
assert(!strcmp(find->str, value) || !"duplicate string for key");
}
}
2011-03-07 08:02:35 +01:00
const char *locale_name(const locale * lang)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
assert(lang);
return lang->name;
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;
}
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
{
2011-03-07 08:02:35 +01:00
static char zBuffer[128]; /* STATIC_RESULT: used for return, not across calls */
2010-08-08 10:06:34 +02:00
return mkname_buf(space, name, zBuffer);
}
2011-03-07 08:02:35 +01:00
locale *nextlocale(const struct locale * lang)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01: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);
2011-03-07 08:02:35 +01:00
assert(lang->index < MAXLOCALES
|| "you have to increase MAXLOCALES and recompile");
if (lang->index < MAXLOCALES) {
return lstrs[lang->index].tokens + index;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
return lstrs[0].tokens + index;
2010-08-08 10:06:34 +02:00
}
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;
}
}