00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef HEADER_TINYGETTEXT_H
00021 #define HEADER_TINYGETTEXT_H
00022
00023 #include <map>
00024 #include <vector>
00025 #include <set>
00026 #include <string>
00027
00028 namespace TinyGetText {
00029
00030 typedef int (*PluralFunc)(int n);
00031
00032 struct LanguageDef {
00033 const char* code;
00034 const char* name;
00035 int nplural;
00036 PluralFunc plural;
00037
00038 LanguageDef(const char* code_, const char* name_, int nplural_, PluralFunc plural_)
00039 : code(code_), name(name_), nplural(nplural_), plural(plural_)
00040 {}
00041 };
00042
00046 class Dictionary
00047 {
00048 private:
00049 typedef std::map<std::string, std::string> Entries;
00050 Entries entries;
00051
00052 typedef std::map<std::string, std::map<int, std::string> > PluralEntries;
00053 PluralEntries plural_entries;
00054
00055 LanguageDef language;
00056 std::string charset;
00057 public:
00059 Dictionary(const LanguageDef& language_, const std::string& charset = "");
00060
00061 Dictionary();
00062
00064 std::string get_charset() const;
00065
00069 void set_charset(const std::string& charset);
00070
00073 void set_language(const LanguageDef& lang);
00074
00078 std::string translate(const std::string& msgid, const std::string& msgid2, int num);
00079
00081 std::string translate(const std::string& msgid);
00083 const char* translate(const char* msgid);
00084
00090 void add_translation(const std::string& msgid, const std::string& msgid2,
00091 const std::map<int, std::string>& msgstrs);
00092
00095 void add_translation(const std::string& msgid, const std::string& msgstr);
00096 };
00097
00101 class DictionaryManager
00102 {
00103 private:
00104 typedef std::map<std::string, Dictionary> Dictionaries;
00105 Dictionaries dictionaries;
00106 typedef std::vector<std::string> SearchPath;
00107 SearchPath search_path;
00108 typedef std::map<std::string, std::string> Aliases;
00109 Aliases language_aliases;
00110 std::string charset;
00111 std::string language;
00112 Dictionary* current_dict;
00113 Dictionary empty_dict;
00114
00115 public:
00116 DictionaryManager();
00117
00120 Dictionary& get_dictionary()
00121 { return *current_dict; }
00122
00124 Dictionary& get_dictionary(const std::string& langspec);
00125
00127 void set_language(const std::string& langspec);
00128
00130 const std::string& get_language() const;
00131
00133 void set_charset(const std::string& charset);
00134
00136 void set_language_alias(const std::string& alias, const std::string& lang);
00137
00139 void add_directory(const std::string& pathname);
00140
00142 std::set<std::string> get_languages();
00143
00144 private:
00145 void parseLocaleAliases();
00147 std::string get_language_from_spec(const std::string& spec);
00148 };
00149
00152 void read_po_file(Dictionary& dict, std::istream& in);
00153 LanguageDef& get_language_def(const std::string& name);
00154
00155 }
00156
00157 #endif
00158
00159