add PIString::lineNumber() method

add ""_tr literal to translate string by PITranslator
add pip_tr util, now useless, only can generate *.ts
add qt_support internal lib, now only works with *.ts file
pip_vtt migrate to qt_support
This commit is contained in:
2024-11-03 14:39:42 +03:00
parent 9a928f6feb
commit b43158d3a8
14 changed files with 532 additions and 194 deletions

View File

@@ -66,6 +66,11 @@ void PITranslator::loadConfig(const PIString & content) {
for (const auto & s: cn.children())
c->add(s.name(), s.value().toString());
}
auto c = s->createContextInternal("");
for (const auto & s: lang.children()) {
if (s.hasChildren()) continue;
c->add(s.name(), s.value().toString());
}
}

View File

@@ -26,9 +26,10 @@
#ifndef pitranslator_H
#define pitranslator_H
#include "pifile.h"
#include "piiostream.h"
#include "pithread.h"
#include "pistring.h"
#define piTr PITranslator::tr
//! \ingroup Application
//! \~\brief
@@ -37,7 +38,7 @@
class PIP_EXPORT PITranslator {
public:
static PIString tr(const PIString & in, const PIString & context = {});
static PIString tr(const char * in, const PIString & context = {}) { return tr(PIString(in), context); }
static PIString tr(const char * in, const PIString & context = {}) { return tr(PIString::fromUTF8(in), context); }
static void clear();
static void loadLang(const PIString & short_lang);
@@ -61,4 +62,11 @@ private:
PIMap<uint, Context *> contexts;
};
//! \~\brief
//! \~english PIString from UTF-8
//! \~russian PIString из UTF-8
inline PIString operator""_tr(const char * v, size_t sz) {
return PITranslator::tr(PIString::fromUTF8(v, sz));
}
#endif

View File

@@ -99,8 +99,8 @@ public:
//! \~russian Оператор сравнения
bool operator<=(const PIChar & o) const;
//! \~english Returns \b true if symbol is digit ('0' to '9')
//! \~russian Возвращает \b true если символ является
//! \~english Returns \b true if symbol is digit (from '0' to '9')
//! \~russian Возвращает \b true если символ является цифрой (от '0' до '9')
bool isDigit() const;
//! \~english Returns \b true if symbol is HEX digit ('0' to '9', 'a' to 'f', 'A' to 'F')

View File

@@ -1209,6 +1209,17 @@ int PIString::entries(const PIString & str) const {
}
int PIString::lineNumber(int pos) const {
if (isEmpty()) return 0;
if (pos < 0 || pos >= size_s()) pos = size_s() - 1;
int line = 1;
for (int i = 0; i < pos; ++i) {
if (at(i) == '\n') ++line;
}
return line;
}
bool PIString::startsWith(const PIChar c) const {
if (isEmpty()) return false;
return front() == c;
@@ -1736,7 +1747,7 @@ PIString & PIString::setReadableSize(llong bytes) {
static const PIString tr_c = "PIString"_a;
clear();
if (bytes < 1024) {
*this += (PIString::fromNumber(bytes) + " "_a + PITranslator::tr("B", tr_c));
*this += (PIString::fromNumber(bytes) + " "_a + PITranslator::tr("B", "PIString"));
return *this;
}
double fres = bytes;
@@ -1751,13 +1762,13 @@ PIString & PIString::setReadableSize(llong bytes) {
}
return false;
};
if (checkRange(PITranslator::tr("KiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("MiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("GiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("TiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("PiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("EiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("ZiB", tr_c))) return *this;
if (checkRange(PITranslator::tr("KiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("MiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("GiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("TiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("PiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("EiB", "PIString"))) return *this;
if (checkRange(PITranslator::tr("ZiB", "PIString"))) return *this;
checkRange(PITranslator::tr("YiB", tr_c), true);
return *this;
}

View File

@@ -1282,6 +1282,10 @@ public:
//! \endcode
int entries(const PIString & str) const;
//! \~english Returns line number of position "pos". Lines starts from 1.
//! \~russian Возвращает номер строки позиции "pos". Строки начинаются с 1.
int lineNumber(int pos) const;
//! \~english Returns if string starts with "c".
//! \~russian Возвращает начинается ли строка с "c".
bool startsWith(const char c) const { return startsWith(PIChar(c)); }