Add option to manually set language

This commit is contained in:
Patrick Nicolas
2025-09-18 15:50:08 +02:00
parent f33dc57ec9
commit b411afb946
10 changed files with 479 additions and 352 deletions
+28 -30
View File
@@ -18,7 +18,7 @@
*/
#include "application.h"
#include "asset.h"
#include "hardware.h"
#include "openxr/openxr.h"
#include "scene.h"
@@ -26,6 +26,7 @@
#include "spdlog/spdlog.h"
#include "utils/contains.h"
#include "utils/files.h"
#include "utils/i18n.h"
#include "vk/check.h"
#include "wifi_lock.h"
#include "wivrn_packets.h"
@@ -1329,50 +1330,47 @@ void application::initialize()
vk_cmdpool = vk::raii::CommandPool{vk_device, cmdpool_create_info};
initialize_actions();
load_locale();
}
void application::load_locale()
{
gen.add_messages_domain("wivrn");
std::locale loc = gen("");
messages_info.encoding = "UTF-8";
if (config->locale.empty())
{
#ifdef __ANDROID__
jni::klass java_util_Locale("java/util/Locale");
auto default_locale = java_util_Locale.call<jni::object<"java/util/Locale">>("getDefault");
jni::klass java_util_Locale("java/util/Locale");
auto default_locale = java_util_Locale.call<jni::object<"java/util/Locale">>("getDefault");
// if (auto language = default_locale.call<jni::string>("toString"))
if (auto language = default_locale.call<jni::string>("getLanguage"))
messages_info.language = language;
if (auto country = default_locale.call<jni::string>("getCountry"))
messages_info.country = country;
messages_info.encoding = "UTF-8";
// if (auto language = default_locale.call<jni::string>("toString"))
if (auto language = default_locale.call<jni::string>("getLanguage"))
messages_info.language = language;
if (auto country = default_locale.call<jni::string>("getCountry"))
messages_info.country = country;
#else
auto & facet = std::use_facet<boost::locale::info>(loc);
messages_info.language = facet.language();
messages_info.country = facet.country();
messages_info.encoding = "UTF-8";
auto & facet = std::use_facet<boost::locale::info>(loc);
messages_info.language = facet.language();
messages_info.country = facet.country();
#endif
}
else
{
auto pos = config->locale.find("_");
messages_info.language = config->locale.substr(0, pos);
if (pos != std::string::npos)
messages_info.country = config->locale.substr(pos + 1);
}
spdlog::info("Current locale: language {}, country {}, encoding {}", messages_info.language, messages_info.country, messages_info.encoding);
messages_info.paths.push_back("locale");
messages_info.domains.push_back(boost::locale::gnu_gettext::messages_info::domain("wivrn"));
messages_info.callback = [](const std::string & file_name, const std::string & encoding) {
std::vector<char> buffer;
try
{
asset file(file_name);
buffer.resize(file.size());
memcpy(buffer.data(), file.data(), file.size());
}
catch (...)
{
}
return buffer;
};
messages_info.callback = open_locale_file;
loc = std::locale(loc, boost::locale::gnu_gettext::create_messages_facet<char>(messages_info));
std::locale::global(loc);
+2
View File
@@ -417,4 +417,6 @@ public:
{
return instance().messages_info;
}
void load_locale();
};
+4
View File
@@ -218,6 +218,9 @@ configuration::configuration(xr::system & system)
if (auto val = root["first_run"]; val.is_bool())
first_run = val.get_bool();
if (auto val = root["locale"]; val.is_string())
locale = val.get_string().value();
}
catch (std::exception & e)
{
@@ -304,5 +307,6 @@ void configuration::save()
json << ",\"override_foveation_pitch\":" << override_foveation_pitch;
json << ",\"override_foveation_distance\":" << override_foveation_distance;
json << ",\"first_run\":" << std::boolalpha << first_run;
json << ",\"locale\":" << json_string(locale);
json << "}";
}
+2
View File
@@ -74,6 +74,8 @@ public:
bool first_run = true;
std::string locale;
bool check_feature(feature f) const;
void set_feature(feature f, bool state);
+15 -15
View File
@@ -490,22 +490,22 @@ static std::vector<std::string> find_font(std::u16string sample_text, const std:
void imgui_context::initialize_fonts()
{
std::u16string sample_text = u"Hello world";
const auto & locale = application::get_messages_info();
auto it = glyph_set_per_language.find(locale.language + "_" + locale.country);
if (it == glyph_set_per_language.end())
it = glyph_set_per_language.find(locale.language);
if (it != glyph_set_per_language.end())
for (char16_t c: it->second)
sample_text += c;
// Load Fonts
auto fonts = find_font(sample_text, application::get_messages_info().language);
for (auto & i: fonts)
std::vector<std::string> fonts;
for (auto & [code, glyphs]: glyph_set_per_language)
{
spdlog::info("Font {}", i);
std::u16string sample_text = u"Hello world";
for (char16_t c: glyphs)
sample_text += c;
auto language = code.substr(0, code.find("_"));
// Load Fonts
for (auto && i: find_font(sample_text, language))
{
spdlog::info("Font for {} {}", code, i);
if (not std::ranges::contains(fonts, i))
fonts.push_back(std::move(i));
}
}
asset font_awesome_regular("Font Awesome 6 Free-Regular-400.otf");
+38
View File
@@ -17,6 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <uni_algo/case.h>
#define IMGUI_DEFINE_MATH_OPERATORS
#ifdef __ANDROID__
@@ -794,6 +795,43 @@ void scenes::lobby::gui_settings()
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(20, 20));
{
auto language_name = [&](const std::locale & loc = std::locale()) {
return boost::locale::pgettext("language selection, replace with the name of the language", "English", loc);
};
std::vector<std::tuple<std::string, std::locale, std::string>> languages;
for (const auto & msg_info: get_locales())
{
std::string code = msg_info.language;
if (not msg_info.country.empty())
code += "_" + msg_info.country;
std::locale loc(std::locale(), boost::locale::gnu_gettext::create_messages_facet<char>(msg_info));
languages.emplace_back(language_name(loc), loc, std::move(code));
}
std::ranges::sort(languages, [](auto & l, auto & r) {
return una::casesens::collate_utf8(std::get<0>(l), std::get<0>(r)) < 0;
});
if (ImGui::BeginCombo(_S("Language"), config.locale.empty() ? _S("System language") : language_name().c_str()))
{
if (ImGui::Selectable(_S("System language"), config.locale.empty(), ImGuiSelectableFlags_SelectOnRelease))
{
config.locale.clear();
config.save();
application::instance().load_locale();
}
for (const auto & [lang, locale, code]: languages)
{
if (ImGui::Selectable(lang.c_str(), code == config.locale, ImGuiSelectableFlags_SelectOnRelease))
{
config.locale = code;
config.save();
application::instance().load_locale();
}
}
ImGui::EndCombo();
}
}
if (instance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME))
{
const auto & refresh_rates = session.get_refresh_rates();
+70
View File
@@ -0,0 +1,70 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "i18n.h"
#include "asset.h"
#include <map>
#include <string>
#include <vector>
extern std::map<std::string, std::vector<int>> glyph_set_per_language;
std::vector<boost::locale::gnu_gettext::messages_info> get_locales()
{
boost::locale::gnu_gettext::messages_info messages_info;
messages_info.paths.push_back("locale");
messages_info.encoding = "UTF-8";
messages_info.domains.push_back(boost::locale::gnu_gettext::messages_info::domain("wivrn"));
messages_info.callback = open_locale_file;
std::vector<boost::locale::gnu_gettext::messages_info> res;
messages_info.language = "en";
messages_info.country = "US";
res.push_back(messages_info);
for (const auto & [code, glyphs]: glyph_set_per_language)
{
auto pos = code.find("_");
messages_info.language = code.substr(0, pos);
if (pos == std::string::npos)
messages_info.country = "";
else
messages_info.country = code.substr(pos + 1);
res.push_back(messages_info);
}
return res;
}
std::vector<char> open_locale_file(const std::string & file_name, const std::string & encoding)
{
std::vector<char> buffer;
try
{
asset file(file_name);
buffer.resize(file.size());
memcpy(buffer.data(), file.data(), file.size());
}
catch (...)
{
}
return buffer;
}
+3
View File
@@ -23,3 +23,6 @@
#define _F(x) fmt::runtime(boost::locale::gettext(x))
#define _S(x) boost::locale::gettext(x).c_str()
#define _cS(c, x) boost::locale::pgettext(c, x).c_str()
std::vector<boost::locale::gnu_gettext::messages_info> get_locales();
std::vector<char> open_locale_file(const std::string & file_name, const std::string & encoding);