3 // TinyGetText - A small flexible gettext() replacement
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifndef HEADER_TINYGETTEXT_H
20 #define HEADER_TINYGETTEXT_H
27 namespace TinyGetText {
29 typedef int (*PluralFunc)(int n);
37 LanguageDef(const char* code_, const char* name_, int nplural_, PluralFunc plural_)
38 : code(code_), name(name_), nplural(nplural_), plural(plural_)
42 /** A simple dictionary class that mimics gettext() behaviour. Each
43 Dictionary only works for a single language, for managing multiple
44 languages and .po files at once use the DictionaryManager. */
48 typedef std::map<std::string, std::string> Entries;
51 typedef std::map<std::string, std::map<int, std::string> > PluralEntries;
52 PluralEntries plural_entries;
58 Dictionary(const LanguageDef& language_, const std::string& charset = "");
62 /** Return the charset used for this dictionary */
63 std::string get_charset() const;
65 /** Set a charset for this dictionary, this will NOT convert stuff,
66 it is for information only, you have to convert stuff yourself
67 when you add it with \a add_translation() */
68 void set_charset(const std::string& charset);
70 /** Set the language that is used for this dictionary, this is
71 mainly needed to evaluate plural forms */
72 void set_language(const LanguageDef& lang);
74 /** Translate the string \a msgid to its correct plural form, based
75 on the number of items given by \a num. \a msgid2 is \a msgid in
77 std::string translate(const std::string& msgid, const std::string& msgid2, int num);
79 /** Translate the string \a msgid. */
80 std::string translate(const std::string& msgid);
82 /** Add a translation from \a msgid to \a msgstr to the dictionary,
83 where \a msgid is the singular form of the message, msgid2 the
84 plural form and msgstrs a table of translations. The right
85 translation will be calculated based on the \a num argument to
87 void add_translation(const std::string& msgid, const std::string& msgid2,
88 const std::map<int, std::string>& msgstrs);
90 /** Add a translation from \a msgid to \a msgstr to the
92 void add_translation(const std::string& msgid, const std::string& msgstr);
95 /** Manager class for dictionaries, you give it a bunch of directories
96 with .po files and it will then automatically load the right file
97 on demand depending on which language was set. */
98 class DictionaryManager
101 typedef std::map<std::string, Dictionary> Dictionaries;
102 Dictionaries dictionaries;
103 typedef std::vector<std::string> SearchPath;
104 SearchPath search_path;
105 typedef std::map<std::string, std::string> Aliases;
106 Aliases language_aliases;
107 std::string language;
108 Dictionary* current_dict;
109 Dictionary empty_dict;
114 /** Return the currently active dictionary, if none is set, an empty
115 dictionary is returned. */
116 Dictionary& get_dictionary()
117 { return *current_dict; }
119 /** Get dictionary for lang */
120 Dictionary& get_dictionary(const std::string& langspec);
122 /** Set a language based on a four? letter country code */
123 void set_language(const std::string& langspec);
125 /** Define an alias for a language */
126 void set_language_alias(const std::string& alias, const std::string& lang);
128 /** Add a directory to the search path for dictionaries */
129 void add_directory(const std::string& pathname);
131 /** Return a set of the available languages in their country code */
132 std::set<std::string> get_languages();
135 void parseLocaleAliases();
136 /// returns the language part in a language spec (like de_DE.UTF-8 -> de)
137 std::string get_language_from_spec(const std::string& spec);
140 /** Read the content of the .po file given as \a in into the
141 dictionary given as \a dict */
142 void read_po_file(Dictionary& dict, std::istream& in);
143 LanguageDef& get_language_def(const std::string& name);
145 } // namespace TinyGetText