nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
translations.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_TRANSLATIONS_H_
2#define NANOMMO_CLIENT_TRANSLATIONS_H_
3
4#include <array>
5#include <cstdint>
6#include <functional>
7#include <map>
8#include <string>
9#include <string_view>
10#include <vector>
11
12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14
15namespace nanommo {
16
17/// How a language picks among a plural's forms by the count, as a PO file's
18/// Plural-Forms says, such as Romanian's "nplurals=3; plural=(n==1 ? 0 :
19/// (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);": a C expression of n, with
20/// ?:, ||, &&, the comparisons, %, numbers and brackets.
22 public:
23 /// English's: one form for 1, and the other for any other count.
24 PluralRule() = default;
25
26 /// A rule that can't be read, or that picks a form past its last, is
27 /// refused.
28 static absl::StatusOr<PluralRule> Parse(std::string_view forms);
29
30 int forms() const { return forms_; }
31
32 /// The form for the count, from 0.
33 int Form(uint64_t n) const;
34
35 private:
36 int forms_ = 2;
37 std::string plural_ = "n != 1";
38};
39
40/// A language's translations, from a gettext PO file: what each English
41/// string is in the language, by its context, if it has one, with the rule
42/// its plurals follow.
43class Catalog {
44 public:
45 /// Reads a PO file. What's marked fuzzy, or left untranslated, is left out,
46 /// and so is a translation with a placeholder, $0 to $9, that its English
47 /// doesn't have, which absl::Substitute would have no argument for: each
48 /// of those shows in English.
49 static absl::StatusOr<Catalog> Parse(std::string_view po);
50
51 /// The pseudo-language, from the template: each of its strings with its
52 /// letters marked, a third longer, in brackets, so that the screen shows
53 /// what isn't translated, and what a longer language would cut off.
54 static absl::StatusOr<Catalog> PseudoFrom(std::string_view pot);
55
56 /// The translation, or null for one there isn't.
57 const char* Find(std::string_view context, std::string_view text) const;
58
59 /// A plural's form for the count, by its English for one, or null.
60 const char* Find(std::string_view context, std::string_view one,
61 uint64_t n) const;
62
63 private:
64 // By the context and the English, as gettext keys them: the two with an
65 // EOT between, or the English alone. A plural has a string for each form.
66 std::map<std::string, std::vector<std::string>, std::less<>> translations_;
67 PluralRule plurals_;
68};
69
70/// The English made over for the pseudo-language: "[Șéțțîñgș ~~~]".
71std::string Pseudo(std::string_view english);
72
73/// A language the interface can be in: its code, which the settings keep and
74/// its PO file is named by, and its name, in itself.
75struct Language {
76 const char* code;
77 const char* name;
78};
79
80/// The pseudo-language's code: it's made from the template, nanommo.pot.
81inline constexpr std::string_view kPseudo = "pseudo";
82
83/// English, which is the code's own, Romanian, from ro.po, and the
84/// pseudo-language.
85inline constexpr std::array<Language, 3> kLanguages = {{
86 {"", "English"},
87 {"ro", "Română"},
88 {"pseudo", "Pseudo"},
89}};
90
91/// Where the PO files are, next to the client.
92std::string LanguageFolder();
93
94/// Puts the interface in the language, by its code, from its PO file in the
95/// folder: none for English. One that can't be read leaves it in English.
96absl::Status UseLanguage(std::string_view code, const std::string& folder);
97
98/// The text in the language in use, or the text itself where there's no
99/// translation, which is all of them in English. The pointer lasts until the
100/// language changes, or, untranslated, as long as the text does.
101const char* Tr(const char* text);
102
103/// In a context, for a string that could mean one of two things, or one of
104/// the content's names, such as "item 8".
105const char* Tr(std::string_view context, const char* text);
106
107/// The form for the count: in English, one for 1 and many for the rest.
108const char* TrN(const char* one, const char* many, uint64_t n);
109const char* TrN(std::string_view context, const char* one, const char* many,
110 uint64_t n);
111
112/// An ImGui label in the language in use, with the English one as its ID
113/// after "###", so that a widget stays itself in every language.
114const char* TrLabel(const char* label);
115
116/// Marks a string for the template without translating it, as gettext's N_
117/// does, for a table that's translated where it's shown, and in a context,
118/// which Tr is given there too.
119constexpr const char* N_(const char* text) { return text; }
120constexpr const char* N_(const char* /*context*/, const char* text) {
121 return text;
122}
123
124} // namespace nanommo
125
126#endif // NANOMMO_CLIENT_TRANSLATIONS_H_
static absl::StatusOr< Catalog > Parse(std::string_view po)
const char * Find(std::string_view context, std::string_view text) const
The translation, or null for one there isn't.
static absl::StatusOr< Catalog > PseudoFrom(std::string_view pot)
static absl::StatusOr< PluralRule > Parse(std::string_view forms)
int Form(uint64_t n) const
The form for the count, from 0.
PluralRule()=default
English's: one form for 1, and the other for any other count.
Definition chat.cc:9
constexpr const char * N_(const char *text)
std::string Pseudo(std::string_view english)
The English made over for the pseudo-language: "[Șéțțîñgș ~~~]".
constexpr std::array< Language, 3 > kLanguages
const char * Tr(const char *text)
const char * TrN(const char *one, const char *many, uint64_t n)
The form for the count: in English, one for 1 and many for the rest.
const char * TrLabel(const char *label)
constexpr std::string_view kPseudo
The pseudo-language's code: it's made from the template, nanommo.pot.
std::string LanguageFolder()
Where the PO files are, next to the client.
absl::Status UseLanguage(std::string_view code, const std::string &folder)
const char * code
const char * name