2
* libintlemu - A Core Foundation libintl emulator
3
* Copyright (C) 2008 Heikki Lindholm <holin@iki.fi>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
11
#include <CoreFoundation/CoreFoundation.h>
15
static pthread_mutex_t intlemu_lock;
16
static CFMutableDictionaryRef intlemu_dict;
18
static void intlemu_cstring_release(CFAllocatorRef allocator, const void *value)
23
void __attribute__ ((constructor)) intlemu_init_() {
24
CFDictionaryValueCallBacks cstring_value_callbacks =
27
NULL, /* retain callback */
28
&intlemu_cstring_release, /* release callback */
29
NULL, /* copy description */
32
if (pthread_mutex_init(&intlemu_lock, NULL) != 0)
35
intlemu_dict = CFDictionaryCreateMutable(
38
&kCFCopyStringDictionaryKeyCallBacks,
39
&cstring_value_callbacks);
40
if (intlemu_dict == NULL)
44
void __attribute__ ((destructor)) intlemu_fini_() {
46
CFRelease(intlemu_dict);
48
pthread_mutex_destroy(&intlemu_lock);
51
char * intlemu_bgettext (CFBundleRef bundle, const char *msgid)
66
key = CFStringCreateWithBytes(
69
(CFIndex)strlen(msgid),
70
kCFStringEncodingUTF8,
73
if (pthread_mutex_lock(&intlemu_lock) != 0)
75
value = (char *)CFDictionaryGetValue(intlemu_dict, key);
76
if (pthread_mutex_unlock(&intlemu_lock) != 0)
83
/* no cached translaation, so, find one from the bundle */
84
s = CFBundleCopyLocalizedString(
93
/* get the length in bytes */
95
r.length = CFStringGetLength(s);
97
clen = CFStringGetBytes(
100
kCFStringEncodingUTF8,
107
if (clen == r.length) {
108
buf = malloc(len + 1);
114
return (char *)msgid;
117
clen = CFStringGetBytes(
120
kCFStringEncodingUTF8,
127
if (clen == r.length) {
128
if (pthread_mutex_lock(&intlemu_lock) != 0)
130
CFDictionaryAddValue(intlemu_dict, key, buf);
131
if (pthread_mutex_unlock(&intlemu_lock) != 0)
144
return (char *)value;