summaryrefslogtreecommitdiffstats
path: root/gettext.c
blob: 7b5f3018b2a1cc7b56208e6d39c2a562706a756d (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string.h>

#include "mutt.h"

#include "hash.h"

#include "iconv.h"
#include "lib.h"
#include "charset.h"

#ifdef ENABLE_NLS

/*
 * One day, gettext will return strings in the appropriate
 * encoding. In the meantime, we use this code to handle
 * the conversion.
 */

/*
 * Note: the routines in this file can be invoked before debugfile
 * has been opened.  When using dprint here, please check if
 * debugfile != NULL before.
 */

static HASH *Messages = NULL;
static char *PoHeader = NULL;
static char *PoCharset = NULL;
static char *MessageCharset = NULL;

struct msg
{
  char *key;
  char *data;
};

static char *get_charset (const char *header)
{
  /* FIXME: the comparison should at least be case-insensitive */
  const char f[] = "\nContent-Type: text/plain; charset=";
  char *s, *t;

  if (!header) return NULL;
  
  if ((s = strstr (header, f)))
  {
    s += sizeof (f)-1;
    for (t = s; *t >= 32; t++)
      ;
    return mutt_substrdup (s, t);
  }
  
  /* else */
  
  return NULL;
}

static void destroy_one_message (void *vp)
{
  struct msg *mp = (struct msg *) vp;
  
  safe_free ((void **) &mp->key);
  safe_free ((void **) &mp->data);
}

static void destroy_messages (void)
{
  if (Messages)
    hash_destroy (&Messages, destroy_one_message);
}

static void set_po_charset (void)
{
  char *t;
  char *empty = "";

  t = gettext (empty);

  if (mutt_strcmp (t, PoHeader))
  {
    mutt_str_replace (&PoHeader, t);
    safe_free ((void **) &PoCharset);
    PoCharset = get_charset (PoHeader);
    
    destroy_messages ();
  }
}

static void set_message_charset (void)
{
  if (mutt_strcmp (MessageCharset, Charset))
  {
    mutt_str_replace (&MessageCharset, Charset);
    destroy_messages ();
  }
}

    
char *mutt_gettext (const char *message)
{
  char *orig;
  struct msg *mp;
  
  set_po_charset ();
  set_message_charset ();

  if (!Messages && MessageCharset)
    Messages = hash_create (127);

  orig = gettext (message);

# ifdef DEBUG
  if (debugfile)
    dprint (3, (debugfile, "mutt_gettext (`%s'): original gettext returned `%s'\n",
		message, orig));
# endif
  
  if (!Messages)
    return orig;
  
  if ((mp = hash_find (Messages, orig)))
  {
# ifdef DEBUG
    if (debugfile)
      dprint (3, (debugfile, "mutt_gettext: cache hit - key = `%s', data = `%s'\n", orig, mp->data));
# endif
    return mp->data;
  }

  /* the message could not be found in the hash */
  mp = safe_malloc (sizeof (struct msg));
  mp->key = safe_strdup (orig);
  mp->data = safe_strdup (orig);
  mutt_convert_string (&mp->data, PoCharset ? PoCharset : "utf-8", 
		       MessageCharset, 0);

# ifdef DEBUG
  if (debugfile)
    dprint (3, (debugfile, "mutt_gettext: conversion done - src = `%s', res = `%s'\n", 
		mp->key, mp->data));
# endif
  
  hash_insert (Messages, mp->key, mp, 0);
  
  return mp->data;
}

#endif /* ENABLE_NLS */