annotate templates/dictionary.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 #include "dictionary.h"
ferencd@0 2
ferencd@0 3 #include <tntdb.h>
ferencd@0 4 #include <boost/algorithm/string.hpp>
ferencd@0 5
ferencd@0 6 const std::vector<std::string> dictionary::supported_languages = {"gb", "no"};
ferencd@0 7 std::map<std::string, std::map<std::string, std::string>> dictionary::m_inMemoryTranslations;
ferencd@0 8
ferencd@0 9 template<typename T>
ferencd@0 10 std::string join(const std::vector<T> in,
ferencd@0 11 const std::string & separator =", ", // see 1.
ferencd@0 12 const std::string & concluder =" ") // see 1.
ferencd@0 13 {
ferencd@0 14 std::ostringstream ss;
ferencd@0 15 std::size_t i = 0;
ferencd@0 16 for(; i<in.size() - 1; i++)
ferencd@0 17 {
ferencd@0 18 ss << in[i] << separator;
ferencd@0 19 }
ferencd@0 20
ferencd@0 21 ss << in[i] << concluder;
ferencd@0 22 return ss.str();
ferencd@0 23 }
ferencd@0 24
ferencd@0 25 std::string dictionary::translate(const std::string & what, const std::string &target_language, bool other_languages_too, std::map<std::string, std::string> &translations)
ferencd@0 26 {
ferencd@0 27 std::string result_translation;
ferencd@0 28 bool found = false;
ferencd@0 29
ferencd@0 30 try {
ferencd@0 31 tntdb::Connection conn = tntdb::connect("sqlite:lang.db");
ferencd@0 32
ferencd@0 33 // TODO: prepared statement
ferencd@0 34
ferencd@0 35 std::string v = "select " + join(supported_languages) + " from translations where source='" + what + "'";
ferencd@0 36 tntdb::Result result = conn.select(v);
ferencd@0 37 for (tntdb::Result::const_iterator it = result.begin(); it != result.end(); ++it)
ferencd@0 38 {
ferencd@0 39 tntdb::Row row = *it;
ferencd@0 40
ferencd@0 41 for(size_t i=0; i<row.size(); i++)
ferencd@0 42 {
ferencd@0 43 std::string row_name = boost::to_lower_copy(row.getName(i));
ferencd@0 44
ferencd@0 45 if(other_languages_too)
ferencd@0 46 {
ferencd@0 47 std::string temp;
ferencd@0 48 row[i].get(temp);
ferencd@0 49 translations[row_name] = temp;
ferencd@0 50 }
ferencd@0 51
ferencd@0 52 if(row_name == boost::to_lower_copy(target_language))
ferencd@0 53 {
ferencd@0 54 row[i].get(result_translation);
ferencd@0 55 found = true;
ferencd@0 56 }
ferencd@0 57 }
ferencd@0 58 }
ferencd@0 59
ferencd@0 60 }
ferencd@0 61 catch (std::exception& ex)
ferencd@0 62 {
ferencd@0 63 std::cerr << ex.what();
ferencd@0 64 return "";
ferencd@0 65 }
ferencd@0 66
ferencd@0 67 if(!found)
ferencd@0 68 {
ferencd@0 69 if(m_inMemoryTranslations.count(what))
ferencd@0 70 {
ferencd@0 71 for(const auto& l : supported_languages)
ferencd@0 72 {
ferencd@0 73 if(m_inMemoryTranslations[what].count(l))
ferencd@0 74 {
ferencd@0 75 translations[l] = m_inMemoryTranslations[what][l];
ferencd@0 76 }
ferencd@0 77 }
ferencd@0 78 if(m_inMemoryTranslations[what].count(target_language))
ferencd@0 79 {
ferencd@0 80 result_translation = m_inMemoryTranslations[what][target_language];
ferencd@0 81 }
ferencd@0 82 }
ferencd@0 83 }
ferencd@0 84
ferencd@0 85 return result_translation;
ferencd@0 86 }
ferencd@0 87
ferencd@0 88 std::string dictionary::translate(const std::string &what, const std::string &target_language)
ferencd@0 89 {
ferencd@0 90 std::map<std::string, std::string> translations;
ferencd@0 91 return translate(what, target_language, false, translations);
ferencd@0 92 }
ferencd@0 93
ferencd@0 94 void dictionary::add_translation(const std::string &key, const std::string &language, const std::string &translated)
ferencd@0 95 {
ferencd@0 96 m_inMemoryTranslations[key][language] = translated;
ferencd@0 97 }