From 1379117213f2cf052d2343562023082b9f0ff528 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Fri, 14 Oct 2022 09:58:01 +0800 Subject: [PATCH] A new multilingual solution --- DSView/pv/ui/langresource.cpp | 184 ++++++++++++++++++++++++++++++++++ DSView/pv/ui/langresource.h | 91 +++++++++++++++++ DSView/pv/ui/string_ids.h | 39 +++++++ lang/en/device.json | 10 ++ lang/en/main.json | 10 ++ lang/en/msg.json | 10 ++ lang/en/toolbar.json | 10 ++ 7 files changed, 354 insertions(+) create mode 100644 DSView/pv/ui/langresource.cpp create mode 100644 DSView/pv/ui/langresource.h create mode 100644 DSView/pv/ui/string_ids.h create mode 100644 lang/en/device.json create mode 100644 lang/en/main.json create mode 100644 lang/en/msg.json create mode 100644 lang/en/toolbar.json diff --git a/DSView/pv/ui/langresource.cpp b/DSView/pv/ui/langresource.cpp new file mode 100644 index 00000000..03bff5a0 --- /dev/null +++ b/DSView/pv/ui/langresource.cpp @@ -0,0 +1,184 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 +#include "../log.h" +#include "../config/appconfig.h" +#include +#include +#include +#include +#include +#include +#include + +//---------------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; +} \ No newline at end of file diff --git a/DSView/pv/ui/langresource.h b/DSView/pv/ui/langresource.h new file mode 100644 index 00000000..aa8683fc --- /dev/null +++ b/DSView/pv/ui/langresource.h @@ -0,0 +1,91 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 +#include +#include +#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 _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 _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 \ No newline at end of file diff --git a/DSView/pv/ui/string_ids.h b/DSView/pv/ui/string_ids.h new file mode 100644 index 00000000..b0ad2671 --- /dev/null +++ b/DSView/pv/ui/string_ids.h @@ -0,0 +1,39 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2022 DreamSourceLab + * + * 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 \ No newline at end of file diff --git a/lang/en/device.json b/lang/en/device.json new file mode 100644 index 00000000..b8e07c0b --- /dev/null +++ b/lang/en/device.json @@ -0,0 +1,10 @@ +[ + { + "id": "vvv", + "text": "xxx" + }, + { + "id": "vvv1", + "text": "xxx1" + } +] \ No newline at end of file diff --git a/lang/en/main.json b/lang/en/main.json new file mode 100644 index 00000000..b8e07c0b --- /dev/null +++ b/lang/en/main.json @@ -0,0 +1,10 @@ +[ + { + "id": "vvv", + "text": "xxx" + }, + { + "id": "vvv1", + "text": "xxx1" + } +] \ No newline at end of file diff --git a/lang/en/msg.json b/lang/en/msg.json new file mode 100644 index 00000000..b8e07c0b --- /dev/null +++ b/lang/en/msg.json @@ -0,0 +1,10 @@ +[ + { + "id": "vvv", + "text": "xxx" + }, + { + "id": "vvv1", + "text": "xxx1" + } +] \ No newline at end of file diff --git a/lang/en/toolbar.json b/lang/en/toolbar.json new file mode 100644 index 00000000..1822748d --- /dev/null +++ b/lang/en/toolbar.json @@ -0,0 +1,10 @@ +[ + { + "id": "IDS_TOOLBAR_MODE", + "text": "模式1" + }, + { + "id": "IDS_TOOLBAR_START", + "text": "Start" + } +] \ No newline at end of file