mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +00:00
Gettext and plural support for client-side translations (#14726)
--------- Co-authored-by: Ekdohibs <nathanael.courant@laposte.net> Co-authored-by: y5nw <y5nw@protonmail.com> Co-authored-by: rubenwardy <rw@rubenwardy.com>
This commit is contained in:
parent
dbbe0ca065
commit
e3aa79cffb
28 changed files with 1360 additions and 74 deletions
|
@ -19,8 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "gettext_plural_form.h"
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Translations;
|
||||
#ifndef SERVER
|
||||
|
@ -30,11 +34,39 @@ extern Translations *g_client_translations;
|
|||
class Translations
|
||||
{
|
||||
public:
|
||||
void loadTranslation(const std::string &data);
|
||||
void loadTranslation(const std::string &filename, const std::string &data);
|
||||
void clear();
|
||||
const std::wstring &getTranslation(const std::wstring &textdomain,
|
||||
const std::wstring &s) const;
|
||||
const std::wstring &getTranslation(
|
||||
const std::wstring &textdomain, const std::wstring &s) const;
|
||||
const std::wstring &getPluralTranslation(const std::wstring &textdomain,
|
||||
const std::wstring &s, unsigned long int number) const;
|
||||
static const std::string_view getFileLanguage(const std::string &filename);
|
||||
static inline bool isTranslationFile(const std::string &filename)
|
||||
{
|
||||
return getFileLanguage(filename) != "";
|
||||
}
|
||||
// for testing
|
||||
inline size_t size()
|
||||
{
|
||||
return m_translations.size() + m_plural_translations.size()/2;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::wstring, std::wstring> m_translations;
|
||||
std::unordered_map<std::wstring, std::pair<GettextPluralForm::Ptr, std::vector<std::wstring>>> m_plural_translations;
|
||||
|
||||
void addTranslation(const std::wstring &textdomain, const std::wstring &original,
|
||||
const std::wstring &translated);
|
||||
void addPluralTranslation(const std::wstring &textdomain,
|
||||
const GettextPluralForm::Ptr &plural,
|
||||
const std::wstring &original,
|
||||
std::vector<std::wstring> &translated);
|
||||
std::wstring unescapeC(const std::wstring &str);
|
||||
std::optional<std::pair<std::wstring, std::wstring>> parsePoLine(const std::string &line);
|
||||
bool inEscape(const std::wstring &str, size_t pos);
|
||||
void loadPoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::map<std::wstring, std::wstring> &entry);
|
||||
void loadMoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::string &original, const std::string &translated);
|
||||
void loadTrTranslation(const std::string &data);
|
||||
void loadPoTranslation(const std::string &basefilename, const std::string &data);
|
||||
void loadMoTranslation(const std::string &basefilename, const std::string &data);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue