OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4LangStringTable.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // Loads StringTbl* and replaces $..$-strings by localized versions
17 
18 #ifndef INC_C4LangStringTable
19 #define INC_C4LangStringTable
20 
22 
24 {
25  // Contains the localization string->string mapping. Populated lazily from PopulateStringTable, thus mutable.
26  typedef std::map<std::string, std::string> Table;
27  mutable Table strings;
28  void PopulateStringTable() const;
29  int32_t ref_count{1}; // ref counter initialized to 1 on ctor; delete when zero is reached
30 public:
31  C4LangStringTable() = default;
32  const std::string &Translate(const std::string &text) const;
33  bool HasTranslation(const std::string &text) const;
34  // do replacement in buffer
35  // if any replacement is done, the buffer will be realloced
36  void ReplaceStrings(StdStrBuf &rBuf);
37  void ReplaceStrings(const StdStrBuf &rBuf, StdStrBuf &rTarget);
38 
39  void AddRef() { ++ref_count; }
40  void DelRef() { if (!--ref_count) delete this; }
41 
42  class NoSuchTranslation : public std::runtime_error
43  {
44  public:
45  NoSuchTranslation(const std::string &text) : std::runtime_error(R"(No such translation: ")" + text + R"(")") {}
46  };
47 
48  static inline C4LangStringTable &GetSystemStringTable() { return system_string_table; }
49 protected:
50  void OnLoad() override { strings.clear(); } // Make sure we re-populate when the string table is reloaded
51 
52 private:
53  static C4LangStringTable system_string_table;
54 };
55 
56 #endif // INC_C4LangStringTable
const std::string & Translate(const std::string &text) const
void OnLoad() override
NoSuchTranslation(const std::string &text)
C4LangStringTable()=default
static C4LangStringTable & GetSystemStringTable()
void ReplaceStrings(StdStrBuf &rBuf)
bool HasTranslation(const std::string &text) const