add static import of tinygettext
[supertux.git] / external / tinygettext / 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 <map>
22 #include <vector>
23 #include <string>
24 #include "plural_forms.hpp"
25
26 namespace tinygettext {
27
28 /** A simple dictionary class that mimics gettext() behaviour. Each
29     Dictionary only works for a single language, for managing multiple
30     languages and .po files at once use the DictionaryManager. */
31 class Dictionary
32 {
33 private:
34   typedef std::map<std::string, std::vector<std::string> > Entries;
35   Entries entries;
36
37   typedef std::map<std::string, Entries> CtxtEntries;
38   CtxtEntries ctxt_entries;
39
40   std::string charset;
41   PluralForms plural_forms;
42
43   std::string translate(const Entries& dict, const std::string& msgid);
44   std::string translate_plural(const Entries& dict, const std::string& msgid, const std::string& msgidplural, int num);
45
46 public:
47   /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */
48   Dictionary(const std::string& charset = "UTF-8");
49   ~Dictionary();
50
51   /** Return the charset used for this dictionary */
52   std::string get_charset() const;
53
54   void set_plural_forms(const PluralForms&);
55   PluralForms get_plural_forms() const;
56
57
58   /** Translate the string \a msgid. */
59   std::string translate(const std::string& msgid);
60
61   /** Translate the string \a msgid to its correct plural form, based
62       on the number of items given by \a num. \a msgid_plural is \a msgid in
63       plural form. */
64   std::string translate_plural(const std::string& msgid, const std::string& msgidplural, int num);
65
66   /** Translate the string \a msgid that is in context \a msgctx. A
67       context is a way to disambiguate msgids that contain the same
68       letters, but different meaning. For example "exit" might mean to
69       quit doing something or it might refer to a door that leads
70       outside (i.e. 'Ausgang' vs 'Beenden' in german) */
71   std::string translate_ctxt(const std::string& msgctxt, const std::string& msgid);
72
73   std::string translate_ctxt_plural(const std::string& msgctxt, const std::string& msgid, const std::string& msgidplural, int num);
74
75   /** Add a translation from \a msgid to \a msgstr to the dictionary,
76       where \a msgid is the singular form of the message, msgid_plural the
77       plural form and msgstrs a table of translations. The right
78       translation will be calculated based on the \a num argument to
79       translate(). */
80   void add_translation(const std::string& msgid, const std::string& msgid_plural,
81                        const std::vector<std::string>& msgstrs);
82   void add_translation(const std::string& msgctxt, 
83                        const std::string& msgid, const std::string& msgid_plural,
84                        const std::vector<std::string>& msgstrs);
85
86   /** Add a translation from \a msgid to \a msgstr to the
87       dictionary */
88   void add_translation(const std::string& msgid, const std::string& msgstr);
89   void add_translation(const std::string& msgctxt, const std::string& msgid, const std::string& msgstr);
90
91   /** Iterate over all messages, Func is of type:
92       void func(const std::string& msgid, const std::vector<std::string>& msgstrs) */
93   template<class Func>
94   Func foreach(Func func) 
95   {
96     for(Entries::iterator i = entries.begin(); i != entries.end(); ++i)
97     {
98       func(i->first, i->second);
99     }
100     return func;
101   }
102
103   /** Iterate over all messages with a context, Func is of type:
104       void func(const std::string& ctxt, const std::string& msgid, const std::vector<std::string>& msgstrs) */
105   template<class Func>
106   Func foreach_ctxt(Func func) 
107   {
108     for(CtxtEntries::iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
109     {
110       for(Entries::iterator j = i->second.begin(); j != i->second.end(); ++j)
111       {
112         func(i->first, j->first, j->second);
113       }
114     }
115     return func;
116   }
117 };
118
119 } // namespace tinygettext
120
121 #endif
122
123 /* EOF */