Luanti 5.17.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 loadTranslation(const std::string &filename, const std::string &data);
22 void clear();
23
24 const std::wstring &getTranslation(
25 const std::wstring &textdomain, const std::wstring &s) const;
26 const std::wstring &getPluralTranslation(const std::wstring &textdomain,
27 const std::wstring &s, unsigned long int number) const;
28
29 static const std::string_view getFileBaseName(std::string_view filename);
30 static const std::string_view getFileLanguage(std::string_view filename);
31 // Checks just the file extension
32 static inline bool isTranslationFileType(std::string_view filename)
33 {
34 return !getFileBaseName(filename).empty();
35 }
36 // Checks that the filename includes a language tag
37 static inline bool isTranslationFile(std::string_view filename)
38 {
39 return !getFileLanguage(filename).empty();
40 }
41
42 // for testing
43 inline size_t size()
44 {
45 return m_translations.size() + m_plural_translations.size()/2;
46 }
47
48private:
49 std::unordered_map<std::wstring, std::wstring> m_translations;
50 std::unordered_map<std::wstring, std::pair<GettextPluralForm::Ptr, std::vector<std::wstring>>> m_plural_translations;
51
52 void addTranslation(const std::wstring &textdomain, const std::wstring &original,
53 const std::wstring &translated);
54 void addPluralTranslation(const std::wstring &textdomain,
55 const GettextPluralForm::Ptr &plural,
56 const std::wstring &original,
57 std::vector<std::wstring> &translated);
58 std::wstring unescapeC(const std::wstring &str);
59 std::optional<std::pair<std::wstring, std::wstring>> parsePoLine(const std::string &line);
60 bool inEscape(const std::wstring &str, size_t pos);
61 void loadPoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::map<std::wstring, std::wstring> &entry);
62 void loadMoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::string &original, const std::string &translated);
63 void loadTrTranslation(const std::string &data);
64 void loadPoTranslation(const std::string &basefilename, const std::string &data);
65 void loadMoTranslation(const std::string &basefilename, const std::string &data);
66};
std::shared_ptr< GettextPluralForm > Ptr
Definition gettext_plural_form.h:14
Definition translation.h:19
std::wstring unescapeC(const std::wstring &str)
Definition translation.cpp:203
static const std::string_view getFileBaseName(std::string_view filename)
Definition translation.cpp:20
size_t size()
Definition translation.h:43
const std::wstring & getPluralTranslation(const std::wstring &textdomain, const std::wstring &s, unsigned long int number) const
Definition translation.cpp:53
void loadMoTranslation(const std::string &basefilename, const std::string &data)
Definition translation.cpp:556
void clear()
Definition translation.cpp:37
void addTranslation(const std::wstring &textdomain, const std::wstring &original, const std::wstring &translated)
Definition translation.cpp:71
const std::wstring & getTranslation(const std::wstring &textdomain, const std::wstring &s) const
Definition translation.cpp:43
void loadTrTranslation(const std::string &data)
Definition translation.cpp:98
std::unordered_map< std::wstring, std::wstring > m_translations
Definition translation.h:49
bool inEscape(const std::wstring &str, size_t pos)
Definition translation.cpp:359
void loadTranslation(const std::string &filename, const std::string &data)
Definition translation.cpp:633
static bool isTranslationFileType(std::string_view filename)
Definition translation.h:32
void addPluralTranslation(const std::wstring &textdomain, const GettextPluralForm::Ptr &plural, const std::wstring &original, std::vector< std::wstring > &translated)
Definition translation.cpp:80
void loadMoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::string &original, const std::string &translated)
Definition translation.cpp:518
std::unordered_map< std::wstring, std::pair< GettextPluralForm::Ptr, std::vector< std::wstring > > > m_plural_translations
Definition translation.h:50
static bool isTranslationFile(std::string_view filename)
Definition translation.h:37
void loadPoEntry(const std::wstring &basefilename, const GettextPluralForm::Ptr &plural_form, const std::map< std::wstring, std::wstring > &entry)
Definition translation.cpp:325
static const std::string_view getFileLanguage(std::string_view filename)
Definition translation.cpp:28
std::optional< std::pair< std::wstring, std::wstring > > parsePoLine(const std::string &line)
Definition translation.cpp:373
void loadPoTranslation(const std::string &basefilename, const std::string &data)
Definition translation.cpp:422
Translations * g_client_translations
Definition translation.cpp:17