add PITranslator

begin localization "ru"
This commit is contained in:
2024-11-02 18:43:30 +03:00
parent df75efe881
commit 9a928f6feb
14 changed files with 305 additions and 407 deletions

View File

@@ -55,5 +55,6 @@
#include "pilog.h"
#include "pisingleapplication.h"
#include "pisystemmonitor.h"
#include "pitranslator.h"
#endif

View File

@@ -0,0 +1,101 @@
/*
PIP - Platform Independent Primitives
Translation support
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pitranslator.h"
#include "pivaluetree_conversions.h"
#include "tr/pip_all.h"
//! \class PITranslator pitranslator.h
//! \details
//! \~english \section PITranslator_sec0 Synopsis
//! \~russian \section PITranslator_sec0 Краткий обзор
//! \~english
//!
//! \~russian
//!
PIString PITranslator::tr(const PIString & in, const PIString & context) {
auto s = instance();
auto c = s->contexts.value(context.hash());
if (!c) return in;
return c->strings.value(in.hash(), in);
}
void PITranslator::clear() {
instance()->clearInternal();
}
void PITranslator::loadLang(const PIString & short_lang) {
auto s = instance();
auto vt = PIValueTreeConversions::fromText(getBuiltinConfig());
auto lang = vt.child(short_lang.toLowerCase().trim());
for (const auto & cn: lang.children()) {
auto c = s->createContextInternal(cn.name());
for (const auto & s: cn.children())
c->add(s.name(), s.value().toString());
}
}
void PITranslator::loadConfig(const PIString & content) {
auto s = instance();
auto lang = PIValueTreeConversions::fromText(content);
for (const auto & cn: lang.children()) {
auto c = s->createContextInternal(cn.name());
for (const auto & s: cn.children())
c->add(s.name(), s.value().toString());
}
}
PITranslator::PITranslator() {}
PITranslator::~PITranslator() {
clearInternal();
}
PITranslator * PITranslator::instance() {
static PITranslator ret;
return &ret;
}
void PITranslator::clearInternal() {
piDeleteAll(contexts.values());
contexts.clear();
}
PITranslator::Context * PITranslator::createContextInternal(const PIString & context) {
auto & ret(contexts[context.hash()]);
if (!ret) ret = new Context();
return ret;
}
void PITranslator::Context::add(const PIString & in, const PIString & out) {
strings[in.hash()] = out;
}

View File

@@ -0,0 +1,64 @@
/*! \file pitranslator.h
* \ingroup Application
* \~\brief
* \~english Translation support
* \~russian Поддержка перевода
*/
/*
PIP - Platform Independent Primitives
Translation support
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef pitranslator_H
#define pitranslator_H
#include "pifile.h"
#include "piiostream.h"
#include "pithread.h"
//! \ingroup Application
//! \~\brief
//! \~english Translation support
//! \~russian Поддержка перевода
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 void clear();
static void loadLang(const PIString & short_lang);
static void loadConfig(const PIString & content);
private:
PITranslator();
~PITranslator();
NO_COPY_CLASS(PITranslator)
struct Context {
void add(const PIString & in, const PIString & out);
PIMap<uint, PIString> strings;
};
static PITranslator * instance();
void clearInternal();
Context * createContextInternal(const PIString & context);
PIMap<uint, Context *> contexts;
};
#endif

View File

@@ -0,0 +1,16 @@
#ifndef pitr_pip_all_H
#define pitr_pip_all_H
#include "pip_ru.h"
#include "pistring.h"
PIString getBuiltinConfig() {
// clang-format off
static const PIString ret =
PIString::fromUTF8(config_ru)
;
// clang-format on
return ret;
}
#endif

View File

@@ -0,0 +1,25 @@
constexpr char config_ru[] = "\
[ru.PIString] \n\
B = Б \n\
KiB = KиБ \n\
MiB = MиБ \n\
GiB = ГиБ \n\
TiB = ТиБ \n\
PiB = ПиБ \n\
EiB = ЭиБ \n\
ZiB = ЗиБ \n\
YiB = ЙиБ \n\
\n\
[ru.PIDiag] \n\
/s = /сек \n\
\n\
[ru.PITime] \n\
Hz = Гц \n\
KHz = КГц \n\
kHz = кГц \n\
MHz = МГц \n\
GHz = ГГц \n\
THz = ТГц \n\
PHz = ПГц \n\
\n\
";

View File

@@ -340,6 +340,13 @@ bool PIInit::isBuildOptionEnabled(PIInit::BuildOption o) {
true;
# else
false;
# endif
case boConsole:
return
# ifdef PIP_CONSOLE
true;
# else
false;
# endif
default: return false;
}
@@ -357,6 +364,7 @@ PIStringList PIInit::buildOptions() {
if (isBuildOptionEnabled(boCompress)) ret << "Compress";
if (isBuildOptionEnabled(boOpenCL)) ret << "OpenCL";
if (isBuildOptionEnabled(boCloud)) ret << "Cloud";
if (isBuildOptionEnabled(boConsole)) ret << "Console";
return ret;
}

View File

@@ -67,6 +67,7 @@ public:
boCompress /*! \~english Zlib compression support \~russian Поддержка сжатия Zlib */ = 0x80,
boOpenCL /*! \~english OpenCL support \~russian Поддержка OpenCL */ = 0x100,
boCloud /*! \~english PICloud transport support \~russian Поддержка облачного транспорта PICloud */ = 0x200,
boConsole /*! \~english Console graphics support \~russian Поддержка графики в консоли */ = 0x400,
};
static PIInit * instance() { return __PIInit_Initializer__::__instance__; }

