Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
localisation.cpp
Go to the documentation of this file.
1 #include "localisation.h"
2 
3 #include <unordered_map>
4 #include <string>
5 #include <fstream>
6 #include <exception>
7 #include <spdlog/spdlog.h>
8 #include "app_workspace.h"
9 #include "utility.h"
10 
12 std::unordered_map<std::string, std::string> default_dict;
15 std::unordered_map<std::string, std::string> current_dict;
16 const char* err_string = "ERR_TXT";
17 
18 const char* get_localized_text(const char *key) {
19  app_workspace *app_wrk = app_workspace::get_instance().get();
20 
21  try {
22  if (app_wrk->main_config->lang_current.compare(app_wrk->main_config->lang_default))
23  // originally wanted this branch to fall back on default_dict in case a key isn't found in
24  // current_dict but for simplicity this isn't implemented
25  return current_dict.at(key).c_str();
26  else
27  return default_dict.at(key).c_str();
28  } catch (std::out_of_range &ex) {
29  spdlog::info("localisation.cpp - Failed to find localisation {0} key for currently selected language."
30  "Using key instead.", key);
31  }
32 
33  //return err_string;
34  return key; //return string key instead of general error, to know what string is missing from lang file
35 }
36 
38  app_workspace *app_wrk = app_workspace::get_instance().get();
39  std::string file = app_wrk->main_config->lang_path + "dictionary_" + app_wrk->main_config->lang_default;
40  std::string line;
41  int sep_index;
42  std::ifstream dict(file, std::ios::in);
43 
44  if (!dict.is_open()) {
45  spdlog::critical("localisation.cpp - Failed to load default language dictionary");
46  return 1;
47  }
48 
49  while (std::getline(dict, line)) {
50  sep_index = line.find_first_of('=');
51 
52  std::string key = line.substr(0, sep_index);
53  trim(key);
54  std::string val = line.substr(sep_index + 1, line.length());
55  trim(val);
56 
57  default_dict.emplace(key, val);
58  }
59 
60  dict.close();
61  app_wrk->init_labels();
62  return 0;
63 }
64 
66  app_workspace *app_wrk = app_workspace::get_instance().get();
67  std::string file = app_wrk->main_config->lang_path + "dictionary_" + app_wrk->main_config->lang_current;
68  std::string line;
69  int sep_index;
70  std::ifstream dict(file, std::ios::in);
71 
72  if (!dict.is_open()) {
73  spdlog::critical("localisation.cpp - Failed to load current language dictionary");
74  return 1;
75  }
76 
77  while (std::getline(dict, line)) {
78  sep_index = line.find_first_of('=');
79 
80  std::string key = line.substr(0, sep_index);
81  trim(key);
82  std::string val = line.substr(sep_index + 1, line.length());
83  trim(val);
84 
85  current_dict.emplace(key, val);
86  }
87 
88  dict.close();
89  app_wrk->init_labels();
90  return 0;
91 }
One of the most importat classes in the whole project. Holds variables that define the state of the a...
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
void init_labels()
This is called after localisation dictionary is loaded. After the dict is loaded arrays defined in ap...
std::unique_ptr< app_config > main_config
Application config loaded from app_config.conf (main config file).
const char * get_localized_text(const char *key)
Get the localized text object.
std::unordered_map< std::string, std::string > default_dict
int load_default_dict()
Loads default language dictionary.
std::unordered_map< std::string, std::string > current_dict
int load_current_dict()
Loads current language dictionary. This could be called with an action during runtime to change the a...
const char * err_string