tinygettext_test.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // tinygettext - A gettext replacement that works directly on .po files
  2. // Copyright (c) 2009 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. //
  8. // Permission is granted to anyone to use this software for any purpose,
  9. // including commercial applications, and to alter it and redistribute it
  10. // freely, subject to the following restrictions:
  11. //
  12. // 1. The origin of this software must not be misrepresented; you must not
  13. // claim that you wrote the original software. If you use this software
  14. // in a product, an acknowledgement in the product documentation would be
  15. // appreciated but is not required.
  16. // 2. Altered source versions must be plainly marked as such, and must not be
  17. // misrepresented as being the original software.
  18. // 3. This notice may not be removed or altered from any source distribution.
  19. #include <iostream>
  20. #include <string.h>
  21. #include <fstream>
  22. #include <stdlib.h>
  23. #include <iostream>
  24. #include <stdexcept>
  25. #include "tinygettext/po_parser.hpp"
  26. #include "tinygettext/tinygettext.hpp"
  27. #include "tinygettext/unix_file_system.hpp"
  28. using namespace tinygettext;
  29. namespace {
  30. void print_msg(const std::string& msgid, const std::vector<std::string>& msgstrs)
  31. {
  32. std::cout << "Msgid: " << msgid << std::endl;
  33. for(std::vector<std::string>::const_iterator i = msgstrs.begin(); i != msgstrs.end(); ++i)
  34. {
  35. std::cout << *i << std::endl;
  36. }
  37. }
  38. void print_msg_ctxt(const std::string& ctxt, const std::string& msgid, const std::vector<std::string>& msgstrs)
  39. {
  40. std::cout << "Msgctxt: " << ctxt << std::endl;
  41. std::cout << "Msgid: " << msgid << std::endl;
  42. for(std::vector<std::string>::const_iterator i = msgstrs.begin(); i != msgstrs.end(); ++i)
  43. {
  44. std::cout << *i << std::endl;
  45. }
  46. }
  47. void print_usage(int /*argc*/, char** argv)
  48. {
  49. std::cout << "Usage: " << argv[0] << " translate FILE MESSAGE" << std::endl;
  50. std::cout << " " << argv[0] << " translate FILE MESSAGE_S MESSAGE_P NUM" << std::endl;
  51. std::cout << " " << argv[0] << " directory DIRECTORY MESSAGE [LANG]" << std::endl;
  52. std::cout << " " << argv[0] << " language LANGUAGE" << std::endl;
  53. std::cout << " " << argv[0] << " language-dir DIR" << std::endl;
  54. std::cout << " " << argv[0] << " list-msgstrs FILE" << std::endl;
  55. }
  56. void read_dictionary(const std::string& filename, Dictionary& dict)
  57. {
  58. std::ifstream in(filename.c_str());
  59. if (!in)
  60. {
  61. throw std::runtime_error("Couldn't open " + filename);
  62. }
  63. else
  64. {
  65. POParser::parse(filename, in, dict);
  66. in.close();
  67. }
  68. }
  69. } // namespace
  70. int main(int argc, char** argv)
  71. {
  72. try
  73. {
  74. if (argc == 3 && strcmp(argv[1], "language-dir") == 0)
  75. {
  76. DictionaryManager dictionary_manager(std::unique_ptr<tinygettext::FileSystem>(new UnixFileSystem));
  77. dictionary_manager.add_directory(argv[2]);
  78. const std::set<Language>& languages = dictionary_manager.get_languages();
  79. std::cout << "Number of languages: " << languages.size() << std::endl;
  80. for (std::set<Language>::const_iterator i = languages.begin(); i != languages.end(); ++i)
  81. {
  82. const Language& language = *i;
  83. std::cout << "Env: " << language.str() << std::endl
  84. << "Name: " << language.get_name() << std::endl
  85. << "Language: " << language.get_language() << std::endl
  86. << "Country: " << language.get_country() << std::endl
  87. << "Modifier: " << language.get_modifier() << std::endl
  88. << std::endl;
  89. }
  90. }
  91. else if (argc == 3 && strcmp(argv[1], "language") == 0)
  92. {
  93. const char* language_cstr = argv[2];
  94. Language language = Language::from_name(language_cstr);
  95. if (language)
  96. std::cout << "Env: " << language.str() << std::endl
  97. << "Name: " << language.get_name() << std::endl
  98. << "Language: " << language.get_language() << std::endl
  99. << "Country: " << language.get_country() << std::endl
  100. << "Modifier: " << language.get_modifier() << std::endl;
  101. else
  102. std::cout << "not found" << std::endl;
  103. }
  104. else if (argc == 4 && strcmp(argv[1], "translate") == 0)
  105. {
  106. const char* filename = argv[2];
  107. const char* message = argv[3];
  108. Dictionary dict;
  109. read_dictionary(filename, dict);
  110. std::cout << "TRANSLATION: \"\"\"" << dict.translate(message) << "\"\"\""<< std::endl;
  111. }
  112. else if (argc == 5 && strcmp(argv[1], "translate") == 0)
  113. {
  114. const char* filename = argv[2];
  115. const char* context = argv[3];
  116. const char* message = argv[4];
  117. Dictionary dict;
  118. read_dictionary(filename, dict);
  119. std::cout << dict.translate_ctxt(context, message) << std::endl;
  120. }
  121. else if (argc == 6 && strcmp(argv[1], "translate") == 0)
  122. {
  123. const char* filename = argv[2];
  124. const char* message_singular = argv[3];
  125. const char* message_plural = argv[4];
  126. int num = atoi(argv[5]);
  127. Dictionary dict;
  128. read_dictionary(filename, dict);
  129. std::cout << dict.translate_plural(message_singular, message_plural, num) << std::endl;
  130. }
  131. else if (argc == 7 && strcmp(argv[1], "translate") == 0)
  132. {
  133. const char* filename = argv[2];
  134. const char* context = argv[3];
  135. const char* message_singular = argv[4];
  136. const char* message_plural = argv[5];
  137. int num = atoi(argv[6]);
  138. Dictionary dict;
  139. read_dictionary(filename, dict);
  140. std::cout << dict.translate_ctxt_plural(context, message_singular, message_plural, num) << std::endl;
  141. }
  142. else if ((argc == 4 || argc == 5) && strcmp(argv[1], "directory") == 0)
  143. {
  144. const char* directory = argv[2];
  145. const char* message = argv[3];
  146. const char* language = (argc == 5) ? argv[4] : nullptr;
  147. DictionaryManager manager(std::unique_ptr<tinygettext::FileSystem>(new UnixFileSystem));
  148. manager.add_directory(directory);
  149. if (language)
  150. {
  151. Language lang = Language::from_name(language);
  152. if (lang)
  153. {
  154. manager.set_language(lang);
  155. }
  156. else
  157. {
  158. std::cout << "Unknown language: " << language << std::endl;
  159. exit(EXIT_FAILURE);
  160. }
  161. }
  162. std::cout << "Directory: '" << directory << "'" << std::endl;
  163. std::cout << "Message: '" << message << "'" << std::endl;
  164. std::cout << "Language: '" << manager.get_language().str() << "' (name: '"
  165. << manager.get_language().get_name() << "', language: '"
  166. << manager.get_language().get_language() << "', country: '"
  167. << manager.get_language().get_country() << "', modifier: '"
  168. << manager.get_language().get_modifier() << "')"
  169. << std::endl;
  170. std::cout << "Translation: '" << manager.get_dictionary().translate(message) << "'" << std::endl;
  171. }
  172. else if ((argc == 3) && strcmp(argv[1], "list-msgstrs") == 0)
  173. {
  174. const char* filename = argv[2];
  175. Dictionary dict;
  176. read_dictionary(filename, dict);
  177. dict.foreach(print_msg);
  178. dict.foreach_ctxt(print_msg_ctxt);
  179. }
  180. else
  181. {
  182. print_usage(argc, argv);
  183. }
  184. }
  185. catch(std::exception& err)
  186. {
  187. std::cout << "Exception: " << err.what() << std::endl;
  188. }
  189. return 0;
  190. }
  191. /* EOF */