1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// TinyGetText
// Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <iostream>
#include <string.h>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "tinygettext/po_parser.hpp"
#include "tinygettext/tinygettext.hpp"
using namespace tinygettext;
void print_msg(const std::string& msgid, const std::vector<std::string>& msgstrs)
{
std::cout << "Msgid: " << msgid << std::endl;
for(std::vector<std::string>::const_iterator i = msgstrs.begin(); i != msgstrs.end(); ++i)
{
std::cout << *i << std::endl;
}
}
void print_msg_ctxt(const std::string& ctxt, const std::string& msgid, const std::vector<std::string>& msgstrs)
{
std::cout << "Msgctxt: " << ctxt << std::endl;
std::cout << "Msgid: " << msgid << std::endl;
for(std::vector<std::string>::const_iterator i = msgstrs.begin(); i != msgstrs.end(); ++i)
{
std::cout << *i << std::endl;
}
}
void print_usage(int /*argc*/, char** argv)
{
std::cout << "Usage: " << argv[0] << " translate FILE MESSAGE" << std::endl;
std::cout << " " << argv[0] << " translate FILE MESSAGE_S MESSAGE_P NUM" << std::endl;
std::cout << " " << argv[0] << " directory DIRECTORY MESSAGE [LANG]" << std::endl;
std::cout << " " << argv[0] << " language LANGUAGE" << std::endl;
std::cout << " " << argv[0] << " language-dir DIR" << std::endl;
std::cout << " " << argv[0] << " list-msgstrs FILE" << std::endl;
}
void read_dictionary(const std::string& filename, Dictionary& dict)
{
std::ifstream in(filename.c_str());
if (!in)
{
throw std::runtime_error("Couldn't open " + filename);
}
else
{
POParser::parse(filename, in, dict);
in.close();
}
}
int main(int argc, char** argv)
{
try
{
if (argc == 3 && strcmp(argv[1], "language-dir") == 0)
{
DictionaryManager dictionary_manager;
dictionary_manager.add_directory(argv[2]);
const std::set<Language>& languages = dictionary_manager.get_languages();
std::cout << "Number of languages: " << languages.size() << std::endl;
for (std::set<Language>::const_iterator i = languages.begin(); i != languages.end(); ++i)
{
const Language& language = *i;
std::cout << "Env: " << language.str() << std::endl
<< "Name: " << language.get_name() << std::endl
<< "Language: " << language.get_language() << std::endl
<< "Country: " << language.get_country() << std::endl
<< "Modifier: " << language.get_modifier() << std::endl
<< std::endl;
}
}
else if (argc == 3 && strcmp(argv[1], "language") == 0)
{
Language language = Language::from_name(argv[2]);
if (language)
std::cout << "Env: " << language.str() << std::endl
<< "Name: " << language.get_name() << std::endl
<< "Language: " << language.get_language() << std::endl
<< "Country: " << language.get_country() << std::endl
<< "Modifier: " << language.get_modifier() << std::endl;
else
std::cout << "not found" << std::endl;
}
else if (argc == 4 && strcmp(argv[1], "translate") == 0)
{
const char* filename = argv[2];
const char* message = argv[3];
Dictionary dict;
read_dictionary(filename, dict);
std::cout << "TRANSLATION: \"\"\"" << dict.translate(message) << "\"\"\""<< std::endl;
}
else if (argc == 5 && strcmp(argv[1], "translate") == 0)
{
const char* filename = argv[2];
const char* context = argv[3];
const char* message = argv[4];
Dictionary dict;
read_dictionary(filename, dict);
std::cout << dict.translate_ctxt(context, message) << std::endl;
}
else if (argc == 6 && strcmp(argv[1], "translate") == 0)
{
const char* filename = argv[2];
const char* message_singular = argv[3];
const char* message_plural = argv[4];
int num = atoi(argv[5]);
Dictionary dict;
read_dictionary(filename, dict);
std::cout << dict.translate_plural(message_singular, message_plural, num) << std::endl;
}
else if (argc == 7 && strcmp(argv[1], "translate") == 0)
{
const char* filename = argv[2];
const char* context = argv[3];
const char* message_singular = argv[4];
const char* message_plural = argv[5];
int num = atoi(argv[6]);
Dictionary dict;
read_dictionary(filename, dict);
std::cout << dict.translate_ctxt_plural(context, message_singular, message_plural, num) << std::endl;
}
else if ((argc == 4 || argc == 5) && strcmp(argv[1], "directory") == 0)
{
const char* directory = argv[2];
const char* message = argv[3];
const char* language = (argc == 5) ? argv[4] : NULL;
DictionaryManager manager;
manager.add_directory(directory);
if (language)
{
Language lang = Language::from_name(language);
if (lang)
{
manager.set_language(lang);
}
else
{
std::cout << "Unknown language: " << language << std::endl;
exit(EXIT_FAILURE);
}
}
std::cout << "Directory: '" << directory << "'" << std::endl;
std::cout << "Message: '" << message << "'" << std::endl;
std::cout << "Language: '" << manager.get_language().str() << "' (name: '"
<< manager.get_language().get_name() << "', language: '"
<< manager.get_language().get_language() << "', country: '"
<< manager.get_language().get_country() << "', modifier: '"
<< manager.get_language().get_modifier() << "')"
<< std::endl;
std::cout << "Translation: '" << manager.get_dictionary().translate(message) << "'" << std::endl;
}
else if ((argc == 3) && strcmp(argv[1], "list-msgstrs") == 0)
{
const char* filename = argv[2];
Dictionary dict;
read_dictionary(filename, dict);
dict.foreach(print_msg);
dict.foreach_ctxt(print_msg_ctxt);
}
else
{
print_usage(argc, argv);
}
}
catch(std::exception& err)
{
std::cout << "Exception: " << err.what() << std::endl;
}
return 0;
}
/* EOF */
|