Pyrogenesis  trunk
dictionary.hpp
Go to the documentation of this file.
1 // tinygettext - A gettext replacement that works directly on .po files
2 // Copyright (c) 2006 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgement in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 
20 #ifndef HEADER_TINYGETTEXT_DICTIONARY_HPP
21 #define HEADER_TINYGETTEXT_DICTIONARY_HPP
22 
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "plural_forms.hpp"
28 
29 namespace tinygettext {
30 
31 /** A simple dictionary class that mimics gettext() behaviour. Each
32  Dictionary only works for a single language, for managing multiple
33  languages and .po files at once use the DictionaryManager. */
35 {
36 private:
37  typedef std::unordered_map<std::string, std::vector<std::string> > Entries;
38  Entries entries;
39 
40  typedef std::unordered_map<std::string, Entries> CtxtEntries;
41  CtxtEntries ctxt_entries;
42 
43  std::string charset;
45 
46  std::string translate(const Entries& dict, const std::string& msgid) const;
47  std::string translate_plural(const Entries& dict, const std::string& msgid, const std::string& msgidplural, int num) const;
48 
51 
52 public:
53  /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */
54  Dictionary(const std::string& charset = "UTF-8");
55  ~Dictionary();
56 
57  /** Return the charset used for this dictionary */
58  std::string get_charset() const;
59 
60  void set_plural_forms(const PluralForms&);
62 
63 
64  /** Translate the string \a msgid. */
65  std::string translate(const std::string& msgid) const;
66 
67  /** Translate the string \a msgid to its correct plural form, based
68  on the number of items given by \a num. \a msgid_plural is \a msgid in
69  plural form. */
70  std::string translate_plural(const std::string& msgid, const std::string& msgidplural, int num) const;
71 
72  /** Translate the string \a msgid that is in context \a msgctx. A
73  context is a way to disambiguate msgids that contain the same
74  letters, but different meaning. For example "exit" might mean to
75  quit doing something or it might refer to a door that leads
76  outside (i.e. 'Ausgang' vs 'Beenden' in german) */
77  std::string translate_ctxt(const std::string& msgctxt, const std::string& msgid) const;
78 
79  std::string translate_ctxt_plural(const std::string& msgctxt, const std::string& msgid, const std::string& msgidplural, int num) const;
80 
81  /** Add a translation from \a msgid to \a msgstr to the dictionary,
82  where \a msgid is the singular form of the message, msgid_plural the
83  plural form and msgstrs a table of translations. The right
84  translation will be calculated based on the \a num argument to
85  translate(). */
86  void add_translation(const std::string& msgid, const std::string& msgid_plural,
87  const std::vector<std::string>& msgstrs);
88  void add_translation(const std::string& msgctxt,
89  const std::string& msgid, const std::string& msgid_plural,
90  const std::vector<std::string>& msgstrs);
91 
92  /** Add a translation from \a msgid to \a msgstr to the
93  dictionary */
94  void add_translation(const std::string& msgid, const std::string& msgstr);
95  void add_translation(const std::string& msgctxt, const std::string& msgid, const std::string& msgstr);
96 
97  /** Iterate over all messages, Func is of type:
98  void func(const std::string& msgid, const std::vector<std::string>& msgstrs) */
99  template<class Func>
100  Func foreach(Func func)
101  {
102  for(Entries::iterator i = entries.begin(); i != entries.end(); ++i)
103  {
104  func(i->first, i->second);
105  }
106  return func;
107  }
108 
109  void addFallback(Dictionary* fallback)
110  {
111  m_has_fallback = true;
112  m_fallback = fallback;
113  }
114 
115  /** Iterate over all messages with a context, Func is of type:
116  void func(const std::string& ctxt, const std::string& msgid, const std::vector<std::string>& msgstrs) */
117  template<class Func>
118  Func foreach_ctxt(Func func)
119  {
120  for(CtxtEntries::iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
121  {
122  for(Entries::iterator j = i->second.begin(); j != i->second.end(); ++j)
123  {
124  func(i->first, j->first, j->second);
125  }
126  }
127  return func;
128  }
129 
130 private:
131  Dictionary(const Dictionary&) = delete;
132  Dictionary& operator=(const Dictionary&) = delete;
133 };
134 
135 } // namespace tinygettext
136 
137 #endif
138 
139 /* EOF */
Dictionary(const std::string &charset="UTF-8")
Constructs a dictionary converting to the specified charset (default UTF-8)
Definition: dictionary.cpp:40
PluralForms get_plural_forms() const
Definition: dictionary.cpp:67
std::string translate_ctxt_plural(const std::string &msgctxt, const std::string &msgid, const std::string &msgidplural, int num) const
Definition: dictionary.cpp:154
std::string translate_ctxt(const std::string &msgctxt, const std::string &msgid) const
Translate the string msgid that is in context msgctx.
Definition: dictionary.cpp:139
void add_translation(const std::string &msgid, const std::string &msgid_plural, const std::vector< std::string > &msgstrs)
Add a translation from msgid to msgstr to the dictionary, where msgid is the singular form of the mes...
Definition: dictionary.cpp:173
std::string charset
Definition: dictionary.hpp:43
Dictionary * m_fallback
Definition: dictionary.hpp:50
CtxtEntries ctxt_entries
Definition: dictionary.hpp:41
~Dictionary()
Definition: dictionary.cpp:50
std::string translate_plural(const Entries &dict, const std::string &msgid, const std::string &msgidplural, int num) const
Definition: dictionary.cpp:79
void addFallback(Dictionary *fallback)
Definition: dictionary.hpp:109
std::unordered_map< std::string, Entries > CtxtEntries
Definition: dictionary.hpp:40
std::string translate(const Entries &dict, const std::string &msgid) const
Definition: dictionary.cpp:122
Entries entries
Definition: dictionary.hpp:38
void set_plural_forms(const PluralForms &)
Definition: dictionary.cpp:61
std::unordered_map< std::string, std::vector< std::string > > Entries
Definition: dictionary.hpp:37
PluralForms plural_forms
Definition: dictionary.hpp:44
Definition: dictionary.hpp:29
A simple dictionary class that mimics gettext() behaviour.
Definition: dictionary.hpp:34
bool m_has_fallback
Definition: dictionary.hpp:49
Func foreach_ctxt(Func func)
Iterate over all messages with a context, Func is of type: void func(const std::string& ctxt...
Definition: dictionary.hpp:118
Dictionary & operator=(const Dictionary &)=delete
Definition: plural_forms.hpp:29
std::string get_charset() const
Return the charset used for this dictionary.
Definition: dictionary.cpp:55