summaryrefslogtreecommitdiff
path: root/src/library/gettext.c
blob: b964fde5c19eb913c6ea79cafc9fe2d587df0542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Very minimal (and stupid) implementation of gettext, with a fixed lookup
 * table.
 *
 * This library ONLY handles gettext(), and that only for the basic form (it
 * translates strings to other strings with no other modification, so %2$d
 * style constructs are not dealt with). The setlocale(), bindtextdomain(),
 * and textdomain() functions are ignored.
 *
 * To use this library, create a function that, given a language string,
 * returns a struct msg_table_s[] of msgid and msgstr pairs, with the end
 * of the table being marked by a NULL msgid. The po2table.sh script will do
 * this.
 *
 * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0.
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef HAVE_GETTEXT

struct msgtable_s {
	char *msgid;
	char *msgstr;
};

#if ENABLE_NLS
struct msgtable_s *minigettext__gettable(char *);
#else				/* ENABLE_NLS */
struct msgtable_s *minigettext__gettable(char *a)
{
	return NULL;
}
#endif				/* ENABLE_NLS */

char *minisetlocale(char *a, char *b)
{
	return NULL;
}


char *minibindtextdomain(char *a, char *b)
{
	return NULL;
}


char *minitextdomain(char *a)
{
	return NULL;
}


char *minigettext(char *msgid)
{
	static struct msgtable_s *table = NULL;
	static int tried_lang = 0;
	char *lang;
	int i;

	if (msgid == NULL)
		return msgid;

	if (tried_lang == 0) {
		lang = getenv("LANGUAGE");  /* RATS: ignore */
		if (lang)
			table = minigettext__gettable(lang);

		if (table == NULL) {
			lang = getenv("LANG");	/* RATS: ignore */
			if (lang)
				table = minigettext__gettable(lang);
		}

		if (table == NULL) {
			lang = getenv("LC_ALL");	/* RATS: ignore */
			if (lang)
				table = minigettext__gettable(lang);
		}

		if (table == NULL) {
			lang = getenv("LC_MESSAGES");	/* RATS: ignore */
			if (lang)
				table = minigettext__gettable(lang);
		}

		tried_lang = 1;
	}

	if (table == NULL)
		return msgid;

	for (i = 0; table[i].msgid; i++) {
		if (strcmp(table[i].msgid, msgid) == 0) {
			if (table[i].msgstr == 0)
				return msgid;
			if (table[i].msgstr[0] == 0)
				return msgid;
			return table[i].msgstr;
		}
	}

	return msgid;
}

#endif				/* HAVE_GETTEXT */

/* EOF */