summaryrefslogtreecommitdiff
path: root/src/libtracker-common/tracker-locale.c
blob: 0d2af23cb04f5f8357d3d0239bdda99ca434f5d8 (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
/*
 * Copyright (C) 2010 Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <locale.h>
#include <string.h>

#include <glib.h>

#include "tracker-locale.h"

static const gchar *locale_names[TRACKER_LOCALE_LAST] = {
	"LANG",
	"LC_TIME",
	"LC_COLLATE",
	"LC_NUMERIC",
	"LC_MONETARY"
};

static GRecMutex locales_mutex;

static const gchar *
tracker_locale_get_unlocked (TrackerLocaleID id)
{
	const gchar *env_locale = NULL;

	switch (id) {
	case TRACKER_LOCALE_LANGUAGE:
		env_locale = g_getenv ("LANG");
		break;
	case TRACKER_LOCALE_TIME:
		env_locale = setlocale (LC_TIME, NULL);
		break;
	case TRACKER_LOCALE_COLLATE:
		env_locale = setlocale (LC_COLLATE, NULL);
		break;
	case TRACKER_LOCALE_NUMERIC:
		env_locale = setlocale (LC_NUMERIC, NULL);
		break;
	case TRACKER_LOCALE_MONETARY:
		env_locale = setlocale (LC_MONETARY, NULL);
		break;
	default:
		g_assert_not_reached ();
		break;
	}

	return env_locale;
}

void
tracker_locale_sanity_check (void)
{
	guint i;

	g_rec_mutex_lock (&locales_mutex);

	for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
		const gchar *env_locale = NULL;

		env_locale = tracker_locale_get_unlocked (i);

		if (!env_locale) {
			g_warning ("Locale '%s' is not set, defaulting to C locale", locale_names[i]);
		}
	}

	g_rec_mutex_unlock (&locales_mutex);
}

gchar *
tracker_locale_get (TrackerLocaleID id)
{
	const gchar *env_locale = NULL;
	gchar *locale;

	g_rec_mutex_lock (&locales_mutex);

	env_locale = tracker_locale_get_unlocked (id);

	/* Always return a duplicated string, as the locale may change at any
	 * moment */
	locale = g_strdup (env_locale);

	g_rec_mutex_unlock (&locales_mutex);

	return locale;
}