Updated to version 79b7bde of tinygettext
[supertux.git] / external / tinygettext / include / tinygettext / dictionary.hpp
1 //  tinygettext - A gettext replacement that works directly on .po files
2 //  Copyright (C) 2006 Ingo Ruhnke <grumbel@gmx.de>
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 2
7 //  of the License, or (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 #ifndef HEADER_TINYGETTEXT_DICTIONARY_HPP
19 #define HEADER_TINYGETTEXT_DICTIONARY_HPP
20
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24
25 #include "plural_forms.hpp"
26
27 namespace tinygettext {
28
29 /** A simple dictionary class that mimics gettext() behaviour. Each
30     Dictionary only works for a single language, for managing multiple
31     languages and .po files at once use the DictionaryManager. */
32 class Dictionary
33 {
34 private:
35   typedef std::unordered_map<std::string, std::vector<std::string> > Entries;
36   Entries entries;
37
38   typedef std::unordered_map<std::string, Entries> CtxtEntries;
39   CtxtEntries ctxt_entries;
40
41   std::string charset;
42   PluralForms plural_forms;
43
44   std::string translate(const Entries& dict, const std::string& msgid);
45   std::string translate_plural(const Entries& dict, const std::string& msgid, const std::string& msgidplural, int num);
46
47 public:
48   /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */
49   Dictionary(const std::string& charset = "UTF-8");
50   ~Dictionary();
51
52   /** Return the charset used for this dictionary */
53   std::string get_charset() const;
54
55   void set_plural_forms(const PluralForms&);
56   PluralForms get_plural_forms() const;
57
58
59   /** Translate the string \a msgid. */
60   std::string translate(const std::string& msgid);
61
62   /** Translate the string \a msgid to its correct plural form, based
63       on the number of items given by \a num. \a msgid_plural is \a msgid in
64       plural form. */
65   std::string translate_plural(const std::string& msgid, const std::string& msgidplural, int num);
66
67   /** Translate the string \a msgid that is in context \a msgctx. A
68       context is a way to disambiguate msgids that contain the same
69       letters, but different meaning. For example "exit" might mean to
70       quit doing something or it might refer to a door that leads
71       outside (i.e. 'Ausgang' vs 'Beenden' in german) */
72   std::string translate_ctxt(const std::string& msgctxt, const std::string& msgid);
73
74   std::string translate_ctxt_plural(const std::string& msgctxt, const std::string& msgid, const std::string& msgidplural, int num);
75
76   /** Add a translation from \a msgid to \a msgstr to the dictionary,
77       where \a msgid is the singular form of the message, msgid_plural the
78       plural form and msgstrs a table of translations. The right
79       translation will be calculated based on the \a num argument to
80       translate(). */
81   void add_translation(const std::string& msgid, const std::string& msgid_plural,
82                        const std::vector<std::string>& msgstrs);
83   void add_translation(const std::string& msgctxt, 
84                        const std::string& msgid, const std::string& msgid_plural,
85                        const std::vector<std::string>& msgstrs);
86
87   /** Add a translation from \a msgid to \a msgstr to the
88       dictionary */
89   void add_translation(const std::string& msgid, const std::string& msgstr);
90   void add_translation(const std::string& msgctxt, const std::string& msgid, const std::string& msgstr);
91
92   /** Iterate over all messages, Func is of type:
93       void func(const std::string& msgid, const std::vector<std::string>& msgstrs) */
94   template<class Func>
95   Func foreach(Func func) 
96   {
97     for(Entries::iterator i = entries.begin(); i != entries.end(); ++i)
98     {
99       func(i->first, i->second);
100     }
101     return func;
102   }
103
104   /** Iterate over all messages with a context, Func is of type:
105       void func(const std::string& ctxt, const std::string& msgid, const std::vector<std::string>& msgstrs) */
106   template<class Func>
107   Func foreach_ctxt(Func func) 
108   {
109     for(CtxtEntries::iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
110     {
111       for(Entries::iterator j = i->second.begin(); j != i->second.end(); ++j)
112       {
113         func(i->first, j->first, j->second);
114       }
115     }
116     return func;
117   }
118 };
119
120 } // namespace tinygettext
121
122 #endif
123
124 /* EOF */