Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
Functions | Variables
localisation.cpp File Reference
#include "localisation.h"
#include <unordered_map>
#include <string>
#include <fstream>
#include <exception>
#include <spdlog/spdlog.h>
#include "app_workspace.h"
#include "utility.h"

Go to the source code of this file.

Functions

const char * get_localized_text (const char *key)
 Get the localized text object. More...
 
int load_default_dict ()
 Loads default language dictionary. More...
 
int load_current_dict ()
 Loads current language dictionary. This could be called with an action during runtime to change the app language. More...
 

Variables

std::unordered_map< std::string, std::string > default_dict
 
std::unordered_map< std::string, std::string > current_dict
 
const char * err_string = "ERR_TXT"
 

Function Documentation

◆ get_localized_text()

const char* get_localized_text ( const char *  key)

Get the localized text object.

Not doing any eror cheking. Programmer MUST ensure all languages are in switch and every key has a translation in map.

Parameters
keystring key
Returns
const char*

Definition at line 18 of file localisation.cpp.

18  {
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 }
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.
std::unique_ptr< app_config > main_config
Application config loaded from app_config.conf (main config file).
std::unordered_map< std::string, std::string > default_dict
std::unordered_map< std::string, std::string > current_dict

◆ load_current_dict()

int load_current_dict ( )

Loads current language dictionary. This could be called with an action during runtime to change the app language.

Returns
int

Definition at line 65 of file localisation.cpp.

65  {
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 }
void init_labels()
This is called after localisation dictionary is loaded. After the dict is loaded arrays defined in ap...

◆ load_default_dict()

int load_default_dict ( )

Loads default language dictionary.

Returns
int

Definition at line 37 of file localisation.cpp.

37  {
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 }

Variable Documentation

◆ current_dict

std::unordered_map<std::string, std::string> current_dict

Unordered map of current language dict, application currently doesn't have function to use this (can't set current language during runtime)

Definition at line 15 of file localisation.cpp.

◆ default_dict

std::unordered_map<std::string, std::string> default_dict

Unordered of default language dictionary

Definition at line 12 of file localisation.cpp.

◆ err_string

const char* err_string = "ERR_TXT"

Definition at line 16 of file localisation.cpp.