PageRenderTime 42ms CodeModel.GetById 19ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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