summaryrefslogtreecommitdiff
path: root/i18n.c
blob: 664b8113778420a83dd503d19bfddb759b836b9b (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * i18n.c: Internationalization
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: i18n.c 1.310 2007/08/17 12:31:17 kls Exp $
 *
 *
 */

/*
 * In case an English phrase is used in more than one context (and might need
 * different translations in other languages) it can be preceded with an
 * arbitrary string to describe its context, separated from the actual phrase
 * by a '$' character (see for instance "Button$Stop" vs. "Stop").
 * Of course this means that no English phrase may contain the '$' character!
 * If this should ever become necessary, the existing '$' would have to be
 * replaced with something different...
 */

#include "i18n.h"
#include <ctype.h>
#include <libintl.h>
#include <locale.h>
#include "tools.h"

// TRANSLATORS: The name of the language, as written natively
const char *LanguageName = trNOOP("LanguageName$English");
// TRANSLATORS: The 3-letter code of the language
const char *LanguageCode = trNOOP("LanguageCode$eng");

// List of known language codes with aliases.
// Actually we could list all codes from http://www.loc.gov/standards/iso639-2
// here, but that would be several hundreds - and for most of them it's unlikely
// they're ever going to be used...

const char *LanguageCodeList[] = {
  "eng,dos",
  "deu,ger",
  "slv,slo",
  "ita",
  "dut,nla,nld",
  "por",
  "fra,fre",
  "nor",
  "fin,smi",
  "pol",
  "esl,spa",
  "ell,gre",
  "sve,swe",
  "rom,rum",
  "hun",
  "cat,cln",
  "rus",
  "hrv",
  "est",
  "dan",
  "cze,ces",
  "tur",
  NULL
  };

static char *I18nLocaleDir = LOCDIR;

static cStringList LanguageLocales;
static cStringList LanguageNames;
static cStringList LanguageCodes;

static int CurrentLanguage = 0;

static bool ContainsCode(const char *Codes, const char *Code)
{
  while (*Codes) {
        int l = 0;
        for ( ; l < 3 && Code[l]; l++) {
            if (Codes[l] != tolower(Code[l]))
               break;
            }
        if (l == 3)
           return true;
        Codes++;
        }
  return false;
}

static const char *SkipContext(const char *s)
{
  const char *p = strchr(s, '$');
  return p ? p + 1 : s;
}

void I18nInitialize(void)
{
  LanguageLocales.Append(strdup(I18N_DEFAULT_LOCALE));
  LanguageNames.Append(strdup(SkipContext(LanguageName)));
  LanguageCodes.Append(strdup(LanguageCodeList[0]));
  textdomain("vdr");
  bindtextdomain("vdr", I18nLocaleDir);
  cFileNameList Locales(I18nLocaleDir, true);
  if (Locales.Size() > 0) {
     dsyslog("found %d locales in %s", Locales.Size(), I18nLocaleDir);
     int MatchFull = 0, MatchPartial = 0;
     char *OldLocale = strdup(setlocale(LC_MESSAGES, NULL));
     for (int i = 0; i < Locales.Size(); i++) {
         if (i < I18N_MAX_LANGUAGES - 1) {
            if (setlocale(LC_MESSAGES, Locales[i])) {
               if (strstr(OldLocale, Locales[i]) == OldLocale)
                  MatchFull = LanguageLocales.Size();
               else if (strncmp(OldLocale, Locales[i], 2) == 0)
                  MatchPartial = LanguageLocales.Size();
               LanguageLocales.Append(strdup(Locales[i]));
               LanguageNames.Append(strdup(gettext(LanguageName)));
               const char *Code = gettext(LanguageCode);
               for (const char **lc = LanguageCodeList; *lc; lc++) {
                   if (ContainsCode(*lc, Code)) {
                      Code = *lc;
                      break;
                      }
                   }
               LanguageCodes.Append(strdup(Code));
               }
            }
         else
            esyslog("ERROR: too many locales - increase I18N_MAX_LANGUAGES!");
         }
     CurrentLanguage = MatchFull ? MatchFull : MatchPartial;
     setlocale(LC_MESSAGES, CurrentLanguage ? LanguageLocales[CurrentLanguage] : OldLocale);
     free(OldLocale);
     }
  // Prepare any known language codes for which there was no locale:
  for (const char **lc = LanguageCodeList; *lc; lc++) {
      bool Found = false;
      for (int i = 0; i < LanguageCodes.Size(); i++) {
          if (strcmp(*lc, LanguageCodes[i]) == 0) {
             Found = true;
             break;
             }
          }
      if (!Found) {
         dsyslog("no locale for language code '%s'", *lc);
         LanguageLocales.Append(strdup(I18N_DEFAULT_LOCALE));
         LanguageNames.Append(strdup(*lc));
         LanguageCodes.Append(strdup(*lc));
         }
      }
}

void I18nRegister(const char *Plugin)
{
  bindtextdomain(Plugin, I18nLocaleDir);
}

void I18nSetLocale(const char *Locale)
{
  if (Locale && *Locale) {
     int i = LanguageLocales.Find(Locale);
     if (i >= 0) {
        CurrentLanguage = i;
        setlocale(LC_MESSAGES, Locale);
        }
     else
        dsyslog("unknown locale: '%s'", Locale);
     }
}

int I18nCurrentLanguage(void)
{
  return CurrentLanguage;
}

void I18nSetLanguage(int Language)
{
  if (Language < LanguageNames.Size()) {
     CurrentLanguage = Language;
     I18nSetLocale(I18nLocale(CurrentLanguage));
     }
}

const cStringList *I18nLanguages(void)
{
  return &LanguageNames;
}

const char *I18nTranslate(const char *s, const char *Plugin)
{
  if (!s)
     return s;
  if (CurrentLanguage) {
     const char *t = s;
     if (Plugin)
        t = dgettext(Plugin, s);
     if (t == s)
        t = gettext(s);
     s = t;
     }
  const char *p = strchr(s, '$');
  return p ? p + 1 : s;
}

const char *I18nLocale(int Language)
{
  return 0 <= Language && Language < LanguageLocales.Size() ? LanguageLocales[Language] : NULL;
}

const char *I18nLanguageCode(int Language)
{
  return 0 <= Language && Language < LanguageCodes.Size() ? LanguageCodes[Language] : NULL;
}

int I18nLanguageIndex(const char *Code)
{
  for (int i = 0; i < LanguageCodes.Size(); i++) {
      if (ContainsCode(LanguageCodes[i], Code))
         return i;
      }
  //dsyslog("unknown language code: '%s'", Code);
  return -1;
}

const char *I18nNormalizeLanguageCode(const char *Code)
{
  for (int i = 0; i < 3; i++) {
      if (Code[i]) {
         // ETSI EN 300 468 defines language codes as consisting of three letters
         // according to ISO 639-2. This means that they are supposed to always consist
         // of exactly three letters in the range a-z - no digits, UTF-8 or other
         // funny characters. However, some broadcasters apparently don't have a
         // copy of the DVB standard (or they do, but are perhaps unable to read it),
         // so they put all sorts of non-standard stuff into the language codes,
         // like nonsense as "2ch" or "A 1" (yes, they even go as far as using
         // blanks!). Such things should go into the description of the EPG event's
         // ComponentDescriptor.
         // So, as a workaround for this broadcaster stupidity, let's ignore
         // language codes with unprintable characters...
         if (!isprint(Code[i])) {
            //dsyslog("invalid language code: '%s'", Code);
            return "???";
            }
         // ...and replace blanks with underlines (ok, this breaks the 'const'
         // of the Code parameter - but hey, it's them who started this):
         if (Code[i] == ' ')
            *((char *)&Code[i]) = '_';
         }
      else
         break;
      }
  int n = I18nLanguageIndex(Code);
  return n >= 0 ? I18nLanguageCode(n) : Code;
}

bool I18nIsPreferredLanguage(int *PreferredLanguages, const char *LanguageCode, int &OldPreference, int *Position)
{
  int pos = 1;
  bool found = false;
  while (LanguageCode) {
        int LanguageIndex = I18nLanguageIndex(LanguageCode);
        for (int i = 0; i < LanguageCodes.Size(); i++) {
            if (PreferredLanguages[i] < 0)
               break; // the language is not a preferred one
            if (PreferredLanguages[i] == LanguageIndex) {
               if (OldPreference < 0 || i < OldPreference) {
                  OldPreference = i;
                  if (Position)
                     *Position = pos;
                  found = true;
                  break;
                  }
               }
            }
        if ((LanguageCode = strchr(LanguageCode, '+')) != NULL) {
           LanguageCode++;
           pos++;
           }
        else if (pos == 1 && Position)
           *Position = 0;
        }
  if (OldPreference < 0) {
     OldPreference = LanguageCodes.Size(); // higher than the maximum possible value
     return true; // if we don't find a preferred one, we take the first one
     }
  return found;
}