2017-07-17 21:50:40 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-07-17 21:50:40 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Translation.h"
|
2017-07-17 21:50:40 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iterator>
|
2020-06-02 20:13:23 +00:00
|
|
|
#include <string>
|
2017-07-17 21:50:40 +00:00
|
|
|
|
2022-10-12 02:21:51 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QTranslator>
|
|
|
|
|
2017-07-17 21:50:40 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2017-07-17 21:50:40 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2021-12-31 02:00:39 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2019-03-04 19:49:00 +00:00
|
|
|
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
|
|
|
|
2018-03-10 12:38:23 +00:00
|
|
|
#include "UICommon/UICommon.h"
|
2017-07-17 21:50:40 +00:00
|
|
|
|
|
|
|
constexpr u32 MO_MAGIC_NUMBER = 0x950412de;
|
|
|
|
|
|
|
|
static u16 ReadU16(const char* data)
|
|
|
|
{
|
|
|
|
u16 value;
|
|
|
|
std::memcpy(&value, data, sizeof(value));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ReadU32(const char* data)
|
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
std::memcpy(&value, data, sizeof(value));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MoIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
using value_type = const char*;
|
|
|
|
using difference_type = s64;
|
|
|
|
using pointer = value_type;
|
|
|
|
using reference = value_type;
|
|
|
|
|
|
|
|
explicit MoIterator(const char* data, u32 table_offset, u32 index = 0)
|
|
|
|
: m_data{data}, m_table_offset{table_offset}, m_index{index}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the actual underlying logic of accessing a Mo file. Patterned after the
|
|
|
|
// boost::iterator_facade library, which nicely separates out application logic from
|
|
|
|
// iterator-concept logic.
|
|
|
|
void advance(difference_type n) { m_index += n; }
|
2018-03-17 19:48:52 +00:00
|
|
|
difference_type distance_to(const MoIterator& other) const
|
|
|
|
{
|
|
|
|
return static_cast<difference_type>(other.m_index) - m_index;
|
|
|
|
}
|
2017-07-17 21:50:40 +00:00
|
|
|
reference dereference() const
|
|
|
|
{
|
|
|
|
u32 offset = ReadU32(&m_data[m_table_offset + m_index * 8 + 4]);
|
|
|
|
return &m_data[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed for Iterator concept
|
|
|
|
reference operator*() const { return dereference(); }
|
|
|
|
MoIterator& operator++()
|
|
|
|
{
|
|
|
|
advance(1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed for InputIterator concept
|
|
|
|
bool operator==(const MoIterator& other) const { return distance_to(other) == 0; }
|
|
|
|
pointer operator->() const { return dereference(); }
|
|
|
|
MoIterator operator++(int)
|
|
|
|
{
|
|
|
|
MoIterator tmp(*this);
|
|
|
|
advance(1);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed for BidirectionalIterator concept
|
|
|
|
MoIterator& operator--()
|
|
|
|
{
|
|
|
|
advance(-1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
MoIterator operator--(int)
|
|
|
|
{
|
|
|
|
MoIterator tmp(*this);
|
|
|
|
advance(-1);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed for RandomAccessIterator concept
|
|
|
|
bool operator<(const MoIterator& other) const { return distance_to(other) > 0; }
|
|
|
|
bool operator<=(const MoIterator& other) const { return distance_to(other) >= 0; }
|
|
|
|
bool operator>(const MoIterator& other) const { return distance_to(other) < 0; }
|
|
|
|
bool operator>=(const MoIterator& other) const { return distance_to(other) <= 0; }
|
|
|
|
reference operator[](difference_type n) const { return *(*this + n); }
|
|
|
|
MoIterator& operator+=(difference_type n)
|
|
|
|
{
|
|
|
|
advance(n);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
MoIterator& operator-=(difference_type n)
|
|
|
|
{
|
|
|
|
advance(-n);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
friend MoIterator operator+(difference_type n, const MoIterator& it) { return it + n; }
|
|
|
|
friend MoIterator operator+(const MoIterator& it, difference_type n)
|
|
|
|
{
|
|
|
|
MoIterator tmp(it);
|
|
|
|
tmp += n;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
difference_type operator-(const MoIterator& other) const { return other.distance_to(*this); }
|
|
|
|
friend MoIterator operator-(difference_type n, const MoIterator& it) { return it - n; }
|
|
|
|
friend MoIterator operator-(const MoIterator& it, difference_type n)
|
|
|
|
{
|
|
|
|
MoIterator tmp(it);
|
|
|
|
tmp -= n;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* m_data;
|
|
|
|
u32 m_table_offset;
|
|
|
|
u32 m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MoFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MoFile() = default;
|
|
|
|
explicit MoFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
File::IOFile file(filename, "rb");
|
|
|
|
m_data.resize(file.GetSize());
|
|
|
|
file.ReadBytes(m_data.data(), m_data.size());
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
2020-11-26 02:13:50 +00:00
|
|
|
WARN_LOG_FMT(COMMON, "Error reading MO file '{}'", filename);
|
2017-07-17 21:50:40 +00:00
|
|
|
m_data = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 02:13:50 +00:00
|
|
|
const u32 magic = ReadU32(&m_data[0]);
|
2017-07-17 21:50:40 +00:00
|
|
|
if (magic != MO_MAGIC_NUMBER)
|
|
|
|
{
|
2020-11-26 02:13:50 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "MO file '{}' has bad magic number {:x}\n", filename, magic);
|
2017-07-17 21:50:40 +00:00
|
|
|
m_data = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 02:13:50 +00:00
|
|
|
const u16 version_major = ReadU16(&m_data[4]);
|
2017-07-17 21:50:40 +00:00
|
|
|
if (version_major > 1)
|
|
|
|
{
|
2020-11-26 02:13:50 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "MO file '{}' has unsupported version number {}", filename,
|
|
|
|
version_major);
|
2017-07-17 21:50:40 +00:00
|
|
|
m_data = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_number_of_strings = ReadU32(&m_data[8]);
|
|
|
|
m_offset_original_table = ReadU32(&m_data[12]);
|
|
|
|
m_offset_translation_table = ReadU32(&m_data[16]);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetNumberOfStrings() const { return m_number_of_strings; }
|
|
|
|
const char* Translate(const char* original_string) const
|
|
|
|
{
|
|
|
|
const MoIterator begin(m_data.data(), m_offset_original_table);
|
|
|
|
const MoIterator end(m_data.data(), m_offset_original_table, m_number_of_strings);
|
|
|
|
auto iter = std::lower_bound(begin, end, original_string,
|
|
|
|
[](const char* a, const char* b) { return strcmp(a, b) < 0; });
|
|
|
|
|
2018-03-17 19:48:52 +00:00
|
|
|
if (iter == end || strcmp(*iter, original_string) != 0)
|
2020-11-12 18:44:44 +00:00
|
|
|
return nullptr;
|
2017-07-17 21:50:40 +00:00
|
|
|
|
|
|
|
u32 offset = ReadU32(&m_data[m_offset_translation_table + std::distance(begin, iter) * 8 + 4]);
|
|
|
|
return &m_data[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<char> m_data;
|
|
|
|
u32 m_number_of_strings = 0;
|
|
|
|
u32 m_offset_original_table = 0;
|
|
|
|
u32 m_offset_translation_table = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MoTranslator : public QTranslator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using QTranslator::QTranslator;
|
|
|
|
|
|
|
|
bool isEmpty() const override { return m_mo_file.GetNumberOfStrings() == 0; }
|
|
|
|
bool load(const std::string& filename)
|
|
|
|
{
|
|
|
|
m_mo_file = MoFile(filename);
|
|
|
|
return !isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString translate(const char* context, const char* source_text,
|
|
|
|
const char* disambiguation = nullptr, int n = -1) const override
|
|
|
|
{
|
2020-11-12 18:44:44 +00:00
|
|
|
const char* translated_text;
|
|
|
|
|
2020-06-02 20:13:23 +00:00
|
|
|
if (disambiguation)
|
|
|
|
{
|
|
|
|
std::string combined_string = disambiguation;
|
|
|
|
combined_string += '\4';
|
|
|
|
combined_string += source_text;
|
2020-11-12 18:44:44 +00:00
|
|
|
translated_text = m_mo_file.Translate(combined_string.c_str());
|
2020-06-02 20:13:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-12 18:44:44 +00:00
|
|
|
translated_text = m_mo_file.Translate(source_text);
|
2020-06-02 20:13:23 +00:00
|
|
|
}
|
2020-11-12 18:44:44 +00:00
|
|
|
|
|
|
|
return QString::fromUtf8(translated_text ? translated_text : source_text);
|
2017-07-17 21:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MoFile m_mo_file;
|
|
|
|
};
|
|
|
|
|
2018-04-17 13:10:05 +00:00
|
|
|
static QStringList FindPossibleLanguageCodes(const QString& exact_language_code)
|
2017-07-17 21:50:40 +00:00
|
|
|
{
|
|
|
|
QStringList possible_language_codes;
|
|
|
|
possible_language_codes << exact_language_code;
|
|
|
|
|
|
|
|
// Qt likes to separate language, script, and country by hyphen, but on disk they're separated by
|
|
|
|
// underscores.
|
|
|
|
possible_language_codes.replaceInStrings(QStringLiteral("-"), QStringLiteral("_"));
|
|
|
|
|
|
|
|
// Try successively dropping subtags (like the stock QTranslator, and as specified by RFC 4647
|
|
|
|
// "Matching of Language Tags").
|
|
|
|
// Example: fr_Latn_CA -> fr_Latn -> fr
|
|
|
|
for (auto lang : QStringList(possible_language_codes))
|
|
|
|
{
|
|
|
|
while (lang.contains(QLatin1Char('_')))
|
|
|
|
{
|
|
|
|
lang = lang.left(lang.lastIndexOf(QLatin1Char('_')));
|
|
|
|
possible_language_codes << lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// On macOS, Chinese (Simplified) and Chinese (Traditional) are represented as zh-Hans and
|
|
|
|
// zh-Hant, but on Linux they're represented as zh-CN and zh-TW. Qt should probably include the
|
|
|
|
// script subtags on Linux, but it doesn't.
|
2018-03-10 11:52:02 +00:00
|
|
|
const int hans_index = possible_language_codes.indexOf(QStringLiteral("zh_Hans"));
|
|
|
|
if (hans_index != -1)
|
|
|
|
possible_language_codes.insert(hans_index + 1, QStringLiteral("zh_CN"));
|
|
|
|
const int hant_index = possible_language_codes.indexOf(QStringLiteral("zh_Hant"));
|
|
|
|
if (hant_index != -1)
|
|
|
|
possible_language_codes.insert(hant_index + 1, QStringLiteral("zh_TW"));
|
2017-07-17 21:50:40 +00:00
|
|
|
|
|
|
|
return possible_language_codes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TryInstallTranslator(const QString& exact_language_code)
|
|
|
|
{
|
|
|
|
for (const auto& qlang : FindPossibleLanguageCodes(exact_language_code))
|
|
|
|
{
|
|
|
|
std::string lang = qlang.toStdString();
|
|
|
|
auto filename =
|
|
|
|
#if defined _WIN32
|
2022-10-12 02:21:51 +00:00
|
|
|
fmt::format("{}/Languages/{}.mo", File::GetExeDirectory(), lang)
|
2017-07-17 21:50:40 +00:00
|
|
|
#elif defined __APPLE__
|
2022-10-12 02:21:51 +00:00
|
|
|
fmt::format("{}/Contents/Resources/{}.lproj/dolphin-emu.mo", File::GetBundleDirectory(),
|
|
|
|
lang)
|
2018-10-28 11:33:13 +00:00
|
|
|
#elif defined LINUX_LOCAL_DEV
|
2022-10-12 02:21:51 +00:00
|
|
|
fmt::format("{}/../Source/Core/DolphinQt/{}/dolphin-emu.mo", File::GetExeDirectory(), lang)
|
2017-07-17 21:50:40 +00:00
|
|
|
#else
|
2022-10-12 02:21:51 +00:00
|
|
|
fmt::format("{}/../locale/{}/LC_MESSAGES/dolphin-emu.mo", DATA_DIR, lang)
|
2017-07-17 21:50:40 +00:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
auto* translator = new MoTranslator(QApplication::instance());
|
|
|
|
if (translator->load(filename))
|
|
|
|
{
|
|
|
|
QApplication::instance()->installTranslator(translator);
|
2018-03-10 12:38:23 +00:00
|
|
|
|
|
|
|
QLocale::setDefault(QLocale(exact_language_code));
|
|
|
|
UICommon::SetLocale(exact_language_code.toStdString());
|
|
|
|
|
2017-07-17 21:50:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
translator->deleteLater();
|
|
|
|
}
|
2020-11-26 02:13:50 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "No suitable translation file found");
|
2017-07-17 21:50:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Translation::Initialize()
|
|
|
|
{
|
|
|
|
// Hook up Dolphin internal translation
|
2019-06-17 03:45:37 +00:00
|
|
|
Common::RegisterStringTranslator(
|
|
|
|
[](const char* text) { return QObject::tr(text).toStdString(); });
|
2017-07-17 21:50:40 +00:00
|
|
|
|
|
|
|
// Hook up Qt translations
|
2021-12-31 02:00:39 +00:00
|
|
|
std::string configured_language = Config::Get(Config::MAIN_INTERFACE_LANGUAGE);
|
2017-07-17 21:50:40 +00:00
|
|
|
if (!configured_language.empty())
|
|
|
|
{
|
|
|
|
if (TryInstallTranslator(QString::fromStdString(configured_language)))
|
|
|
|
return;
|
|
|
|
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::warning(
|
2017-07-17 21:50:40 +00:00
|
|
|
nullptr, QObject::tr("Error"),
|
|
|
|
QObject::tr("Error loading selected language. Falling back to system default."));
|
2021-12-31 02:00:39 +00:00
|
|
|
Config::SetBase(Config::MAIN_INTERFACE_LANGUAGE, "");
|
2017-07-17 21:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& lang : QLocale::system().uiLanguages())
|
|
|
|
{
|
|
|
|
if (TryInstallTranslator(lang))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|