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 
23 #include <map>
24 #include <string>
25 #include <stdexcept>
26 
28 {
29  // Contains the localization string->string mapping. Populated lazily from PopulateStringTable, thus mutable.
30  typedef std::map<std::string, std::string> Table;
31  mutable Table strings;
32  void PopulateStringTable() const;
33  int32_t ref_count; // ref counter initialized to 1 on ctor; delete when zero is reached
34 public:
36  const std::string &Translate(const std::string &text) const;
37  bool HasTranslation(const std::string &text) const;
38  // do replacement in buffer
39  // if any replacement is done, the buffer will be realloced
40  void ReplaceStrings(StdStrBuf &rBuf);
41  void ReplaceStrings(const StdStrBuf &rBuf, StdStrBuf &rTarget);
42 
43  void AddRef() { ++ref_count; }
44  void DelRef() { if (!--ref_count) delete this; }
45 
46  class NoSuchTranslation : public std::runtime_error
47  {
48  public:
49  NoSuchTranslation(const std::string &text) : std::runtime_error("No such translation: \"" + text + "\"") {}
50  };
51 
52  static inline C4LangStringTable &GetSystemStringTable() { return system_string_table; }
53 protected:
54  virtual void OnLoad() { strings.clear(); } // Make sure we re-populate when the string table is reloaded
55 
56 private:
57  static C4LangStringTable system_string_table;
58 };
59 
60 #endif // INC_C4LangStringTable
const std::string & Translate(const std::string &text) const
NoSuchTranslation(const std::string &text)
virtual void OnLoad()
static C4LangStringTable & GetSystemStringTable()
void ReplaceStrings(StdStrBuf &rBuf)
bool HasTranslation(const std::string &text) const