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.
19 #include "log_stream.hpp"
20 #include "dictionary.hpp"
22 namespace tinygettext {
24 Dictionary::Dictionary(const std::string& charset_) :
32 Dictionary::~Dictionary()
37 Dictionary::get_charset() const
43 Dictionary::set_plural_forms(const PluralForms& plural_forms_)
45 plural_forms = plural_forms_;
49 Dictionary::get_plural_forms() const
55 Dictionary::translate_plural(const std::string& msgid, const std::string& msgid_plural, int num)
57 return translate_plural(entries, msgid, msgid_plural, num);
61 Dictionary::translate_plural(const Entries& dict, const std::string& msgid, const std::string& msgid_plural, int count)
63 Entries::const_iterator i = dict.find(msgid);
64 const std::vector<std::string>& msgstrs = i->second;
69 n = plural_forms.get_plural(count);
70 assert(/*n >= 0 &&*/ n < msgstrs.size());
72 if (!msgstrs[n].empty())
75 if (count == 1) // default to english rules
82 log_info << "Couldn't translate: " << msgid << std::endl;
83 log_info << "Candidates: " << std::endl;
84 for (i = dict.begin(); i != dict.end(); ++i)
85 log_info << "'" << i->first << "'" << std::endl;
87 if (count == 1) // default to english rules
95 Dictionary::translate(const std::string& msgid)
97 return translate(entries, msgid);
101 Dictionary::translate(const Entries& dict, const std::string& msgid)
103 Entries::const_iterator i = dict.find(msgid);
104 if (i != dict.end() && !i->second.empty())
110 log_info << "Couldn't translate: " << msgid << std::endl;
116 Dictionary::translate_ctxt(const std::string& msgctxt, const std::string& msgid)
118 CtxtEntries::iterator i = ctxt_entries.find(msgctxt);
119 if (i != ctxt_entries.end())
121 return translate(i->second, msgid);
125 log_info << "Couldn't translate: " << msgid << std::endl;
131 Dictionary::translate_ctxt_plural(const std::string& msgctxt,
132 const std::string& msgid, const std::string& msgidplural, int num)
134 CtxtEntries::iterator i = ctxt_entries.find(msgctxt);
135 if (i != ctxt_entries.end())
137 return translate_plural(i->second, msgid, msgidplural, num);
141 log_info << "Couldn't translate: " << msgid << std::endl;
142 if (num != 1) // default to english
150 Dictionary::add_translation(const std::string& msgid, const std::string& ,
151 const std::vector<std::string>& msgstrs)
153 // Do we need msgid2 for anything? its after all supplied to the
154 // translate call, so we just throw it away here
155 entries[msgid] = msgstrs;
159 Dictionary::add_translation(const std::string& msgid, const std::string& msgstr)
161 std::vector<std::string>& vec = entries[msgid];
164 vec.push_back(msgstr);
168 log_warning << "collision in add_translation: '"
169 << msgid << "' -> '" << msgstr << "' vs '" << vec[0] << "'" << std::endl;
175 Dictionary::add_translation(const std::string& msgctxt,
176 const std::string& msgid, const std::string& msgid_plural,
177 const std::vector<std::string>& msgstrs)
179 std::vector<std::string>& vec = ctxt_entries[msgctxt][msgid];
186 log_warning << "collision in add_translation(\"" << msgctxt << "\", \"" << msgid << "\", \"" << msgid_plural << "\")" << std::endl;
192 Dictionary::add_translation(const std::string& msgctxt, const std::string& msgid, const std::string& msgstr)
194 std::vector<std::string>& vec = ctxt_entries[msgctxt][msgid];
197 vec.push_back(msgstr);
201 log_warning << "collision in add_translation(\"" << msgctxt << "\", \"" << msgid << "\")" << std::endl;
206 } // namespace tinygettext