first release of translation facility
* runtime - loading and translating * design-time - works with *.ts file (pip_tr utility) * compile-time - CMake macro for compile *.ts
This commit is contained in:
@@ -19,8 +19,11 @@
|
||||
|
||||
#include "pitranslator.h"
|
||||
|
||||
#include "pidir.h"
|
||||
#include "pifile.h"
|
||||
#include "piliterals_string.h"
|
||||
#include "pitranslator_p.h"
|
||||
#include "pivaluetree_conversions.h"
|
||||
#include "tr/pip_all.h"
|
||||
|
||||
|
||||
//! \class PITranslator pitranslator.h
|
||||
@@ -33,28 +36,38 @@
|
||||
//!
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PITranslator)
|
||||
PITranslatorPrivate::Translation content;
|
||||
PRIVATE_DEFINITION_END(PITranslator)
|
||||
|
||||
|
||||
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);
|
||||
return instance()->PRIVATEWB->content.translate(in, context);
|
||||
}
|
||||
|
||||
|
||||
void PITranslator::clear() {
|
||||
instance()->clearInternal();
|
||||
instance()->PRIVATEWB->content.clear();
|
||||
}
|
||||
|
||||
|
||||
void PITranslator::loadLang(const PIString & short_lang) {
|
||||
auto s = instance();
|
||||
void PITranslator::loadLang(const PIString & short_lang, PIString dir) {
|
||||
if (dir.isEmpty()) dir = PIDir::current().absolute("lang");
|
||||
clear();
|
||||
auto files = PIDir(dir).entries();
|
||||
for (const auto & f: files) {
|
||||
if (!f.baseName().endsWith(short_lang)) continue;
|
||||
loadFile(f.path);
|
||||
}
|
||||
piCout << "Loaded %1 string for lang \"%2\""_a.arg(instance()->PRIVATEWB->content.count()).arg(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());
|
||||
auto c = s->PRIVATEWB->content.createContext(cn.name());
|
||||
for (const auto & s: cn.children())
|
||||
c->add(s.name(), s.value().toString());
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
@@ -62,11 +75,11 @@ 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());
|
||||
auto c = s->PRIVATEWB->content.createContext(cn.name());
|
||||
for (const auto & s: cn.children())
|
||||
c->add(s.name(), s.value().toString());
|
||||
}
|
||||
auto c = s->createContextInternal("");
|
||||
auto c = s->PRIVATEWB->content.createContext("");
|
||||
for (const auto & s: lang.children()) {
|
||||
if (s.hasChildren()) continue;
|
||||
c->add(s.name(), s.value().toString());
|
||||
@@ -74,11 +87,26 @@ void PITranslator::loadConfig(const PIString & content) {
|
||||
}
|
||||
|
||||
|
||||
bool PITranslator::load(const PIByteArray & content) {
|
||||
return instance()->PRIVATEWB->content.load(content);
|
||||
}
|
||||
|
||||
|
||||
bool PITranslator::loadFile(const PIString & path) {
|
||||
PIFile f(path, PIIODevice::ReadOnly);
|
||||
if (f.isClosed()) return false;
|
||||
if (!PITranslatorPrivate::checkHeader(&f)) return false;
|
||||
auto data = f.readAll();
|
||||
data.remove(0, PITranslatorPrivate::headerSize());
|
||||
return load(data);
|
||||
}
|
||||
|
||||
|
||||
PITranslator::PITranslator() {}
|
||||
|
||||
|
||||
PITranslator::~PITranslator() {
|
||||
clearInternal();
|
||||
PRIVATE->content.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -86,21 +114,3 @@ 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user