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\
";