mirror of
https://github.com/Vita3K/Vita3K.git
synced 2026-07-11 01:34:23 +02:00
i18n: Make use of static assets instead of hardcoded base (#3968)
This commit is contained in:
committed by
GitHub
parent
d7c48ec07c
commit
c93dcb7ee1
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <util/fs.h>
|
||||
|
||||
class QApplication;
|
||||
|
||||
@@ -31,8 +32,8 @@ struct UiLanguageOption {
|
||||
std::string_view native_name;
|
||||
};
|
||||
|
||||
std::span<const UiLanguageOption> ui_language_options();
|
||||
bool apply_ui_language(QApplication &app, std::string_view configured_tag);
|
||||
std::span<const UiLanguageOption> ui_language_options(fs::path &static_assets_path);
|
||||
bool apply_ui_language(QApplication &app, std::string_view configured_tag, fs::path &static_assets_path);
|
||||
QString language_name(std::string_view tag);
|
||||
|
||||
} // namespace gui::i18n
|
||||
|
||||
@@ -61,19 +61,18 @@ const std::array<UiLanguageOption, 23> k_ui_languages = { {
|
||||
|
||||
std::unique_ptr<QTranslator> s_translator;
|
||||
|
||||
QStringList translation_search_paths() {
|
||||
const QString app_dir = QCoreApplication::applicationDirPath();
|
||||
QStringList translation_search_paths(fs::path &static_assets_path) {
|
||||
return {
|
||||
app_dir + QStringLiteral("/translations"),
|
||||
app_dir + QStringLiteral("/../Resources/translations"),
|
||||
QString::fromUtf8((static_assets_path / "translations").string().c_str()),
|
||||
QString::fromUtf8((static_assets_path / "../Resources/translations").string().c_str()),
|
||||
};
|
||||
}
|
||||
|
||||
QSet<QString> available_translation_tags() {
|
||||
QSet<QString> available_translation_tags(fs::path &static_assets_path) {
|
||||
const QString prefix = QStringLiteral("vita3k_");
|
||||
|
||||
QSet<QString> tags;
|
||||
for (const QString &path : translation_search_paths()) {
|
||||
for (const QString &path : translation_search_paths(static_assets_path)) {
|
||||
const QDir dir(path);
|
||||
const auto entries = dir.entryList({ QStringLiteral("vita3k_*.qm"), QStringLiteral("vita3k_*.ts") }, QDir::Files);
|
||||
for (const QString &entry : entries) {
|
||||
@@ -92,12 +91,12 @@ QSet<QString> available_translation_tags() {
|
||||
|
||||
} // namespace
|
||||
|
||||
std::span<const UiLanguageOption> ui_language_options() {
|
||||
std::span<const UiLanguageOption> ui_language_options(fs::path &static_assets_path) {
|
||||
static std::vector<UiLanguageOption> available_languages;
|
||||
|
||||
available_languages.clear();
|
||||
|
||||
const QSet<QString> tags = available_translation_tags();
|
||||
const QSet<QString> tags = available_translation_tags(static_assets_path);
|
||||
if (tags.isEmpty()) {
|
||||
available_languages.push_back(k_ui_languages.front());
|
||||
return available_languages;
|
||||
@@ -124,7 +123,7 @@ QString language_name(std::string_view tag) {
|
||||
return QString::fromUtf8(tag.data(), static_cast<int>(tag.size()));
|
||||
}
|
||||
|
||||
bool apply_ui_language(QApplication &app, std::string_view configured_tag) {
|
||||
bool apply_ui_language(QApplication &app, std::string_view configured_tag, fs::path &static_assets_path) {
|
||||
if (s_translator)
|
||||
app.removeTranslator(s_translator.get());
|
||||
|
||||
@@ -134,7 +133,7 @@ bool apply_ui_language(QApplication &app, std::string_view configured_tag) {
|
||||
? QLocale::system()
|
||||
: QLocale(QString::fromUtf8(configured_tag.data(), static_cast<int>(configured_tag.size())));
|
||||
|
||||
for (const QString &path : translation_search_paths()) {
|
||||
for (const QString &path : translation_search_paths(static_assets_path)) {
|
||||
if (s_translator->load(locale, QStringLiteral("vita3k"), QStringLiteral("_"), path)) {
|
||||
app.installTranslator(s_translator.get());
|
||||
return true;
|
||||
|
||||
@@ -840,7 +840,7 @@ bool MainWindow::handle_pending_app_launch_request() {
|
||||
}
|
||||
|
||||
void MainWindow::apply_ui_language(const QString &locale_tag) {
|
||||
gui::i18n::apply_ui_language(*qApp, locale_tag.toStdString());
|
||||
gui::i18n::apply_ui_language(*qApp, locale_tag.toStdString(), emuenv.static_assets_path);
|
||||
}
|
||||
|
||||
void MainWindow::init_current_user() {
|
||||
|
||||
@@ -530,7 +530,8 @@ void SettingsDialog::load_config() {
|
||||
: false);
|
||||
m_ui->ui_language_box->clear();
|
||||
m_ui->ui_language_box->addItem(tr("System Default"), QStringLiteral(""));
|
||||
for (const auto &language : gui::i18n::ui_language_options()) {
|
||||
|
||||
for (const auto &language : gui::i18n::ui_language_options(emuenv.static_assets_path)) {
|
||||
const QString tag = QString::fromUtf8(language.tag.data(), static_cast<int>(language.tag.size()));
|
||||
const QString native_name = QString::fromUtf8(language.native_name.data(), static_cast<int>(language.native_name.size()));
|
||||
m_ui->ui_language_box->addItem(native_name, tag);
|
||||
|
||||
+1
-1
@@ -156,7 +156,7 @@ int main(int argc, char *argv[]) {
|
||||
return InitConfigFailed;
|
||||
}
|
||||
|
||||
gui::i18n::apply_ui_language(app, cfg.user_lang);
|
||||
gui::i18n::apply_ui_language(app, cfg.user_lang, emuenv.static_assets_path);
|
||||
|
||||
#ifdef _WIN32
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user