[bluray] Fix stream info/language retrieval for blurays in non-nav mode.
[vuplus_xbmc] / xbmc / guilib / LocalizeStrings.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "system.h"
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"
30
31 CLocalizeStrings::CLocalizeStrings(void)
32 {
33
34 }
35
36 CLocalizeStrings::~CLocalizeStrings(void)
37 {
38
39 }
40
41 CStdString CLocalizeStrings::ToUTF8(const CStdString& strEncoding, const CStdString& str)
42 {
43   if (strEncoding.IsEmpty())
44     return str;
45
46   CStdString ret;
47   g_charsetConverter.stringCharsetToUtf8(strEncoding, str, ret);
48   return ret;
49 }
50
51 void CLocalizeStrings::ClearSkinStrings()
52 {
53   // clear the skin strings
54   Clear(31000, 31999);
55 }
56
57 bool CLocalizeStrings::LoadSkinStrings(const CStdString& path, const CStdString& language)
58 {
59   ClearSkinStrings();
60   // load the skin strings in.
61   CStdString encoding;
62   if (!LoadStr2Mem(path, language, encoding))
63   {
64     if (language.Equals(SOURCE_LANGUAGE)) // no fallback, nothing to do
65       return false;
66   }
67
68   // load the fallback
69   if (!language.Equals(SOURCE_LANGUAGE))
70     LoadStr2Mem(path, SOURCE_LANGUAGE, encoding);
71
72   return true;
73 }
74
75 bool CLocalizeStrings::LoadStr2Mem(const CStdString &pathname_in, const CStdString &language,
76                                    CStdString &encoding, uint32_t offset /* = 0 */)
77 {
78   CStdString pathname = CSpecialProtocol::TranslatePathConvertCase(pathname_in + language);
79   if (!XFILE::CDirectory::Exists(pathname))
80   {
81     CLog::Log(LOGDEBUG,
82               "LocalizeStrings: no translation available in currently set gui language, at path %s",
83               pathname.c_str());
84     return false;
85   }
86
87   if (LoadPO(URIUtils::AddFileToFolder(pathname, "strings.po"), encoding, offset,
88       language.Equals(SOURCE_LANGUAGE)))
89     return true;
90
91   CLog::Log(LOGDEBUG, "LocalizeStrings: no strings.po file exist at %s, fallback to strings.xml",
92             pathname.c_str());
93   return LoadXML(URIUtils::AddFileToFolder(pathname, "strings.xml"), encoding, offset);
94 }
95
96 bool CLocalizeStrings::LoadPO(const CStdString &filename, CStdString &encoding,
97                               uint32_t offset /* = 0 */, bool bSourceLanguage)
98 {
99   CPODocument PODoc;
100   if (!PODoc.LoadFile(filename))
101     return false;
102
103   int counter = 0;
104
105   while ((PODoc.GetNextEntry()))
106   {
107     uint32_t id;
108     if (PODoc.GetEntryType() == ID_FOUND)
109     {
110       bool bStrInMem = m_strings.find((id = PODoc.GetEntryID()) + offset) != m_strings.end();
111       PODoc.ParseEntry(bSourceLanguage);
112
113       if (bSourceLanguage && !PODoc.GetMsgid().empty())
114       {
115         if (bStrInMem && (m_strings[id + offset].strOriginal.IsEmpty() ||
116             PODoc.GetMsgid() == m_strings[id + offset].strOriginal))
117           continue;
118         else if (bStrInMem)
119           CLog::Log(LOGDEBUG,
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();
123         counter++;
124       }
125       else if (!bSourceLanguage && !bStrInMem && !PODoc.GetMsgstr().empty())
126       {
127         m_strings[id + offset].strTranslated = PODoc.GetMsgstr();
128         m_strings[id + offset].strOriginal = PODoc.GetMsgid();
129         counter++;
130       }
131     }
132     else if (PODoc.GetEntryType() == MSGID_FOUND)
133     {
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.
139     }
140     else if (PODoc.GetEntryType() == MSGID_PLURAL_FOUND)
141     {
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.
144     }
145   }
146
147   CLog::Log(LOGDEBUG, "POParser: loaded %i strings from file %s", counter, filename.c_str());
148   return true;
149 }
150
151 bool CLocalizeStrings::LoadXML(const CStdString &filename, CStdString &encoding, uint32_t offset /* = 0 */)
152 {
153   CXBMCTinyXML xmlDoc;
154   if (!xmlDoc.LoadFile(filename))
155   {
156     CLog::Log(LOGDEBUG, "unable to load %s: %s at line %d", filename.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
157     return false;
158   }
159
160   XMLUtils::GetEncoding(&xmlDoc, encoding);
161
162   TiXmlElement* pRootElement = xmlDoc.RootElement();
163   if (!pRootElement || pRootElement->NoChildren() ||
164        pRootElement->ValueStr()!=CStdString("strings"))
165   {
166     CLog::Log(LOGERROR, "%s Doesn't contain <strings>", filename.c_str());
167     return false;
168   }
169
170   const TiXmlElement *pChild = pRootElement->FirstChildElement("string");
171   while (pChild)
172   {
173     // Load old style language file with id as attribute
174     const char* attrId=pChild->Attribute("id");
175     if (attrId && !pChild->NoChildren())
176     {
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());
180     }
181     pChild = pChild->NextSiblingElement("string");
182   }
183   return true;
184 }
185
186 bool CLocalizeStrings::Load(const CStdString& strPathName, const CStdString& strLanguage)
187 {
188   bool bLoadFallback = !strLanguage.Equals(SOURCE_LANGUAGE);
189
190   CStdString encoding;
191   Clear();
192
193   if (!LoadStr2Mem(strPathName, strLanguage, encoding))
194   {
195     // try loading the fallback
196     if (!bLoadFallback || !LoadStr2Mem(strPathName, SOURCE_LANGUAGE, encoding))
197       return false;
198
199     bLoadFallback = false;
200   }
201
202   if (bLoadFallback)
203     LoadStr2Mem(strPathName, SOURCE_LANGUAGE, encoding);
204
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";
215
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";
228
229   return true;
230 }
231
232 static CStdString szEmptyString = "";
233
234 const CStdString& CLocalizeStrings::Get(uint32_t dwCode) const
235 {
236   ciStrings i = m_strings.find(dwCode);
237   if (i == m_strings.end())
238   {
239     return szEmptyString;
240   }
241   return i->second.strTranslated;
242 }
243
244 void CLocalizeStrings::Clear()
245 {
246   m_strings.clear();
247 }
248
249 void CLocalizeStrings::Clear(uint32_t start, uint32_t end)
250 {
251   iStrings it = m_strings.begin();
252   while (it != m_strings.end())
253   {
254     if (it->first >= start && it->first <= end)
255       m_strings.erase(it++);
256     else
257       ++it;
258   }
259 }
260
261 uint32_t CLocalizeStrings::LoadBlock(const CStdString &id, const CStdString &path, const CStdString &language)
262 {
263   iBlocks it = m_blocks.find(id);
264   if (it != m_blocks.end())
265     return it->second;  // already loaded
266
267   // grab a new block
268   uint32_t offset = block_start + m_blocks.size()*block_size;
269   m_blocks.insert(make_pair(id, offset));
270
271   // load the strings
272   CStdString encoding;
273   bool success = LoadStr2Mem(path, language, encoding, offset);
274   if (!success)
275   {
276     if (language.Equals(SOURCE_LANGUAGE)) // no fallback, nothing to do
277       return 0;
278   }
279
280   // load the fallback
281   if (!language.Equals(SOURCE_LANGUAGE))
282     success |= LoadStr2Mem(path, SOURCE_LANGUAGE, encoding, offset);
283
284   return success ? offset : 0;
285 }
286
287 void CLocalizeStrings::ClearBlock(const CStdString &id)
288 {
289   iBlocks it = m_blocks.find(id);
290   if (it == m_blocks.end())
291   {
292     CLog::Log(LOGERROR, "%s: Trying to clear non existent block %s", __FUNCTION__, id.c_str());
293     return; // doesn't exist
294   }
295
296   // clear our block
297   Clear(it->second, it->second + block_size);
298   m_blocks.erase(it);
299 }