A new multilingual solution

This commit is contained in:
dreamsourcelabTAI 2022-10-14 09:58:01 +08:00
parent 9457e64e7b
commit 1379117213
7 changed files with 354 additions and 0 deletions

View File

@ -0,0 +1,184 @@
/*
* This file is part of the DSView project.
* DSView is based on PulseView.
*
* Copyright (C) 2022 DreamSourceLab <support@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "langresource.h"
#include <stddef.h>
#include "../log.h"
#include "../config/appconfig.h"
#include <QFile>
#include <QByteArray>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <assert.h>
//---------------Lang_resource_page
void Lang_resource_page::Clear()
{
_res.clear();
}
//---------------LangResource
LangResource::LangResource()
{
_current_page = NULL;
}
LangResource *LangResource::Instance()
{
static LangResource *ins = NULL;
if (ins == NULL)
{
ins = new LangResource();
}
return ins;
}
const char *LangResource::get_lang_key(int lang)
{
int num = sizeof(lang_id_keys) / sizeof(lang_key_item);
char *lan_name = NULL;
for (int i = 0; i < num; i++)
{
if (lang_id_keys[i].id == lang)
{
lan_name = lang_id_keys[i].name;
break;
}
}
return lan_name;
}
bool LangResource::Load(int lang)
{
int num = sizeof(lang_id_keys) / sizeof(lang_key_item);
const char *lan_name = get_lang_key(lang);
if (lan_name == NULL)
{
dsv_err("Can't find language key,lang:%d", lang);
return false;
}
Release();
num = sizeof(lange_page_keys) / sizeof(lang_page_item);
for (int i = 0; i < num; i++)
{
Lang_resource_page *p = new Lang_resource_page();
p->_id = lange_page_keys[i].id;
p->_source = lange_page_keys[i].source;
_pages.push_back(p);
QString file = GetAppDataDir() + "/lang/" + QString(lan_name) + "/" + p->_source;
load_page(*p, file);
}
return false;
}
void LangResource::Release()
{
for (Lang_resource_page *p : _pages)
{
p->Clear();
delete p;
}
_pages.clear();
}
void LangResource::load_page(Lang_resource_page &p, QString file)
{
QFile f(file);
if (f.exists() == false){
dsv_warn("Warning:Language source file is not exists: %s", file.toLocal8Bit().data());
return;
}
f.open(QFile::ReadOnly | QFile::Text);
QByteArray raw_bytes = f.readAll();
f.close();
if (raw_bytes.length() == 0)
return;
QJsonParseError error;
QString jsonStr(raw_bytes.data());
QByteArray qbs = jsonStr.toUtf8();
QJsonDocument doc = QJsonDocument::fromJson(qbs, &error);
if (error.error != QJsonParseError::NoError)
{
QString estr = error.errorString();
dsv_err("LangResource::load_page(), parse json error:\"%s\"!", estr.toUtf8().data());
return;
}
QJsonArray jarray = doc.array();
for (const QJsonValue &dec_value : jarray)
{
QJsonObject obj = dec_value.toObject();
if (obj.contains("id") && obj.contains("text")){
QString id = obj["id"].toString().trimmed();
QString text = obj["text"].toString().trimmed();
p._res[id.toStdString()] = text.toStdString();
}
}
}
const char* LangResource::get_lang_text(int page_id, const char *str_id, const char *default_str)
{
assert(str_id);
if (_current_page == NULL || _current_page->_id != page_id){
_current_page = NULL;
for (Lang_resource_page *p : _pages){
if (p->_id == page_id){
_current_page = p;
break;
}
}
}
if (_current_page == NULL){
dsv_warn("Warning:Cant find language source page:%d", page_id);
return default_str;
}
auto it = _current_page->_res.find(std::string(str_id));
if (it != _current_page->_res.end()){
return (*it).second.c_str();
}
else{
dsv_warn("Warning:Cant't get language text:%s", str_id);
}
return default_str;
}

View File

@ -0,0 +1,91 @@
/*
* This file is part of the DSView project.
* DSView is based on PulseView.
*
* Copyright (C) 2022 DreamSourceLab <support@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LANG_RESOURCE_H
#define LANG_RESOURCE_H
#include <map>
#include <vector>
#include <QString>
#include "string_ids.h"
struct lang_key_item
{
int id;
char *name;
};
class Lang_resource_page
{
public:
void Clear();
public:
int _id;
char *_source;
std::map<std::string, std::string> _res;
};
struct lang_page_item
{
int id;
char *source;
};
static const struct lang_key_item lang_id_keys[] =
{
{25, "cn"},
{31, "en"}
};
static const struct lang_page_item lange_page_keys[] =
{
{STR_PAGE_MAIN, "main.json"},
{STR_PAGE_TOOLBAR, "toolbar.json"},
{STR_PAGE_MSG, "msg.json"},
{STR_PAGE_DEVICE, "device.json"},
};
class LangResource
{
private:
LangResource();
public:
static LangResource* Instance();
bool Load(int lang);
void Release();
const char* get_lang_text(int page_id, const char *str_id, const char *default_str);
private:
const char *get_lang_key(int lang);
void load_page(Lang_resource_page &p, QString file);
private:
std::vector<Lang_resource_page*> _pages;
Lang_resource_page *_current_page;
};
#define S_ID(id) #id
#define L_S(pid,sid,dv) (LangResource::Instance()->get_lang_text((pid), (sid), (dv)))
#endif

39
DSView/pv/ui/string_ids.h Normal file
View File

@ -0,0 +1,39 @@
/*
* This file is part of the DSView project.
* DSView is based on PulseView.
*
* Copyright (C) 2022 DreamSourceLab <support@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRING_IDS_H
#define STRING_IDS_H
#define STR_PAGE_MAIN 1
#define STR_PAGE_MSG 2
#define STR_PAGE_DEVICE 3
#define STR_PAGE_TOOLBAR 4
#define IDS_TOOLBAR_MODE
#define IDS_TOOLBAR_START
#define IDS_TOOLBAR_STOP
#define IDS_TOOLBAR_SINGLE
#define IDS_TOOLBAR_INSTANT
#define IDS_TOOLBAR_SINGLE_ACTION
#define IDS_TOOLBAR_REPEAT_ACTION
#define IDS_TOOLBAR_OPTIONS
#endif

10
lang/en/device.json Normal file
View File

@ -0,0 +1,10 @@
[
{
"id": "vvv",
"text": "xxx"
},
{
"id": "vvv1",
"text": "xxx1"
}
]

10
lang/en/main.json Normal file
View File

@ -0,0 +1,10 @@
[
{
"id": "vvv",
"text": "xxx"
},
{
"id": "vvv1",
"text": "xxx1"
}
]

10
lang/en/msg.json Normal file
View File

@ -0,0 +1,10 @@
[
{
"id": "vvv",
"text": "xxx"
},
{
"id": "vvv1",
"text": "xxx1"
}
]

10
lang/en/toolbar.json Normal file
View File

@ -0,0 +1,10 @@
[
{
"id": "IDS_TOOLBAR_MODE",
"text": "模式1"
},
{
"id": "IDS_TOOLBAR_START",
"text": "Start"
}
]