]> git.vomp.tv Git - vompclient.git/blob - i18n.cc
Switch trunk code to new boxes
[vompclient.git] / i18n.cc
1 /*
2  * i18n.c: Internationalization
3  *
4  * This code is taken from the VDR project and modified for VOMP.
5  * See the main source file 'vdr.c' for original copyright information.
6  * Modifications (C) 2005 D Pickles.
7
8     This file is part of VOMP.
9
10     VOMP is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     VOMP is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with VOMP; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "i18n.h"
26 #include "language-data.h"
27
28 #include "log.h"
29 #include "vdr.h"
30
31 int I18n::LanguageID = DEFAULT_LANGUAGE_INDEX;
32
33 int I18n::initialize(void)
34 {
35   VDR *vdr = VDR::getInstance();
36   char *lang = vdr->configLoad("General", "Language");
37   if (lang)
38   {
39     LanguageID = LanguageIndex(lang);
40     if (LanguageID == -1)
41     {
42       LanguageID = 0;
43     }
44     delete[] lang;
45   }
46   else
47   {
48     LanguageID = DEFAULT_LANGUAGE_INDEX;
49   }
50   return LanguageID;
51 }
52
53 const char* I18n::translate(const char* s)
54 {
55   if (LanguageID >= 0)
56   {
57     const tI18nPhrase *p = Phrases;
58     for (int i = ((p == Phrases) ? 1 : 2); i--; )
59     {
60       for (; **p; p++)
61       {
62         if (strcmp(s, **p) == 0)
63         {
64           char *t = (*p)[LanguageID];
65           if (t && *t) return t;
66         }
67       }
68       p = Phrases;
69     }
70     Log::getInstance()->log("I18n", Log::ERR, "No translation found for '%s' in language %d (%s)",
71                             s, LanguageID, LanguageName(LanguageID));
72   }
73
74   char *p = strchr(s, '$');
75   return p ? p + 1 : s;
76 }
77
78 const char* const * I18n::CharSets(void)
79 {
80   return charSets;
81 }
82
83 const char* const I18n::LanguageCode(int Index)
84 {
85   return 0 <= Index && Index < NumLanguages ? languageCodes[Index] : NULL;
86 }
87
88 const char* I18n::LanguageName(int Index)
89 {
90   return 0 <= Index && Index < NumLanguages ? Languages[Index] : NULL;
91 }
92
93 int I18n::LanguageIndex(const char* Name)
94 {
95   for (int i = 0; i < NumLanguages; i++)
96   {
97     if (STRCASESTR(Languages[i], Name)) return i;
98   }
99   Log::getInstance()->log("I18n", Log::ERR, "Unknown language: '%s'", Name);
100   return -1;
101 }
102
103 int I18n::GetNumLanguages(void)
104 {
105   return NumLanguages;
106 }