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
22 #include <unordered_map>
25 #include "plural_forms.hpp"
27 namespace tinygettext {
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. */
35 typedef std::unordered_map<std::string, std::vector<std::string> > Entries;
38 typedef std::unordered_map<std::string, Entries> CtxtEntries;
39 CtxtEntries ctxt_entries;
42 PluralForms plural_forms;
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);
48 /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */
49 Dictionary(const std::string& charset = "UTF-8");
52 /** Return the charset used for this dictionary */
53 std::string get_charset() const;
55 void set_plural_forms(const PluralForms&);
56 PluralForms get_plural_forms() const;
59 /** Translate the string \a msgid. */
60 std::string translate(const std::string& msgid);
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
65 std::string translate_plural(const std::string& msgid, const std::string& msgidplural, int num);
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);
74 std::string translate_ctxt_plural(const std::string& msgctxt, const std::string& msgid, const std::string& msgidplural, int num);
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
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);
87 /** Add a translation from \a msgid to \a msgstr to the
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);
92 /** Iterate over all messages, Func is of type:
93 void func(const std::string& msgid, const std::vector<std::string>& msgstrs) */
95 Func foreach(Func func)
97 for(Entries::iterator i = entries.begin(); i != entries.end(); ++i)
99 func(i->first, i->second);
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) */
107 Func foreach_ctxt(Func func)
109 for(CtxtEntries::iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
111 for(Entries::iterator j = i->second.begin(); j != i->second.end(); ++j)
113 func(i->first, j->first, j->second);
120 } // namespace tinygettext