/xbmc/guilib/LocalizeStrings.cpp

http://github.com/xbmc/xbmc · C++ · 246 lines · 177 code · 32 blank · 37 comment · 43 complexity · 3b55628d0d303c9bc44015242e310a43 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #include "LocalizeStrings.h"
  9. #include "addons/LanguageResource.h"
  10. #include "filesystem/Directory.h"
  11. #include "filesystem/SpecialProtocol.h"
  12. #include "threads/SharedSection.h"
  13. #include "utils/CharsetConverter.h"
  14. #include "utils/POUtils.h"
  15. #include "utils/StringUtils.h"
  16. #include "utils/URIUtils.h"
  17. #include "utils/log.h"
  18. /*! \brief Tries to load ids and strings from a strings.po file to the `strings` map.
  19. * It should only be called from the LoadStr2Mem function to have a fallback.
  20. \param pathname The directory name, where we look for the strings file.
  21. \param strings [out] The resulting strings map.
  22. \param encoding Encoding of the strings. For PO files we only use utf-8.
  23. \param offset An offset value to place strings from the id value.
  24. \param bSourceLanguage If we are loading the source English strings.po.
  25. \return false if no strings.po file was loaded.
  26. */
  27. static bool LoadPO(const std::string &filename, std::map<uint32_t, LocStr>& strings,
  28. std::string &encoding, uint32_t offset = 0 , bool bSourceLanguage = false)
  29. {
  30. CPODocument PODoc;
  31. if (!PODoc.LoadFile(filename))
  32. return false;
  33. int counter = 0;
  34. while ((PODoc.GetNextEntry()))
  35. {
  36. uint32_t id;
  37. if (PODoc.GetEntryType() == ID_FOUND)
  38. {
  39. bool bStrInMem = strings.find((id = PODoc.GetEntryID()) + offset) != strings.end();
  40. PODoc.ParseEntry(bSourceLanguage);
  41. if (bSourceLanguage && !PODoc.GetMsgid().empty())
  42. {
  43. if (bStrInMem && (strings[id + offset].strOriginal.empty() ||
  44. PODoc.GetMsgid() == strings[id + offset].strOriginal))
  45. continue;
  46. else if (bStrInMem)
  47. CLog::Log(LOGDEBUG,
  48. "POParser: id:%i was recently re-used in the English string file, which is not yet "
  49. "changed in the translated file. Using the English string instead", id);
  50. strings[id + offset].strTranslated = PODoc.GetMsgid();
  51. counter++;
  52. }
  53. else if (!bSourceLanguage && !bStrInMem && !PODoc.GetMsgstr().empty())
  54. {
  55. strings[id + offset].strTranslated = PODoc.GetMsgstr();
  56. strings[id + offset].strOriginal = PODoc.GetMsgid();
  57. counter++;
  58. }
  59. }
  60. else if (PODoc.GetEntryType() == MSGID_FOUND)
  61. {
  62. //! @todo implement reading of non-id based string entries from the PO files.
  63. //! These entries would go into a separate memory map, using hash codes for fast look-up.
  64. //! With this memory map we can implement using gettext(), ngettext(), pgettext() calls,
  65. //! so that we don't have to use new IDs for new strings. Even we can start converting
  66. //! the ID based calls to normal gettext calls.
  67. }
  68. else if (PODoc.GetEntryType() == MSGID_PLURAL_FOUND)
  69. {
  70. //! @todo implement reading of non-id based pluralized string entries from the PO files.
  71. //! We can store the pluralforms for each language, in the langinfo.xml files.
  72. }
  73. }
  74. CLog::Log(LOGDEBUG, "LocalizeStrings: loaded %i strings from file %s", counter, filename.c_str());
  75. return true;
  76. }
  77. /*! \brief Loads language ids and strings to memory map `strings`.
  78. \param pathname The directory name, where we look for the strings file.
  79. \param language We load the strings for this language. Fallback language is always English.
  80. \param strings [out] The resulting strings map.
  81. \param encoding Encoding of the strings. For PO files we only use utf-8.
  82. \param offset An offset value to place strings from the id value.
  83. \return false if no strings.po file was loaded.
  84. */
  85. static bool LoadStr2Mem(const std::string &pathname_in, const std::string &language,
  86. std::map<uint32_t, LocStr>& strings, std::string &encoding, uint32_t offset = 0 )
  87. {
  88. std::string pathname = CSpecialProtocol::TranslatePathConvertCase(pathname_in + language);
  89. if (!XFILE::CDirectory::Exists(pathname))
  90. {
  91. bool exists = false;
  92. std::string lang;
  93. // check if there's a language addon using the old language naming convention
  94. if (ADDON::CLanguageResource::FindLegacyLanguage(language, lang))
  95. {
  96. pathname = CSpecialProtocol::TranslatePathConvertCase(pathname_in + lang);
  97. exists = XFILE::CDirectory::Exists(pathname);
  98. }
  99. if (!exists)
  100. return false;
  101. }
  102. bool useSourceLang = StringUtils::EqualsNoCase(language, LANGUAGE_DEFAULT) || StringUtils::EqualsNoCase(language, LANGUAGE_OLD_DEFAULT);
  103. return LoadPO(URIUtils::AddFileToFolder(pathname, "strings.po"), strings, encoding, offset, useSourceLang);
  104. }
  105. static bool LoadWithFallback(const std::string& path, const std::string& language, std::map<uint32_t, LocStr>& strings)
  106. {
  107. std::string encoding;
  108. if (!LoadStr2Mem(path, language, strings, encoding))
  109. {
  110. if (StringUtils::EqualsNoCase(language, LANGUAGE_DEFAULT)) // no fallback, nothing to do
  111. return false;
  112. }
  113. // load the fallback
  114. if (!StringUtils::EqualsNoCase(language, LANGUAGE_DEFAULT))
  115. LoadStr2Mem(path, LANGUAGE_DEFAULT, strings, encoding);
  116. return true;
  117. }
  118. CLocalizeStrings::CLocalizeStrings(void) = default;
  119. CLocalizeStrings::~CLocalizeStrings(void) = default;
  120. void CLocalizeStrings::ClearSkinStrings()
  121. {
  122. // clear the skin strings
  123. CExclusiveLock lock(m_stringsMutex);
  124. Clear(31000, 31999);
  125. }
  126. bool CLocalizeStrings::LoadSkinStrings(const std::string& path, const std::string& language)
  127. {
  128. //! @todo shouldn't hold lock while loading file
  129. CExclusiveLock lock(m_stringsMutex);
  130. ClearSkinStrings();
  131. // load the skin strings in.
  132. return LoadWithFallback(path, language, m_strings);
  133. }
  134. bool CLocalizeStrings::Load(const std::string& strPathName, const std::string& strLanguage)
  135. {
  136. std::map<uint32_t, LocStr> strings;
  137. if (!LoadWithFallback(strPathName, strLanguage, strings))
  138. return false;
  139. // fill in the constant strings
  140. strings[20022].strTranslated = "";
  141. strings[20027].strTranslated = "°F";
  142. strings[20028].strTranslated = "K";
  143. strings[20029].strTranslated = "°C";
  144. strings[20030].strTranslated = "°Ré";
  145. strings[20031].strTranslated = "°Ra";
  146. strings[20032].strTranslated = "°Rø";
  147. strings[20033].strTranslated = "°De";
  148. strings[20034].strTranslated = "°N";
  149. strings[20200].strTranslated = "km/h";
  150. strings[20201].strTranslated = "m/min";
  151. strings[20202].strTranslated = "m/s";
  152. strings[20203].strTranslated = "ft/h";
  153. strings[20204].strTranslated = "ft/min";
  154. strings[20205].strTranslated = "ft/s";
  155. strings[20206].strTranslated = "mph";
  156. strings[20207].strTranslated = "kts";
  157. strings[20208].strTranslated = "Beaufort";
  158. strings[20209].strTranslated = "inch/s";
  159. strings[20210].strTranslated = "yard/s";
  160. strings[20211].strTranslated = "Furlong/Fortnight";
  161. CExclusiveLock lock(m_stringsMutex);
  162. Clear();
  163. m_strings = std::move(strings);
  164. return true;
  165. }
  166. const std::string& CLocalizeStrings::Get(uint32_t dwCode) const
  167. {
  168. CSharedLock lock(m_stringsMutex);
  169. ciStrings i = m_strings.find(dwCode);
  170. if (i == m_strings.end())
  171. {
  172. return StringUtils::Empty;
  173. }
  174. return i->second.strTranslated;
  175. }
  176. void CLocalizeStrings::Clear()
  177. {
  178. CExclusiveLock lock(m_stringsMutex);
  179. m_strings.clear();
  180. }
  181. void CLocalizeStrings::Clear(uint32_t start, uint32_t end)
  182. {
  183. CExclusiveLock lock(m_stringsMutex);
  184. iStrings it = m_strings.begin();
  185. while (it != m_strings.end())
  186. {
  187. if (it->first >= start && it->first <= end)
  188. m_strings.erase(it++);
  189. else
  190. ++it;
  191. }
  192. }
  193. bool CLocalizeStrings::LoadAddonStrings(const std::string& path, const std::string& language, const std::string& addonId)
  194. {
  195. std::map<uint32_t, LocStr> strings;
  196. if (!LoadWithFallback(path, language, strings))
  197. return false;
  198. CExclusiveLock lock(m_addonStringsMutex);
  199. auto it = m_addonStrings.find(addonId);
  200. if (it != m_addonStrings.end())
  201. m_addonStrings.erase(it);
  202. return m_addonStrings.emplace(std::string(addonId), std::move(strings)).second;
  203. }
  204. std::string CLocalizeStrings::GetAddonString(const std::string& addonId, uint32_t code)
  205. {
  206. CSharedLock lock(m_addonStringsMutex);
  207. auto i = m_addonStrings.find(addonId);
  208. if (i == m_addonStrings.end())
  209. return StringUtils::Empty;
  210. auto j = i->second.find(code);
  211. if (j == i->second.end())
  212. return StringUtils::Empty;
  213. return j->second.strTranslated;
  214. }