146 lines
5.1 KiB
C++
146 lines
5.1 KiB
C++
|
//
|
||
|
// Created by bbr on 14.06.22..
|
||
|
//
|
||
|
|
||
|
#include "message_source.h"
|
||
|
|
||
|
#include <utility>
|
||
|
|
||
|
// static instance of application
|
||
|
static MessageSource *messageSource = nullptr;
|
||
|
|
||
|
MessageSource *MessageSource::instance() {
|
||
|
if (messageSource == nullptr) {
|
||
|
messageSource = new MessageSource();
|
||
|
}
|
||
|
return messageSource;
|
||
|
}
|
||
|
|
||
|
void MessageSource::loadDefaults() {
|
||
|
messages.clear();
|
||
|
|
||
|
// TOOLBAR
|
||
|
add("msg_toolbar_load_library", "Load library");
|
||
|
add("msg_toolbar_load_schema", "Load schema");
|
||
|
add("msg_toolbar_save_schema", "Save schema");
|
||
|
add("msg_toolbar_generate_comdel", "Generate comdel");
|
||
|
|
||
|
// SIDEBAR
|
||
|
add("msg_sidebar_components", "Components:");
|
||
|
add("msg_sidebar_busses", "Busses:");
|
||
|
|
||
|
// VALUES
|
||
|
add("msg_boolean_true", "True");
|
||
|
add("msg_boolean_false", "False");
|
||
|
|
||
|
// SUCCESS MESSAGES
|
||
|
add("msg_dialog_success_title", "Success");
|
||
|
add("msg_dialog_success_validation", "No validation errors found!");
|
||
|
add("msg_dialog_success_comdel_generation", "Successfully generate COMDEL model!");
|
||
|
add("msg_dialog_success_save_schema", "Successfully saved schema");
|
||
|
|
||
|
// DEFAULT ACTIONS
|
||
|
add("msg_dialog_actions_ok", "Ok");
|
||
|
add("msg_dialog_actions_cancel", "Cancel");
|
||
|
add("msg_dialog_actions_set", "Set");
|
||
|
add("msg_dialog_actions_update", "Update");
|
||
|
add("msg_dialog_actions_update_named", "Update {name}");
|
||
|
add("msg_dialog_actions_remove_named", "Remove {name}");
|
||
|
// PIN DIALOG
|
||
|
add("msg_pin_update", "Update");
|
||
|
add("msg_pin_remove", "Remove");
|
||
|
add("msg_sa_pin_update_title", "Update {wire1} - {wire2}");
|
||
|
add("msg_sa_pin_remove_title", "Remove {wire1} - {wire2}");
|
||
|
add("msg_pin_update_action", "Update connection");
|
||
|
add("msg_pin_remove_action", "Remove connection");
|
||
|
// ERROR DIALOG
|
||
|
add("msg_dialog_error_title", "Error");
|
||
|
add("msg_dialog_error_close", "Close");
|
||
|
// WARNING DIALOG
|
||
|
add("msg_dialog_warning_title", "Warning");
|
||
|
// ATTRIBUTE DIALOG/ACTION
|
||
|
add("msg_dialog_attribute_update", "Update {attribute}");
|
||
|
add("msg_dialog_attribute_set", "Set {attribute}");
|
||
|
// NAME DIALOG
|
||
|
add("msg_dialog_name_update", "Update name");
|
||
|
// MEMORY DIALOG/ACTION
|
||
|
add("msg_dialog_memory_update", "Update memory");
|
||
|
add("msg_dialog_memory_set", "Set memory");
|
||
|
add("msg_dialog_memory_default", "No memory selected");
|
||
|
// CONNECTION
|
||
|
add("msg_dialog_sa_pin_set", "Set wire");
|
||
|
// VALIDATORS
|
||
|
add("msg_validators_component_min_count", "Missing instances of component {componentName}. Required at least {min}, found {count}!");
|
||
|
add("msg_validators_component_max_count", "Too many instances of component {componentname}. Expected at most {max}, found {count}!");
|
||
|
add("msg_validators_bus_min_count", "Missing instances of bus {busName}. Required at least {min}, found {count}!");
|
||
|
add("msg_validators_bus_max_count", "Too many instances of bus {busName}. Expected at most {max}, found {count}!");
|
||
|
add("msg_validators_memory_not_found", "Cannot find memory instance {memoryReference}");
|
||
|
add("msg_validators_duplicates_found", "Found multiple instances with name {instanceName}");
|
||
|
|
||
|
// LOADING/SAVING DIALOGS
|
||
|
add("msg_files_load_library", "Open library");
|
||
|
add("msg_files_load_library_format", "COMDEL library (*.csl)");
|
||
|
add("msg_files_load_schema", "Open schema");
|
||
|
add("msg_files_load_schema_format", "COMDEL schema (*.csch)");
|
||
|
add("msg_files_store_schema", "Store schema");
|
||
|
add("msg_files_store_schema_format", "COMDEL schema (*.csch)");
|
||
|
add("msg_files_store_comdel", "Store COMDEL system");
|
||
|
add("msg_files_store_comdel_format", "COMDEL system (*.system)");
|
||
|
|
||
|
}
|
||
|
|
||
|
void MessageSource::add(const std::string& key, std::string message) {
|
||
|
messages[key] = message;
|
||
|
}
|
||
|
|
||
|
std::string MessageSource::get(const std::string& key, std::map<std::string, std::string> parametars) {
|
||
|
std::string message;
|
||
|
if(messages.count(key) == 1) {
|
||
|
// if key not found in translations use key as message
|
||
|
message = messages[key];
|
||
|
} else {
|
||
|
message = key;
|
||
|
}
|
||
|
|
||
|
message = populateMessage(message, parametars);
|
||
|
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
std::string MessageSource::get(const std::string &key) {
|
||
|
return get(key, {});
|
||
|
}
|
||
|
|
||
|
std::string MessageSource::populateMessage(std::string message, std::map<std::string, std::string> parametars) {
|
||
|
for (auto &[key, value]: parametars) {
|
||
|
message = replacePlaceholder(message, key, value);
|
||
|
}
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
std::string MessageSource::replacePlaceholder(std::string message, const std::string key, std::string value) {
|
||
|
auto wholeKey = "{" + key + "}";
|
||
|
|
||
|
auto found = message.find(wholeKey);
|
||
|
while (found != std::string::npos) {
|
||
|
message.replace(found, wholeKey.length(), value);
|
||
|
found = message.find(key);
|
||
|
}
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
void MessageSource::load(std::map<std::string, std::string> _messages) {
|
||
|
loadDefaults();
|
||
|
for (auto &[key, value]: _messages) {
|
||
|
messages[key] = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::map<std::string, std::string> MessageSource::map(std::map<std::string, domain::Value> values) {
|
||
|
std::map<std::string, std::string> parametars;
|
||
|
for (auto &[key, value]: values) {
|
||
|
parametars[key] = value.string();
|
||
|
}
|
||
|
return parametars;
|
||
|
}
|