View File

@@ -20,6 +20,7 @@
#include "pidiagnostics.h"
#include "piliterals_time.h"
#include "pitranslator.h"
/** \class PIDiagnostics
@@ -40,7 +41,7 @@
PIDiagnostics::State::State() {
receive_speed = send_speed = PIString::readableSize(0) + "/s";
receive_speed = send_speed = PIString::readableSize(0) + PITranslator::tr("/s", "PIDiag");
}
@@ -152,14 +153,15 @@ void PIDiagnostics::sended(int size) {
void PIDiagnostics::tick(int) {
mutex_state.lock();
auto speed_sec = PITranslator::tr("/s", "PIDiag");
// piCoutObj << "lock";
int tcnt_recv = 0;
int tcnt_send = 0;
Entry send = calcHistory(history_send, tcnt_send);
Entry recv = calcHistory(history_rec, tcnt_recv);
float itr = disconn_.toSeconds() * (float(tcnt_recv) / history_rec.size());
float its = disconn_.toSeconds() * (float(tcnt_send) / history_send.size());
float hz = 1. / interval().toSeconds();
int tcnt_recv = 0;
int tcnt_send = 0;
Entry send = calcHistory(history_send, tcnt_send);
Entry recv = calcHistory(history_rec, tcnt_recv);
float itr = disconn_.toSeconds() * (float(tcnt_recv) / history_rec.size());
float its = disconn_.toSeconds() * (float(tcnt_send) / history_send.size());
float hz = 1. / interval().toSeconds();
if (tcnt_recv == 0) {
cur_state.integral_freq = cur_state.immediate_freq = 0;
cur_state.received_packets_per_sec = cur_state.received_bytes_per_sec = 0;
@@ -176,8 +178,8 @@ void PIDiagnostics::tick(int) {
cur_state.sended_bytes_per_sec = ullong(double(send.bytes_ok) / its);
}
// piCoutObj << "tick" << recv.cnt_ok << send.cnt_ok;
cur_state.receive_speed = PIString::readableSize(cur_state.received_bytes_per_sec) + "/s";
cur_state.send_speed = PIString::readableSize(cur_state.sended_bytes_per_sec) + "/s";
cur_state.receive_speed = PIString::readableSize(cur_state.received_bytes_per_sec) + speed_sec;
cur_state.send_speed = PIString::readableSize(cur_state.sended_bytes_per_sec) + speed_sec;
int arc = recv.cnt_ok + recv.cnt_fail;
float good_percents = 0.f;
if (arc > 0) good_percents = (float)recv.cnt_ok / arc * 100.f;

View File

@@ -23,6 +23,7 @@
#include "piliterals.h"
#include "pimathbase.h"
#include "pistringlist.h"
#include "pitranslator.h"
#ifdef PIP_ICU
# define U_NOEXCEPT
# include "unicode/ucnv.h"
@@ -1732,43 +1733,32 @@ ldouble PIString::toLDouble() const {
//! piCout << s; // 47.6 GiB
//! \endcode
PIString & PIString::setReadableSize(llong bytes) {
static const PIString tr_c = "PIString"_a;
clear();
if (bytes < 1024) {
*this += (PIString::fromNumber(bytes) + " B"_a);
*this += (PIString::fromNumber(bytes) + " "_a + PITranslator::tr("B", tr_c));
return *this;
}
double fres = bytes / 1024.;
llong res = bytes / 1024;
fres -= res;
if (res < 1024) {
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " KiB"_a);
return *this;
}
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024) {
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " MiB"_a);
return *this;
}
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024) {
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " GiB"_a);
return *this;
}
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024) {
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " TiB"_a);
return *this;
}
fres = res / 1024.;
res /= 1024;
fres -= res;
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " PiB"_a);
double fres = bytes;
llong res = bytes;
auto checkRange = [this, &fres, &res](const PIString & unit, bool last = false) {
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024 || last) {
*this += (PIString::fromNumber(res) + "."_a + PIString::fromNumber(llong(fres * 10)).left(1) + " "_a + unit);
return true;
}
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;
checkRange(PITranslator::tr("YiB", tr_c), true);
return *this;
}