Luanti 5.19.0-dev
Loading...
Searching...
No Matches
translation.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2017 Nore, Nathanaƫlle Courant <nore@mesecons.net>
4
5#pragma once
6
8#include <unordered_map>
9#include <map>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <vector>
14
15class Translations;
17
19{
20public:
21 void setTargetLanguage(const std::string &lang);
22 void loadTranslation(const std::string &filename, const std::string &data);
23 void clear();
24
25 const std::wstring &getTranslation(
26 const std::wstring &textdomain, const std::wstring &s) const;
27 const std::wstring &getPluralTranslation(const std::wstring &textdomain,
28 const std::wstring &s, unsigned long int number) const;
29
30 static const std::string_view getFileBaseName(std::string_view filename);
31 static const std::string_view getFileLanguage(std::string_view filename);
32 // Checks just the file extension
33 static inline bool isTranslationFileType(std::string_view filename)
34 {
35 return !getFileBaseName(filename).empty();
36 }
37 // Checks that the filename includes a language tag
38 static inline bool isTranslationFile(std::string_view filename)
39 {
40 return !getFileLanguage(filename).empty();
41 }
42
43 // for testing
44 inline size_t size()
45 {
46 return m_translations.size() + m_plural_translations.size()/2;
47 }
48
49private:
50 std::string m_target_lang;
51 std::unordered_map<std::wstring, std::wstring> m_translations;
52 std::unordered_map<std::wstring,
53 std::pair<GettextPluralForm::Ptr, std::vector<std::wstring>>> m_plural_translations;
54
55 void addTranslation(const std::wstring &textdomain, const std::wstring &original,
56 const std::wstring &translated, bool is_fallback);
57 void addPluralTranslation(const std::wstring &textdomain,
58 const GettextPluralForm::Ptr &plural,
59 const std::wstring &original,
60 std::vector<std::wstring> &translated,
61 bool is_fallback);
62 static std::wstring unescapeC(const std::wstring &str);
63 static std::optional<std::pair<std::wstring, std::wstring>> parsePoLine(
64 const std::string &line);
65 static bool inEscape(const std::wstring &str, size_t pos);
66 void loadPoEntry(const std::wstring &basefilename,
67 const GettextPluralForm::Ptr &plural_form,
68 const std::map<std::wstring, std::wstring> &entry,
69 bool is_fallback);
70 void loadMoEntry(const std::wstring &basefilename,
71 const GettextPluralForm::Ptr &plural_form,
72 const std::string &original,
73 const std::string &translated,
74 bool is_fallback);
75 void loadTrTranslation(const std::string &data, bool is_fallback);
76 void loadPoTranslation(const std::string &basefilename,
77 const std::string &data, bool is_fallback);
78 void loadMoTranslation(const std::string &basefilename,
79 const std::string &data, bool is_fallback);
80};
std::shared_ptr< GettextPluralForm > Ptr
Definition gettext_plural_form.h:14
Definition translation.h:19
static std::wstring unescapeC(const std::wstring &str)
Definition translation.cpp:246
void loadMoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::string &original, const std::string &translated, bool is_fallback)
Definition translation.cpp:568
static const std::string_view getFileBaseName(std::string_view filename)
Definition translation.cpp:21
void addPluralTranslation(const std::wstring &textdomain, const GettextPluralForm::Ptr &plural, const std::wstring &original, std::vector< std::wstring > &translated, bool is_fallback)
Definition translation.cpp:98
size_t size()
Definition translation.h:44
const std::wstring & getPluralTranslation(const std::wstring &textdomain, const std::wstring &s, unsigned long int number) const
Definition translation.cpp:64
void loadMoTranslation(const std::string &basefilename, const std::string &data, bool is_fallback)
Definition translation.cpp:606
std::string m_target_lang
Definition translation.h:50
void setTargetLanguage(const std::string &lang)
Definition translation.cpp:122
void clear()
Definition translation.cpp:48
const std::wstring & getTranslation(const std::wstring &textdomain, const std::wstring &s) const
Definition translation.cpp:54
std::unordered_map< std::wstring, std::wstring > m_translations
Definition translation.h:51
static bool inEscape(const std::wstring &str, size_t pos)
Definition translation.cpp:402
void loadPoTranslation(const std::string &basefilename, const std::string &data, bool is_fallback)
Definition translation.cpp:465
void loadTranslation(const std::string &filename, const std::string &data)
Definition translation.cpp:690
static bool isTranslationFileType(std::string_view filename)
Definition translation.h:33
void addTranslation(const std::wstring &textdomain, const std::wstring &original, const std::wstring &translated, bool is_fallback)
Definition translation.cpp:82
std::unordered_map< std::wstring, std::pair< GettextPluralForm::Ptr, std::vector< std::wstring > > > m_plural_translations
Definition translation.h:53
static bool isTranslationFile(std::string_view filename)
Definition translation.h:38
static const std::string_view getFileLanguage(std::string_view filename)
Definition translation.cpp:29
static std::optional< std::pair< std::wstring, std::wstring > > parsePoLine(const std::string &line)
Definition translation.cpp:416
void loadTrTranslation(const std::string &data, bool is_fallback)
Definition translation.cpp:127
void loadPoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::map< std::wstring, std::wstring > &entry, bool is_fallback)
Definition translation.cpp:368
Translations * g_client_translations
Definition translation.cpp:18