2 * Copyright (C) 2005-2013 Team XBMC
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, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "LocalizeStrings.h"
23 #include "utils/CharsetConverter.h"
24 #include "utils/log.h"
25 #include "filesystem/SpecialProtocol.h"
26 #include "utils/XMLUtils.h"
27 #include "utils/URIUtils.h"
28 #include "utils/POUtils.h"
29 #include "filesystem/Directory.h"
31 CLocalizeStrings::CLocalizeStrings(void)
36 CLocalizeStrings::~CLocalizeStrings(void)
41 CStdString CLocalizeStrings::ToUTF8(const CStdString& strEncoding, const CStdString& str)
43 if (strEncoding.IsEmpty())
47 g_charsetConverter.stringCharsetToUtf8(strEncoding, str, ret);
51 void CLocalizeStrings::ClearSkinStrings()
53 // clear the skin strings
57 bool CLocalizeStrings::LoadSkinStrings(const CStdString& path, const CStdString& language)
60 // load the skin strings in.
62 if (!LoadStr2Mem(path, language, encoding))
64 if (language.Equals(SOURCE_LANGUAGE)) // no fallback, nothing to do
69 if (!language.Equals(SOURCE_LANGUAGE))
70 LoadStr2Mem(path, SOURCE_LANGUAGE, encoding);
75 bool CLocalizeStrings::LoadStr2Mem(const CStdString &pathname_in, const CStdString &language,
76 CStdString &encoding, uint32_t offset /* = 0 */)
78 CStdString pathname = CSpecialProtocol::TranslatePathConvertCase(pathname_in + language);
79 if (!XFILE::CDirectory::Exists(pathname))
82 "LocalizeStrings: no translation available in currently set gui language, at path %s",
87 if (LoadPO(URIUtils::AddFileToFolder(pathname, "strings.po"), encoding, offset,
88 language.Equals(SOURCE_LANGUAGE)))
91 CLog::Log(LOGDEBUG, "LocalizeStrings: no strings.po file exist at %s, fallback to strings.xml",
93 return LoadXML(URIUtils::AddFileToFolder(pathname, "strings.xml"), encoding, offset);
96 bool CLocalizeStrings::LoadPO(const CStdString &filename, CStdString &encoding,
97 uint32_t offset /* = 0 */, bool bSourceLanguage)
100 if (!PODoc.LoadFile(filename))
105 while ((PODoc.GetNextEntry()))
108 if (PODoc.GetEntryType() == ID_FOUND)
110 bool bStrInMem = m_strings.find((id = PODoc.GetEntryID()) + offset) != m_strings.end();
111 PODoc.ParseEntry(bSourceLanguage);
113 if (bSourceLanguage && !PODoc.GetMsgid().empty())
115 if (bStrInMem && (m_strings[id + offset].strOriginal.IsEmpty() ||
116 PODoc.GetMsgid() == m_strings[id + offset].strOriginal))
120 "POParser: id:%i was recently re-used in the English string file, which is not yet "
121 "changed in the translated file. Using the English string instead", id);
122 m_strings[id + offset].strTranslated = PODoc.GetMsgid();
125 else if (!bSourceLanguage && !bStrInMem && !PODoc.GetMsgstr().empty())
127 m_strings[id + offset].strTranslated = PODoc.GetMsgstr();
128 m_strings[id + offset].strOriginal = PODoc.GetMsgid();
132 else if (PODoc.GetEntryType() == MSGID_FOUND)
134 // TODO: implement reading of non-id based string entries from the PO files.
135 // These entries would go into a separate memory map, using hash codes for fast look-up.
136 // With this memory map we can implement using gettext(), ngettext(), pgettext() calls,
137 // so that we don't have to use new IDs for new strings. Even we can start converting
138 // the ID based calls to normal gettext calls.
140 else if (PODoc.GetEntryType() == MSGID_PLURAL_FOUND)
142 // TODO: implement reading of non-id based pluralized string entries from the PO files.
143 // We can store the pluralforms for each language, in the langinfo.xml files.
147 CLog::Log(LOGDEBUG, "POParser: loaded %i strings from file %s", counter, filename.c_str());
151 bool CLocalizeStrings::LoadXML(const CStdString &filename, CStdString &encoding, uint32_t offset /* = 0 */)
154 if (!xmlDoc.LoadFile(filename))
156 CLog::Log(LOGDEBUG, "unable to load %s: %s at line %d", filename.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
160 XMLUtils::GetEncoding(&xmlDoc, encoding);
162 TiXmlElement* pRootElement = xmlDoc.RootElement();
163 if (!pRootElement || pRootElement->NoChildren() ||
164 pRootElement->ValueStr()!=CStdString("strings"))
166 CLog::Log(LOGERROR, "%s Doesn't contain <strings>", filename.c_str());
170 const TiXmlElement *pChild = pRootElement->FirstChildElement("string");
173 // Load old style language file with id as attribute
174 const char* attrId=pChild->Attribute("id");
175 if (attrId && !pChild->NoChildren())
177 int id = atoi(attrId) + offset;
178 if (m_strings.find(id) == m_strings.end())
179 m_strings[id].strTranslated = ToUTF8(encoding, pChild->FirstChild()->Value());
181 pChild = pChild->NextSiblingElement("string");
186 bool CLocalizeStrings::Load(const CStdString& strPathName, const CStdString& strLanguage)
188 bool bLoadFallback = !strLanguage.Equals(SOURCE_LANGUAGE);
193 if (!LoadStr2Mem(strPathName, strLanguage, encoding))
195 // try loading the fallback
196 if (!bLoadFallback || !LoadStr2Mem(strPathName, SOURCE_LANGUAGE, encoding))
199 bLoadFallback = false;
203 LoadStr2Mem(strPathName, SOURCE_LANGUAGE, encoding);
205 // fill in the constant strings
206 m_strings[20022].strTranslated = "";
207 m_strings[20027].strTranslated = "°F";
208 m_strings[20028].strTranslated = "K";
209 m_strings[20029].strTranslated = "°C";
210 m_strings[20030].strTranslated = "°Ré";
211 m_strings[20031].strTranslated = "°Ra";
212 m_strings[20032].strTranslated = "°Rø";
213 m_strings[20033].strTranslated = "°De";
214 m_strings[20034].strTranslated = "°N";
216 m_strings[20200].strTranslated = "km/h";
217 m_strings[20201].strTranslated = "m/min";
218 m_strings[20202].strTranslated = "m/s";
219 m_strings[20203].strTranslated = "ft/h";
220 m_strings[20204].strTranslated = "ft/min";
221 m_strings[20205].strTranslated = "ft/s";
222 m_strings[20206].strTranslated = "mph";
223 m_strings[20207].strTranslated = "kts";
224 m_strings[20208].strTranslated = "Beaufort";
225 m_strings[20209].strTranslated = "inch/s";
226 m_strings[20210].strTranslated = "yard/s";
227 m_strings[20211].strTranslated = "Furlong/Fortnight";
232 static CStdString szEmptyString = "";
234 const CStdString& CLocalizeStrings::Get(uint32_t dwCode) const
236 ciStrings i = m_strings.find(dwCode);
237 if (i == m_strings.end())
239 return szEmptyString;
241 return i->second.strTranslated;
244 void CLocalizeStrings::Clear()
249 void CLocalizeStrings::Clear(uint32_t start, uint32_t end)
251 iStrings it = m_strings.begin();
252 while (it != m_strings.end())
254 if (it->first >= start && it->first <= end)
255 m_strings.erase(it++);
261 uint32_t CLocalizeStrings::LoadBlock(const CStdString &id, const CStdString &path, const CStdString &language)
263 iBlocks it = m_blocks.find(id);
264 if (it != m_blocks.end())
265 return it->second; // already loaded
268 uint32_t offset = block_start + m_blocks.size()*block_size;
269 m_blocks.insert(make_pair(id, offset));
273 bool success = LoadStr2Mem(path, language, encoding, offset);
276 if (language.Equals(SOURCE_LANGUAGE)) // no fallback, nothing to do
281 if (!language.Equals(SOURCE_LANGUAGE))
282 success |= LoadStr2Mem(path, SOURCE_LANGUAGE, encoding, offset);
284 return success ? offset : 0;
287 void CLocalizeStrings::ClearBlock(const CStdString &id)
289 iBlocks it = m_blocks.find(id);
290 if (it == m_blocks.end())
292 CLog::Log(LOGERROR, "%s: Trying to clear non existent block %s", __FUNCTION__, id.c_str());
293 return; // doesn't exist
297 Clear(it->second, it->second + block_size);