FlightGear next
FGTranslate.cxx
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Florent Rougon
2// SPDX-License-Identifier: GPL-2.0-or-later
3
8
9#include <cstddef>
10#include <memory>
11#include <string>
12#include <utility>
13#include <vector>
14
15#include <simgear/debug/logstream.hxx>
16#include <simgear/nasal/cppbind/Ghost.hxx>
17#include <simgear/nasal/cppbind/NasalCallContext.hxx>
18
19#include <Main/locale.hxx>
20#include <Main/globals.hxx>
21
22#include "FGTranslate.hxx"
23#include "TranslationDomain.hxx"
24
25using std::string;
26
28
29FGTranslate::FGTranslate(const std::string& domain)
30 : _domain(globals->get_locale()->getDomain(domain))
31{ }
32
33FGTranslate& FGTranslate::setDomain(const string& domain)
34{
35 // This logs a warning if the domain can't be found.
36 _domain = globals->get_locale()->getDomain(domain);
37 return *this;
38}
39
41FGTranslate::getResource(const string& resourceName) const
42{
43 if (_domain) {
44 return _domain->getResource(resourceName);
45 }
46
47 return {};
48}
49
50std::shared_ptr<TranslationUnit>
51FGTranslate::translationUnit(const string& resourceName, const string& basicId,
52 int index) const
53{
54 const auto resource = getResource(resourceName);
55
56 if (resource) {
57 return resource->translationUnit(basicId, index);
58 }
59
60 return {};
61}
62
63string FGTranslate::get(const string& resourceName, const string& basicId,
64 int index) const
65{
66 const auto resource = getResource(resourceName);
67
68 if (resource) {
69 return resource->get(basicId, index);
70 }
71
72 return {};
73}
74
75string FGTranslate::getPlural(intType cardinalNumber, const string& resourceName,
76 const string& basicId, int index) const
77{
78 const auto resource = getResource(resourceName);
79
80 if (resource) {
81 return resource->getPlural(cardinalNumber, basicId, index);
82 }
83
84 return {};
85}
86
87string FGTranslate::getWithDefault(const string& resourceName,
88 const string& basicId,
89 const string& defaultValue, int index) const
90{
91 const auto resource = getResource(resourceName);
92
93 if (resource) {
94 return resource->getWithDefault(basicId, defaultValue, index);
95 }
96
97 return defaultValue;
98}
99
101 intType cardinalNumber, const string& resourceName, const string& basicId,
102 const string& defaultValue, int index) const
103{
104 const auto resource = getResource(resourceName);
105
106 if (resource) {
107 return resource->getPluralWithDefault(cardinalNumber, basicId,
108 defaultValue, index);
109 }
110
111 return defaultValue;
112}
113
114std::vector<string> FGTranslate::getAll(const string& resourceName,
115 const string& basicId) const
116{
117 const auto resource = getResource(resourceName);
118
119 if (resource) {
120 return resource->getAll(basicId);
121 }
122
123 return {};
124}
125
126std::size_t FGTranslate::getCount(const string& resourceName,
127 const string& basicId) const
128{
129 const auto resource = getResource(resourceName);
130
131 if (resource) {
132 return resource->getCount(basicId);
133 }
134
135 return 0;
136}
137
138static naRef f_get(const FGTranslate& tr, const nasal::CallContext& ctx)
139{
140 if (ctx.argc < 2 || ctx.argc > 3) {
141 ctx.runtimeError("FGTranslate.get(resource, basicId[, index])");
142 }
143
144 const auto resource = ctx.requireArg<std::string>(0);
145 const auto basicId = ctx.requireArg<std::string>(1);
146 const auto index = ctx.getArg<int>(2); // the index defaults to 0
147
148 return ctx.to_nasal(tr.get(std::move(resource), std::move(basicId),
149 index));
150}
151
152static naRef f_getPlural(const FGTranslate& tr, const nasal::CallContext& ctx)
153{
154 if (ctx.argc < 3 || ctx.argc > 4) {
155 ctx.runtimeError(
156 "FGTranslate.getPlural(cardinalNumber, resource, basicId[, index])");
157 }
158
159 const auto cardinalNumber = ctx.requireArg<FGTranslate::intType>(0);
160 const auto resource = ctx.requireArg<std::string>(1);
161 const auto basicId = ctx.requireArg<std::string>(2);
162 const auto index = ctx.getArg<int>(3); // the index defaults to 0
163
164 return ctx.to_nasal(tr.getPlural(cardinalNumber, std::move(resource),
165 std::move(basicId), index));
166}
167
168static naRef f_getWithDefault(const FGTranslate& tr,
169 const nasal::CallContext& ctx)
170{
171 if (ctx.argc < 3 || ctx.argc > 4) {
172 ctx.runtimeError("FGTranslate.getWithDefault(resource, basicId, "
173 "defaultValue[, index])");
174 }
175
176 const auto resource = ctx.requireArg<std::string>(0);
177 const auto basicId = ctx.requireArg<std::string>(1);
178 const auto defaultValue = ctx.requireArg<std::string>(2);
179 const auto index = ctx.getArg<int>(3); // the index defaults to 0
180
181 return ctx.to_nasal(
182 tr.getWithDefault(std::move(resource), std::move(basicId),
183 std::move(defaultValue), index));
184}
185
186static naRef f_getPluralWithDefault(const FGTranslate& tr,
187 const nasal::CallContext& ctx)
188{
189 if (ctx.argc < 4 || ctx.argc > 5) {
190 ctx.runtimeError(
191 "FGTranslate.getPluralWithDefault(cardinalNumber, resource, "
192 "basicId, defaultValue[, index])");
193 }
194
195 const auto cardinalNumber = ctx.requireArg<FGTranslate::intType>(0);
196 const auto resource = ctx.requireArg<std::string>(1);
197 const auto basicId = ctx.requireArg<std::string>(2);
198 const auto defaultValue = ctx.requireArg<std::string>(3);
199 const auto index = ctx.getArg<int>(4); // the index defaults to 0
200
201 return ctx.to_nasal(
202 tr.getPluralWithDefault(cardinalNumber, std::move(resource),
203 std::move(basicId), std::move(defaultValue),
204 index));
205}
206
207static naRef f_translationUnit(const FGTranslate& tr,
208 const nasal::CallContext& ctx)
209{
210 if (ctx.argc < 2 || ctx.argc > 3) {
211 ctx.runtimeError(
212 "FGTranslate.translationUnit(resource, basicId[, index])");
213 }
214
215 const auto resource = ctx.requireArg<std::string>(0);
216 const auto basicId = ctx.requireArg<std::string>(1);
217 const auto index = ctx.getArg<int>(2); // the index defaults to 0
218
219 return ctx.to_nasal(
220 tr.translationUnit(std::move(resource), std::move(basicId), index));
221}
222
223// Static member function
225{
226 using FGTranslateRef = std::shared_ptr<FGTranslate>;
227 using NasalFGTranslate = nasal::Ghost<FGTranslateRef>;
228
229 NasalFGTranslate::init("FGTranslate")
230 .method("getResource", &FGTranslate::getResource)
231 .method("get", &f_get)
232 .method("getPlural", &f_getPlural)
233 .method("getWithDefault", &f_getWithDefault)
234 .method("getPluralWithDefault", &f_getPluralWithDefault)
235 .method("getAll", &FGTranslate::getAll)
236 .method("getCount", &FGTranslate::getCount)
237 .method("translationUnit", &f_translationUnit);
238}
static naRef f_getPlural(const FGTranslate &tr, const nasal::CallContext &ctx)
static naRef f_getWithDefault(const FGTranslate &tr, const nasal::CallContext &ctx)
static naRef f_translationUnit(const FGTranslate &tr, const nasal::CallContext &ctx)
static naRef f_getPluralWithDefault(const FGTranslate &tr, const nasal::CallContext &ctx)
static naRef f_get(const FGTranslate &tr, const nasal::CallContext &ctx)
Class for retrieving translated strings.
Container for all TranslationResource's belonging to a domain.
Class for retrieving translated strings.
flightgear::LanguageInfo::intType intType
std::vector< std::string > getAll(const std::string &resource, const std::string &basicId) const
Get all translations associated to an id (tag name).
FGTranslate(const std::string &domain="core")
Constructor.
std::size_t getCount(const std::string &resource, const std::string &basicId) const
Get the number of translatable strings with a given id (tag name).
std::string getPluralWithDefault(intType cardinalNumber, const std::string &resource, const std::string &basicId, const std::string &defaultValue, int index=0) const
Same as getWithDefault(), but for a string that has plural forms.
std::string getPlural(intType cardinalNumber, const std::string &resource, const std::string &basicId, int index=0) const
Same as get(), but for a string that has plural forms.
static void setupGhost()
Set up a Nasal type that wraps FGTranslate.
flightgear::TranslationDomain::ResourceRef getResource(const std::string &resourceName) const
Get the specified resource.
std::string getWithDefault(const std::string &resource, const std::string &basicId, const std::string &defaultValue, int index=0) const
Get a single translation, with default for missing or empty strings.
std::string get(const std::string &resource, const std::string &basicId, int index=0) const
Get a single translation.
std::shared_ptr< TranslationUnit > translationUnit(const std::string &resource, const std::string &basicId, int index=0) const
Return a shared pointer to a TranslationUnit.
FGTranslate & setDomain(const std::string &domain)
Change the domain from which to retrieve translations.
std::shared_ptr< TranslationResource > ResourceRef
Class that holds translation resources within a domain.
FGGlobals * globals
Definition globals.cxx:142
FlightGear Localization Support.