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
|
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#include "LocalizeStrings.h"
#include "utils/CharsetConverter.h"
#include "utils/log.h"
#include "FileSystem/SpecialProtocol.h"
#include "XMLUtils.h"
CLocalizeStrings g_localizeStrings;
CLocalizeStrings g_localizeStringsTemp;
CLocalizeStrings::CLocalizeStrings(void)
{
}
CLocalizeStrings::~CLocalizeStrings(void)
{
}
CStdString CLocalizeStrings::ToUTF8(const CStdString& strEncoding, const CStdString& str)
{
if (strEncoding.IsEmpty())
return str;
CStdString ret;
g_charsetConverter.stringCharsetToUtf8(strEncoding, str, ret);
return ret;
}
void CLocalizeStrings::ClearSkinStrings()
{
// clear the skin strings
Clear(31000, 31999);
}
bool CLocalizeStrings::LoadSkinStrings(const CStdString& path, const CStdString& fallbackPath)
{
ClearSkinStrings();
// load the skin strings in.
CStdString encoding;
if (!LoadXML(path, encoding))
{
if (path == fallbackPath) // no fallback, nothing to do
return false;
}
// load the fallback
if (path != fallbackPath)
LoadXML(fallbackPath, encoding);
return true;
}
bool CLocalizeStrings::LoadXML(const CStdString &filename, CStdString &encoding, uint32_t offset /* = 0 */)
{
TiXmlDocument xmlDoc;
if (!xmlDoc.LoadFile(PTH_IC(filename)))
{
CLog::Log(LOGDEBUG, "unable to load %s: %s at line %d", filename.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
return false;
}
XMLUtils::GetEncoding(&xmlDoc, encoding);
TiXmlElement* pRootElement = xmlDoc.RootElement();
if (!pRootElement || pRootElement->NoChildren() ||
pRootElement->ValueStr()!=CStdString("strings"))
{
CLog::Log(LOGERROR, "%s Doesn't contain <strings>", filename.c_str());
return false;
}
const TiXmlElement *pChild = pRootElement->FirstChildElement("string");
while (pChild)
{
// Load new style language file with id as attribute
const char* attrId=pChild->Attribute("id");
if (attrId && !pChild->NoChildren())
{
int id = atoi(attrId) + offset;
if (m_strings.find(id) == m_strings.end())
m_strings[id] = ToUTF8(encoding, pChild->FirstChild()->Value());
}
pChild = pChild->NextSiblingElement("string");
}
return true;
}
bool CLocalizeStrings::Load(const CStdString& strFileName, const CStdString& strFallbackFileName)
{
bool bLoadFallback = !strFileName.Equals(strFallbackFileName);
CStdString encoding;
Clear();
if (!LoadXML(strFileName, encoding))
{
// try loading the fallback
if (!bLoadFallback || !LoadXML(strFallbackFileName, encoding))
return false;
bLoadFallback = false;
}
if (bLoadFallback)
LoadXML(strFallbackFileName, encoding);
// fill in the constant strings
m_strings[20022] = "";
m_strings[20027] = ToUTF8(encoding, "°F");
m_strings[20028] = ToUTF8(encoding, "K");
m_strings[20029] = ToUTF8(encoding, "°C");
m_strings[20030] = ToUTF8(encoding, "°Ré");
m_strings[20031] = ToUTF8(encoding, "°Ra");
m_strings[20032] = ToUTF8(encoding, "°Rø");
m_strings[20033] = ToUTF8(encoding, "°De");
m_strings[20034] = ToUTF8(encoding, "°N");
m_strings[20200] = ToUTF8(encoding, "km/h");
m_strings[20201] = ToUTF8(encoding, "m/min");
m_strings[20202] = ToUTF8(encoding, "m/s");
m_strings[20203] = ToUTF8(encoding, "ft/h");
m_strings[20204] = ToUTF8(encoding, "ft/min");
m_strings[20205] = ToUTF8(encoding, "ft/s");
m_strings[20206] = ToUTF8(encoding, "mph");
m_strings[20207] = ToUTF8(encoding, "kts");
m_strings[20208] = ToUTF8(encoding, "Beaufort");
m_strings[20209] = ToUTF8(encoding, "inch/s");
m_strings[20210] = ToUTF8(encoding, "yard/s");
m_strings[20211] = ToUTF8(encoding, "Furlong/Fortnight");
return true;
}
static CStdString szEmptyString = "";
const CStdString& CLocalizeStrings::Get(uint32_t dwCode) const
{
ciStrings i = m_strings.find(dwCode);
if (i == m_strings.end())
{
return szEmptyString;
}
return i->second;
}
void CLocalizeStrings::Clear()
{
m_strings.clear();
}
void CLocalizeStrings::Clear(uint32_t start, uint32_t end)
{
iStrings it = m_strings.begin();
while (it != m_strings.end())
{
if (it->first >= start && it->first <= end)
m_strings.erase(it++);
else
++it;
}
}
uint32_t CLocalizeStrings::LoadBlock(const CStdString &id, const CStdString &path, const CStdString &fallbackPath)
{
iBlocks it = m_blocks.find(id);
if (it != m_blocks.end())
return it->second; // already loaded
// grab a new block
uint32_t offset = block_start + m_blocks.size()*block_size;
m_blocks.insert(make_pair(id, offset));
// load the strings
CStdString encoding;
bool success = LoadXML(path, encoding, offset);
if (!success)
{
if (path == fallbackPath) // no fallback, nothing to do
return 0;
}
// load the fallback
if (path != fallbackPath)
success |= LoadXML(fallbackPath, encoding, offset);
return success ? offset : 0;
}
void CLocalizeStrings::ClearBlock(const CStdString &id)
{
iBlocks it = m_blocks.find(id);
if (it == m_blocks.end())
{
CLog::Log(LOGERROR, "%s: Trying to clear non existent block %s", __FUNCTION__, id.c_str());
return; // doesn't exist
}
// clear our block
Clear(it->second, it->second + block_size);
m_blocks.erase(it);
}
|