51 const std::string lang_conf = Glib::get_home_dir() +
"/.config/BastionGuard/lang.conf";
52 std::ifstream f(lang_conf);
54 auto normalize = [](std::string s) {
55 auto ltrim = [](std::string& x){ x.erase(0, x.find_first_not_of(
" \t\r\n")); };
56 auto rtrim = [](std::string& x){ x.erase(x.find_last_not_of(
" \t\r\n") + 1); };
58 auto dot = s.find(
'.');
59 if (dot != std::string::npos) s = s.substr(0, dot);
60 auto at = s.find(
'@');
61 if (at != std::string::npos) s = s.substr(0, at);
67 while (std::getline(f, line)) {
68 if (line.empty() || line[0] ==
'#')
continue;
69 auto pos = line.find(
'=');
70 if (pos == std::string::npos)
continue;
72 std::string key = line.substr(0, pos);
73 std::string val = line.substr(pos + 1);
77 if ((key ==
"LANGUAGE" || key ==
"LC_MESSAGES" || key ==
"LANG") && !val.empty()) {
78 auto colon = val.find(
':');
79 std::string first = (colon == std::string::npos) ? val : val.substr(0, colon);
85 auto us = first.find(
'_');
86 if (us != std::string::npos) {
87 std::string base = first.substr(0, us);
103 const std::string lang_conf = Glib::get_home_dir() +
"/.config/BastionGuard/lang.conf";
104 std::ifstream f(lang_conf);
111 while (std::getline(f, line)) {
112 if (line.empty() || line[0] ==
'#')
115 auto pos = line.find(
'=');
116 if (pos == std::string::npos)
119 std::string key = line.substr(0, pos);
120 std::string val = line.substr(pos + 1);
122 auto ltrim = [](std::string& s){
123 s.erase(0, s.find_first_not_of(
" \t\r\n"));
125 auto rtrim = [](std::string& s){
126 s.erase(s.find_last_not_of(
" \t\r\n") + 1);
128 ltrim(key); rtrim(key);
129 ltrim(val); rtrim(val);
131 if (!key.empty() && !val.empty()) {
132 setenv(key.c_str(), val.c_str(), 1);
140int main(
int argc,
char* argv[]) {
142 const std::string lang_conf = Glib::get_home_dir() +
"/.config/BastionGuard/lang.conf";
145 if (!fs::exists(lang_conf, ec) || ec) {
146 std::string cfgdir = Glib::get_home_dir() +
"/.config/BastionGuard";
147 fs::create_directories(cfgdir, ec);
148 std::ofstream out(lang_conf, std::ios::out | std::ios::trunc);
150 out <<
"LANG=en_US.UTF-8\n";
151 out <<
"LC_ALL=en_US.UTF-8\n";
152 out <<
"LANGUAGE=en_US\n";
154 std::cout <<
"[Lang] ✅ Creato lang.conf di default: " << lang_conf << std::endl;
159 std::ifstream f(lang_conf);
162 while (std::getline(f, line)) {
163 auto pos = line.find(
'=');
164 if (pos != std::string::npos) {
165 std::string key = line.substr(0, pos);
166 std::string val = line.substr(pos + 1);
167 if (!key.empty() && !val.empty())
168 setenv(key.c_str(), val.c_str(), 1);
172 std::cout <<
"[Lang] Config caricata da " << lang_conf << std::endl;
174 std::cerr <<
"[Lang] ⚠️ Impossibile aprire lang.conf, forzo inglese di default" << std::endl;
177 const char* lang_env = std::getenv(
"LANG");
178 if (!lang_env || std::string(lang_env).empty())
179 setenv(
"LANG",
"en_US.UTF-8", 1);
181 const char* lc_all_env = std::getenv(
"LC_ALL");
182 if (!lc_all_env || std::string(lc_all_env).empty())
183 setenv(
"LC_ALL",
"en_US.UTF-8", 1);
185 const char* language_env = std::getenv(
"LANGUAGE");
186 if (!language_env || std::string(language_env).empty())
187 setenv(
"LANGUAGE",
"en_US", 1);
189 const char* applied = setlocale(LC_ALL,
"");
190 if (!applied) applied = setlocale(LC_ALL,
"en_US.UTF-8");
191 if (!applied) applied = setlocale(LC_ALL,
"en_US");
192 if (!applied) applied = setlocale(LC_ALL,
"C.UTF-8");
193 if (!applied) applied = setlocale(LC_ALL,
"C");
196 bindtextdomain(
"BastionGuard",
LOCALEDIR);
197 bind_textdomain_codeset(
"BastionGuard",
"UTF-8");
198 textdomain(
"BastionGuard");
200 auto app = Gtk::Application::create(
"org.BastionGuard.wizard");
204 app->add_window(*wizard);
206 wizard->signal_finished().connect([&app, wizard]() {
208 app->remove_window(*wizard);
212 const std::string first_run_app =
"/usr/bin/BastionGuard-first-run";
216 execl(first_run_app.c_str(), first_run_app.c_str(),
nullptr);
217 std::cerr <<
"[Wizard] ❌ Impossibile lanciare BastionGuard-first-run: "
218 << strerror(errno) << std::endl;
221 }
else if (pid > 0) {
222 Glib::signal_child_watch().connect([&app](GPid child_pid,
int status) {
223 g_spawn_close_pid(child_pid);
225 const std::string main_app =
"/usr/bin/BastionGuard";
226 std::cout <<
"[Wizard] 🚀 Lancio: " << main_app << std::endl;
227 execl(main_app.c_str(), main_app.c_str(),
nullptr);
229 std::cerr <<
"[Wizard] ❌ Impossibile lanciare BastionGuard: "
230 << strerror(errno) << std::endl;
235 std::cerr <<
"[Wizard] ❌ fork() fallita: " << strerror(errno) << std::endl;
241 return app->run(argc, argv);
static bool bastionguard_mo_exists(const std::string &lang)
static std::string pick_wizard_language_from_localedir()