Skip to content

Commit 61317a8

Browse files
committed
🧹 move setting locale into gucc
1 parent b5b307e commit 61317a8

File tree

5 files changed

+79
-25
lines changed

5 files changed

+79
-25
lines changed

‎gucc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_library(${PROJECT_NAME} SHARED
2020
src/zfs.cpp include/gucc/zfs.hpp
2121
src/btrfs.cpp include/gucc/btrfs.hpp
2222
src/user.cpp include/gucc/user.hpp
23+
src/locale.cpp include/gucc/locale.hpp
2324
#src/chwd_profiles.cpp src/chwd_profiles.hpp
2425
#src/disk.cpp src/disk.hpp
2526
)

‎gucc/include/gucc/locale.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef LOCALE_HPP
2+
#define LOCALE_HPP
3+
4+
#include <string_view> // for string_view
5+
6+
namespace gucc::locale {
7+
8+
// Set system language
9+
auto set_locale(std::string_view locale, std::string_view mountpoint) noexcept -> bool;
10+
11+
} // namespace gucc::locale
12+
13+
#endif // LOCALE_HPP

‎gucc/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gucc_lib = library('gucc',
1010
'src/zfs.cpp',
1111
'src/btrfs.cpp',
1212
'src/user.cpp',
13+
'src/locale.cpp',
1314
],
1415
include_directories : [include_directories('include')],
1516
dependencies: deps

‎gucc/src/locale.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "gucc/locale.hpp"
2+
#include "gucc/io_utils.hpp"
3+
#include "gucc/string_utils.hpp"
4+
5+
#include <fstream> // for ofstream
6+
7+
#include <fmt/compile.h>
8+
#include <fmt/format.h>
9+
10+
#include <spdlog/spdlog.h>
11+
12+
using namespace std::string_view_literals;
13+
14+
namespace gucc::locale {
15+
16+
auto set_locale(std::string_view locale, std::string_view mountpoint) noexcept -> bool {
17+
const auto& locale_config_path = fmt::format(FMT_COMPILE("{}/etc/locale.conf"), mountpoint);
18+
const auto& locale_gen_path = fmt::format(FMT_COMPILE("{}/etc/locale.gen"), mountpoint);
19+
20+
static constexpr auto LOCALE_CONFIG_PART = R"(LANG="{0}"
21+
LC_NUMERIC="{0}"
22+
LC_TIME="{0}"
23+
LC_MONETARY="{0}"
24+
LC_PAPER="{0}"
25+
LC_NAME="{0}"
26+
LC_ADDRESS="{0}"
27+
LC_TELEPHONE="{0}"
28+
LC_MEASUREMENT="{0}"
29+
LC_IDENTIFICATION="{0}"
30+
LC_MESSAGES="{0}"
31+
)";
32+
33+
{
34+
const auto& locale_config_text = fmt::format(LOCALE_CONFIG_PART, locale);
35+
std::ofstream locale_config_file{locale_config_path, std::ios::out | std::ios::trunc};
36+
if (!locale_config_file.is_open()) {
37+
spdlog::error("Failed to open locale config for writing {}", locale_config_path);
38+
return false;
39+
}
40+
locale_config_file << locale_config_text;
41+
}
42+
43+
// TODO(vnepogodin): refactor and make backups of locale config and locale gen
44+
utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/#{0}/{0}/\" {1}"), locale, locale_gen_path));
45+
46+
// Generate locales
47+
if (!utils::arch_chroot_checked("locale-gen", mountpoint)) {
48+
spdlog::error("Failed to run locale-gen with locale '{}'", locale);
49+
return false;
50+
}
51+
52+
// NOTE: maybe we should also write into /etc/default/locale if /etc/default exists and is a dir?
53+
return true;
54+
}
55+
56+
} // namespace gucc::locale

‎src/utils.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gucc/file_utils.hpp"
1111
#include "gucc/initcpio.hpp"
1212
#include "gucc/io_utils.hpp"
13+
#include "gucc/locale.hpp"
1314
#include "gucc/luks.hpp"
1415
#include "gucc/pacmanconf_repo.hpp"
1516
#include "gucc/string_utils.hpp"
@@ -388,31 +389,13 @@ void set_hostname(const std::string_view& hostname) noexcept {
388389
void set_locale(const std::string_view& locale) noexcept {
389390
spdlog::info("Selected locale: {}", locale);
390391
#ifdef NDEVENV
391-
auto* config_instance = Config::instance();
392-
auto& config_data = config_instance->data();
393-
const auto& mountpoint = std::get<std::string>(config_data["MOUNTPOINT"]);
394-
const auto& locale_config_path = fmt::format(FMT_COMPILE("{}/etc/locale.conf"), mountpoint);
395-
const auto& locale_gen_path = fmt::format(FMT_COMPILE("{}/etc/locale.gen"), mountpoint);
396-
397-
static constexpr auto locale_config_part = R"(LANG="{0}"
398-
LC_NUMERIC="{0}"
399-
LC_TIME="{0}"
400-
LC_MONETARY="{0}"
401-
LC_PAPER="{0}"
402-
LC_NAME="{0}"
403-
LC_ADDRESS="{0}"
404-
LC_TELEPHONE="{0}"
405-
LC_MEASUREMENT="{0}"
406-
LC_IDENTIFICATION="{0}"
407-
LC_MESSAGES="{0}")";
408-
409-
std::ofstream locale_config_file{locale_config_path};
410-
locale_config_file << fmt::format(locale_config_part, locale);
411-
412-
gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/#{0}/{0}/\" {1}"), locale, locale_gen_path));
413-
414-
// Generate locales
415-
utils::arch_chroot("locale-gen", false);
392+
auto* config_instance = Config::instance();
393+
auto& config_data = config_instance->data();
394+
const auto& mountpoint = std::get<std::string>(config_data["MOUNTPOINT"]);
395+
396+
if (!gucc::locale::set_locale(locale, mountpoint)) {
397+
spdlog::error("Failed to set locale");
398+
}
416399
#endif
417400
}
418401

0 commit comments

Comments
 (0)