Tactiliest/Tactility/Source/i18n/TextResources.cpp
Ken Van Hoeylandt 8c8ccd8783
Merge develop into main (#305)
## New features

- Implement translations for apps
- Created `tt::settings::setLanguage` and `::getLanguage()`
- External app errors are now reported to the user via an AlertDialog
- Store system settings in `/data/settings.properties`
- Created a "Region & Language" app and moved the timezone setting there.

## Other changes

- Change `/data` and `/system` filesystem sector size from 4096 to 512 bytes to allow for more small files (60+ files of 4kB were over the limit of 256kB for the filesystem)
- Increased size of `/data` and `/system`
- Moved `tt::time::*` to `tt::settings`
- Removed the timezone setting from the "Time & Date" setting app
- Reverse encoder direction of Lilygo T-Lora Pager
- Improved partability of `Time.cpp` (removed separate set of functions for PC/sim)
2025-08-28 21:50:29 +02:00

83 lines
2.3 KiB
C++

#include "Tactility/i18n/TextResources.h"
#include "Tactility/file/FileLock.h"
#include <Tactility/file/File.h>
#include <cstring>
#include <format>
#include <utility>
#include <Tactility/settings/Language.h>
namespace tt::i18n {
constexpr auto* TAG = "I18n";
static std::string getFallbackLocale() {
return "en-US";
}
static std::string getDesiredLocale() {
switch (settings::getLanguage()) {
case settings::Language::en_GB:
return "en-GB";
case settings::Language::en_US:
return "en-US";
case settings::Language::fr_FR:
return "fr-FR";
case settings::Language::nl_BE:
return "nl-BE";
case settings::Language::nl_NL:
return "nl-NL";
default:
return getFallbackLocale();
}
}
static std::string getI18nDataFilePath(const std::string& path) {
auto locale = getDesiredLocale();
auto desired_file_path = std::format("{}/{}.i18n", path, locale);
if (file::isFile(desired_file_path)) {
return desired_file_path;
} else {
TT_LOG_W(TAG, "Translations not found for %s at %s", locale.c_str(), desired_file_path.c_str());
}
auto fallback_locale = getFallbackLocale();
auto fallback_file_path = std::format("{}/{}.i18n", path, getFallbackLocale());
if (file::isFile(fallback_file_path)) {
return fallback_file_path;
} else {
TT_LOG_W(TAG, "Fallback translations not found for %s at %s", fallback_locale.c_str(), fallback_file_path.c_str());
return "";
}
}
std::string TextResources::ERROR_RESULT = "TXT_RES_ERROR";
bool TextResources::load() {
std::vector<std::string> new_data;
// Resolve the language file that we need (depends on system language selection)
auto file_path = getI18nDataFilePath(path);
if (file_path.empty()) {
TT_LOG_E(TAG, "Couldn't find i18n data for %s", path.c_str());
return false;
}
file::withLock<void>(file_path, [&file_path, &new_data] {
file::readLines(file_path, true, [&new_data](const char* line) {
new_data.push_back(line);
});
});
if (new_data.empty()) {
TT_LOG_E(TAG, "Couldn't find i18n data for %s", path.c_str());
return false;
}
data = std::move(new_data);
return true;
}
}