From caf47e87ea47571b8203a9f4ba16d8b93098eeb3 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Tue, 15 Apr 2025 20:05:49 +0200 Subject: [PATCH] add basic localization framework --- Makefile | 6 +- src/editor_easy.cpp | 21 ++++-- src/main.cpp | 16 +++++ src/menu.cpp | 78 ++++++++++++----------- src/strings.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++++ src/strings.h | 43 +++++++++++++ 6 files changed, 269 insertions(+), 46 deletions(-) create mode 100644 src/strings.cpp create mode 100644 src/strings.h diff --git a/Makefile b/Makefile index 67d7823..eb2c7d3 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ # Compiler and flags CPPC = g++ -CPPC_FLAGS = -std=c++23 -s -O3 -Wall -Wextra -Wno-write-strings -lncurses -lmenu -lform -lcups -DEBUG_FLAGS = -ggdb -std=c++23 -Wall -Wextra -Wno-write-strings -lncurses -lmenu -lform -lcups -DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncurses -lmenu -lcups -Wall -Wextra -Wno-write-strings +CPPC_FLAGS = -std=c++23 -s -O3 -Wall -Wextra -lncurses -lmenu -lform -lcups +DEBUG_FLAGS = -ggdb -std=c++23 -Wall -lncurses -lmenu -lform -lcups +DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncurses -lmenu -lcups -Wall -Wextra SRC_PATH := src diff --git a/src/editor_easy.cpp b/src/editor_easy.cpp index b7230f5..1851ba5 100644 --- a/src/editor_easy.cpp +++ b/src/editor_easy.cpp @@ -64,12 +64,21 @@ std::vector editor_easy_allocated; bool multiline; }; - FieldConfig field_configs[] = { - {"Jméno", 1, 40, false}, {"Adresa:", 1, 50, false}, - {"Telefon:", 1, 20, false}, {"Email:", 1, 30, false}, - {"Místo činu:", 1, 40, false}, {"Datum činu (dd.mm.rrrr):", 1, 15, false}, - {"Popis činu:", 5, 60, true}, {"Jméno pachatele:", 1, 40, false}, - {"Důkazy:", 4, 60, true}, {"Další informace:", 3, 60, true}}; + FieldConfig field_configs[] = {{"Jméno podavatele:", 1, 40, false}, + {"Adresa podavatele:", 1, 50, false}, + {"Telefon podavatele:", 1, 20, false}, + {"Email podavatele:", 1, 30, false}, + + {"Jméno pachatele:", 1, 40, false}, + {"Adresa pachatele:", 1, 50, false}, + {"Telefon pachatele:", 1, 20, false}, + {"Email pachatele:", 1, 30, false}, + + {"Místo činu:", 1, 40, false}, + {"Datum činu (dd.mm.rrrr):", 1, 15, false}, + {"Popis činu:", 4, 60, true}, + {"Důkazy:", 4, 60, true}, + {"Další informace:", 4, 60, true}}; // Vykreslení popisků int label_row = 0; diff --git a/src/main.cpp b/src/main.cpp index 3a551c3..69ec6d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,10 +2,13 @@ #include #include #include +#include +#include #include "color.h" #include "const.h" #include "menu.h" #include "signal.h" +#include "strings.h" void PrintHelp() { std::cout << RED R"( ____ ____ @@ -37,6 +40,19 @@ int main(int argc, char* argv[]) { // error signal handlers signal(SIGSEGV, safe_exit); + // set locale + { + std::locale loc(""); + std::regex czech_regex("czech|cz", std::regex_constants::icase | + std::regex_constants::ECMAScript); + + if (std::regex_search(loc.name(), czech_regex)) { + loc_strings = &czech_strings; + } else { + loc_strings = &english_strings; + } + } + int opt; while ((opt = getopt(argc, argv, "hV")) != -1) { switch (opt) { diff --git a/src/menu.cpp b/src/menu.cpp index ef6d8d3..f65a732 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,32 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include #include "const.h" #include "cups.h" #include "editor_easy.h" #include "gameske_funkce.h" #include "memory.h" #include "types.h" -#include -#include -#include -#include -#include -#include -#include std::vector main_menu_allocated; -const char *choices[] = {"paradox", "kompromis", "Stereotyp", "Žena"}; -const char *date[] = {"2023-10-01", "2023-10-02", "2023-10-03", "2023-10-04"}; -const char *choices2[] = {"PRINT", "EDIT", "DELETE", "Žena"}; +const char* choices[] = {"paradox", "kompromis", "Stereotyp", "Žena"}; +const char* date[] = {"2023-10-01", "2023-10-02", "2023-10-03", "2023-10-04"}; +const char* choices2[] = {"PRINT", "EDIT", "DELETE", "Žena"}; void menu() { current_allocated = &main_menu_allocated; /* Initialize curses */ + setlocale(LC_ALL, ""); initscr(); start_color(); cbreak(); noecho(); - curs_set(0); // Makes cursor invisible + curs_set(0); // Makes cursor invisible keypad(stdscr, TRUE); for (uint8_t i = 0; i < 8; i++) { init_pair(i, i, COLOR_BLACK); @@ -38,7 +40,7 @@ void menu() { complete_menu main_menu = {nullptr, nullptr, 0, nullptr}; main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1}); - main_menu.items = new ITEM *[ARRAY_SIZE(choices) + 1]; + main_menu.items = new ITEM*[ARRAY_SIZE(choices) + 1]; main_menu.items_size = ARRAY_SIZE(choices) + 1; for (size_t i = 0; i < n_choices; ++i) { main_menu.items[i] = new_item(choices[i], date[i]); @@ -75,35 +77,37 @@ void menu() { int c; while ((c = wgetch(main_menu.win)) != KEY_F(1)) { switch (c) { - case KEY_DOWN: - menu_driver(main_menu.menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(main_menu.menu, REQ_UP_ITEM); - break; - case 10: + case KEY_DOWN: + menu_driver(main_menu.menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(main_menu.menu, REQ_UP_ITEM); + break; + case 10: - refresh(); - break; - case ':': - switch (hash_djb2a(spawncmd())) { - case "print"_sh: - case "p"_sh: - // DONT FORGET TO PRINT ACTUAL DOCUMENT - printDocument("test"); - current_allocated = &main_menu_allocated; + refresh(); break; - case "easy_edit"_sh: - case "ee"_sh: - vytvorTrestniOznameni(); - break; - default: - print_in_middle(main_menu.win, 10, 0, 40, "Unknown command", - COLOR_PAIR(1)); - break; - } + case ':': + switch (hash_djb2a(spawncmd())) { + case "print"_sh: + case "p"_sh: + // DONT FORGET TO PRINT ACTUAL DOCUMENT + printDocument("test"); + current_allocated = &main_menu_allocated; + break; + case "easy_edit"_sh: + case "ee"_sh: + std::clog << vytvorTrestniOznameni(); + break; + default: + print_in_middle(main_menu.win, 10, 0, 40, "Unknown command", + COLOR_PAIR(1)); + break; + } } + redrawwin(main_menu.win); wrefresh(main_menu.win); + refresh(); } unpost_menu(main_menu.menu); diff --git a/src/strings.cpp b/src/strings.cpp new file mode 100644 index 0000000..14e16fd --- /dev/null +++ b/src/strings.cpp @@ -0,0 +1,151 @@ +#include "strings.h" + +strings english_strings{ + .invalid_option = "Invalid option:", + .usage = "Usage:", + .options = "OPTIONS", + .print_this_help = "Print this help", + .print_version = "Print version", + .version = "Version:", + .main_menu = "Main menu", + .unknown_command = "Unknown command:", + .min_terminal_size = "Minimum terminal size: 90x28", + .name_of_submiter = "Name of submiter:", + .address_of_submiter = "Address of submiter:", + .phone_of_submiter = "Phone of submiter:", + .email_of_submiter = "Email of submiter:", + .name_of_recipient = "Name of recipient:", + .address_of_recipient = "Address of recipient:", + .phone_of_recipient = "Phone of recipient:", + .email_of_recipient = "Email of recipient:", + .place_of_incident = "Place of incident:", + .date_of_incident = "Date of incident (dd.mm.rrrr):", + .description_of_incident = "Description of incident:", + .evidence_of_incident = "Evidence of incident:", + .additional_info = "Additional info:", + .cease_and_desist_entry_of_information = + "CEASE AND DESIST - ENTRY OF INFORMATION", + .enter_all_information_and_press_f10 = + "Enter all information and press F10 to finish", + .cease_and_desist_template = R"( +CEASE AND DESIST NOTICE + +In {} on {} + +Recipient: +First and last name: {} +Address: {} +Phone: {} +Email: {} + +Sender: +First and last name: {} +Address: {} +Phone: {} +Email: {} + +Subject: Notice to immediately cease unlawful conduct + +{} + +The unlawful conduct occurred on {} in {}. + +Description of the unlawful conduct: +{} + +Evidence: +{} + +Additional information: +{} + +If you do not cease the above-mentioned conduct, I will be forced to take further legal action, including filing a criminal complaint and seeking compensation for damages. + +Sincerely, + +............................ +{} +(signature) + +*This is not a legal document, but only a template for informational purposes. For legal advice, please consult a qualified attorney.* +)", + .call_to_stop_illegal_activity = + "I hereby call upon you to immediately cease the unlawful conduct.", + .call_to_stop_illegal_activity_with_name = + "I hereby call upon you, {}, to immediately cease the unlawful " + "conduct."}; + +strings czech_strings{ + .invalid_option = "Neplatná volba: ", + .usage = "Použití:", + .options = "MOŽNOSTI", + .print_this_help = "Zobrazit tuto nápovědu", + .print_version = "Zobrazit verzi", + .version = "Verze: ", + .main_menu = "Hlavní nabídka", + .unknown_command = "Neznámý příkaz: ", + .min_terminal_size = "Minimální velikost terminálu: 90x28", + .name_of_submiter = "Jméno podavatele: ", + .address_of_submiter = "Adresa podavatele: ", + .phone_of_submiter = "Telefon podavatele: ", + .email_of_submiter = "Email podavatele: ", + .name_of_recipient = "Jméno adresáta: ", + .address_of_recipient = "Adresa adresáta: ", + .phone_of_recipient = "Telefon adresáta: ", + .email_of_recipient = "Email adresáta: ", + .place_of_incident = "Místo činu: ", + .date_of_incident = "Datum činu (dd.mm.rrrr):", + .description_of_incident = "Popis činu:", + .evidence_of_incident = "Důkazy:", + .additional_info = "Další informace:", + .cease_and_desist_entry_of_information = "TRESTNÍ OZNÁMENÍ - ZADÁNÍ ÚDAJŮ", + .enter_all_information_and_press_f10 = + "Zadejte všechny údaje a stiskněte F10 pro dokončení", + .cease_and_desist_template = R"( +VÝZVA K UPUŠTĚNÍ OD PROTIPRÁVNÍHO JEDNÁNÍ + +V {} dne {} + +Adresát: +Jméno a příjmení: {} +Adresa: {} +Telefon: {} +Email: {} + +Odesílatel: +Jméno a příjmení: {} +Adresa: {} +Telefon: {} +Email: {} + +Věc: Výzva k okamžitému upuštění od protiprávního jednání + +{} + +K protiprávnímu jednání došlo dne {} v {}. + +Popis protiprávního jednání: +{} + +Důkazy: +{} + +Další informace: +{} + +Pokud neupustíte od výše uvedeného jednání, budu nucen/a podniknout další právní kroky, včetně podání trestního oznámení a vymáhání náhrady škody. + +S pozdravem, + +............................ +{} +(podpis) + +*Toto není právní dokument, ale pouze vzor pro informativní účely. Pro právní poradenství se obraťte na kvalifikovaného právníka.* +)", + .call_to_stop_illegal_activity = + "Tímto Vás vyzývám, abyste okamžitě upustil/a od protiprávního " + "jednání.", + .call_to_stop_illegal_activity_with_name = + "Tímto Vás, {}, vyzývám, abyste okamžitě upustil/a od protiprávního " + "jednání."}; \ No newline at end of file diff --git a/src/strings.h b/src/strings.h new file mode 100644 index 0000000..e62975b --- /dev/null +++ b/src/strings.h @@ -0,0 +1,43 @@ + +#ifndef PARADOCS_STRINGS_H_ +#define PARADOCS_STRINGS_H_ +typedef struct { + const char* invalid_option; + const char* usage; + const char* options; + const char* print_this_help; + const char* print_version; + const char* version; + const char* main_menu; + const char* unknown_command; + + const char* min_terminal_size; + const char* name_of_submiter; + const char* address_of_submiter; + const char* phone_of_submiter; + const char* email_of_submiter; + + const char* name_of_recipient; + const char* address_of_recipient; + const char* phone_of_recipient; + const char* email_of_recipient; + + const char* place_of_incident; + const char* date_of_incident; + const char* description_of_incident; + const char* evidence_of_incident; + const char* additional_info; + + const char* cease_and_desist_entry_of_information; + const char* enter_all_information_and_press_f10; + + const char* cease_and_desist_template; + + const char* call_to_stop_illegal_activity; + const char* call_to_stop_illegal_activity_with_name; +} strings; + +extern strings english_strings; +extern strings czech_strings; +extern strings* loc_strings; +#endif