1 // tinygettext - A gettext replacement that works directly on .po files
2 // Copyright (C) 2006 Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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.
18 #ifndef HEADER_TINYGETTEXT_DICTIONARY_HPP
19 #define HEADER_TINYGETTEXT_DICTIONARY_HPP
24 #include "plural_forms.hpp"
26 namespace tinygettext {
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. */
34 typedef std::map<std::string, std::vector<std::string> > Entries;
37 typedef std::map<std::string, Entries> CtxtEntries;
38 CtxtEntries ctxt_entries;
41 PluralForms plural_forms;
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);
47 /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */
48 Dictionary(const std::string& charset = "UTF-8");
51 /** Return the charset used for this dictionary */
52 std::string get_charset() const;
54 void set_plural_forms(const PluralForms&);
55 PluralForms get_plural_forms() const;
58 /** Translate the string \a msgid. */
59 std::string translate(const std::string& msgid);
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
64 std::string translate_plural(const std::string& msgid, const std::string& msgidplural, int num);
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);
73 std::string translate_ctxt_plural(const std::string& msgctxt, const std::string& msgid, const std::string& msgidplural, int num);
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
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);
86 /** Add a translation from \a msgid to \a msgstr to the
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);
91 /** Iterate over all messages, Func is of type:
92 void func(const std::string& msgid, const std::vector<std::string>& msgstrs) */
94 Func foreach(Func func)
96 for(Entries::iterator i = entries.begin(); i != entries.end(); ++i)
98 func(i->first, i->second);
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) */
106 Func foreach_ctxt(Func func)
108 for(CtxtEntries::iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
110 for(Entries::iterator j = i->second.begin(); j != i->second.end(); ++j)
112 func(i->first, j->first, j->second);
119 } // namespace tinygettext