Add get_all_used_chars to tinygettext

This commit is contained in:
Benau 2020-09-09 13:31:37 +08:00
parent 22fb981fca
commit 767ba27598
5 changed files with 44 additions and 4 deletions

View File

@ -20,6 +20,7 @@
#ifndef HEADER_TINYGETTEXT_DICTIONARY_HPP
#define HEADER_TINYGETTEXT_DICTIONARY_HPP
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
@ -127,6 +128,8 @@ public:
return func;
}
/** Get unique characters (in utf32) used in current dictionary, useful to get for missing characters in font file. */
std::set<unsigned int> get_all_used_chars();
private:
Dictionary(const Dictionary&) = delete;
Dictionary& operator=(const Dictionary&) = delete;

View File

@ -21,6 +21,7 @@
#include "tinygettext/log_stream.hpp"
#include "tinygettext/dictionary.hpp"
#include "../../../src/utils/utf8/unchecked.h"
namespace tinygettext {
@ -241,6 +242,42 @@ Dictionary::add_translation(const std::string& msgctxt, const std::string& msgid
}
}
std::set<unsigned int> Dictionary::get_all_used_chars()
{
std::set<unsigned int> used_chars;
for (Entries::const_iterator i = entries.begin(); i != entries.end(); ++i)
{
const std::vector<std::string>& msgstrs = i->second;
for (unsigned int k = 0; k < msgstrs.size(); ++k)
{
std::vector<unsigned int> strings;
utf8::unchecked::utf8to32(msgstrs[k].c_str(), msgstrs[k].c_str() + msgstrs[k].size(),
back_inserter(strings));
for (unsigned int j = 0; j < strings.size(); ++j)
used_chars.insert(strings[j]);
}
}
for (CtxtEntries::const_iterator i = ctxt_entries.begin(); i != ctxt_entries.end(); ++i)
{
for (Entries::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
{
const std::vector<std::string>& msgstrs = j->second;
for (unsigned int k = 0; k < msgstrs.size(); ++k)
{
std::vector<unsigned int> strings;
utf8::unchecked::utf8to32(msgstrs[k].c_str(), msgstrs[k].c_str() + msgstrs[k].size(),
back_inserter(strings));
for (unsigned int l = 0; l < strings.size(); ++l)
used_chars.insert(strings[l]);
}
}
}
return used_chars;
}
} // namespace tinygettext
/* EOF */

View File

@ -792,10 +792,10 @@ void FontManager::unitTesting()
#endif
translations = new Translations();
Log::setLogLevel(cur_log_level);
std::set<wchar_t> used_chars = translations->getCurrentAllChar();
std::set<unsigned int> used_chars = translations->getCurrentAllChar();
// First FontWithFace is RegularFace
FaceTTF* ttf = m_fonts.front()->getFaceTTF();
for (const wchar_t& c : used_chars)
for (const unsigned int& c : used_chars)
{
// Skip non-printing characters
if (c < 32) continue;

View File

@ -563,7 +563,7 @@ irr::core::stringw Translations::w_ngettext(const char* singular, const char* pl
// ----------------------------------------------------------------------------
#ifndef SERVER_ONLY
std::set<wchar_t> Translations::getCurrentAllChar()
std::set<unsigned int> Translations::getCurrentAllChar()
{
return m_dictionary->get_all_used_chars();
} // getCurrentAllChar

View File

@ -71,7 +71,7 @@ public:
#ifndef SERVER_ONLY
const std::vector<std::string>* getLanguageList() const;
std::set<wchar_t> getCurrentAllChar();
std::set<unsigned int> getCurrentAllChar();
std::string getCurrentLanguageName();