aboutsummaryrefslogtreecommitdiff
path: root/intl.c
blob: e0817b518bd3d58d93fbcbf993834ed78be1e063 (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
/*
 * ToscoIntl version 0.1
 * Copyright (c) 2002-2003 Leandro Pereira <leandro@linuxmag.com.br>
 * All rights reserved.
 *
 * This script is in the Tosco Public License. It may be copied and/or
 * modified, in whole or in part, provided that all copies and related
 * documentation includes the above copyright notice, this condition
 * and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *
 * (yes, the disclaimer is a copy from the BSD license, eat me!)
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "hardinfo.h"

#ifdef ENABLE_NLS

void intl_init(void)
{
	const gchar *by;
	
	g_print("Translation module registered.\n");

	by = intl_translate("translated-by", "Translation");
	if (strcmp(by, "translated-by")) {
		g_print("Translated by: %s\n", by);
	}
}

/*
 * GNU's gettext is cool and all... but hey, this is smaller :)
 */
const gchar *
intl_translate(const gchar * string, const gchar * source) __THROW
{
	FILE *file;
	gchar buffer[256], *keyname, *lang = NULL, *langenv = NULL;
	const gchar *retval, *langvars[] =
		 {"LANG", "LC_MESSAGES", "LC_ALL", NULL};
	gboolean found;
	struct stat st;
	gint i = 0;

	keyname = g_strdup_printf("[%s]", source);

	for (; langvars[i]; i++)
		if (getenv(langvars[i])) {
			langenv = getenv(langvars[i]);
			goto langenv_ok;
		}

	goto not_found;

     langenv_ok:
	lang = g_strconcat(INTL_PREFIX, langenv, ".lang", NULL);
	if (stat(lang, &st)) {
		lang = g_strconcat(INTL_PREFIX, "default.lang", NULL);
		if (stat(lang, &st)) {
		     not_found:
			retval = string;
			goto finished;
		} 
	}

	retval  = string;

	file = fopen(lang, "r");
	if (!file)
		goto finished;
	
	while (fgets(buffer, 256, file)) {
		const gchar *buf = buffer;
		
		buf = g_strstrip(buf);
		
		if (*buf == '[' && !found &&
			!strncmp(buf, keyname, strlen(keyname)))
				found = TRUE;
		if (found && !strncmp(buf, string, strlen(string)) &&
			*(buf + strlen(string)) == '=') {
				walk_until_inclusive('=');
				retval = g_strdup(buf);

				fclose(file);
	
				goto finished;
		}
	}
	fclose(file);
	
     finished:
     	g_free(keyname);
     	g_free(lang);

	return retval;
}

#endif