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
107 lines
2.7 KiB
C++
107 lines
2.7 KiB
C++
/*
|
|
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());
|
|
}
|
|
auto c = s->createContextInternal("");
|
|
for (const auto & s: lang.children()) {
|
|
if (s.hasChildren()) continue;
|
|
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;
|
|
}
|