16#include <simgear/debug/logstream.hxx>
17#include <simgear/nasal/cppbind/Ghost.hxx>
26 : _name(std::move(
name))
30 std::string sourceText,
33 _map.emplace(KeyType(std::move(
name), index),
38 std::string
name,
int index, std::string targetText)
40 const auto key = std::make_pair(std::move(
name), index);
53 std::string
name,
int index, std::vector<std::string> targetTexts)
55 const auto key = std::make_pair(std::move(
name), index);
64TranslationResource::TranslationUnitRef
67 auto it = _map.find(std::make_pair(
name, index));
68 if (it != _map.end()) {
83 if (translUnit->getPluralStatus()) {
84 SG_LOG(SG_GENERAL, SG_DEV_ALERT,
85 "TranslationResource::get() or "
86 "TranslationResource::getWithDefault() used on translatable "
87 "string '" << _name <<
"/" << basicId <<
":" << index <<
88 "' defined with has-plural=\"true\" in the default translation. "
89 "Use TranslationResource::getPlural() or "
90 "TranslationResource::getPluralWithDefault() instead.");
91 return translUnit->getSourceText();
93 return translUnit->getTranslation();
98 const string& basicId,
int index)
const
106 if (!translUnit->getPluralStatus()) {
107 SG_LOG(SG_GENERAL, SG_DEV_ALERT,
108 "TranslationResource::getPlural() or "
109 "TranslationResource::getPluralWithDefault() used on "
110 "translatable string '" << _name <<
"/" << basicId <<
":" <<
111 index <<
"' that isn't defined with has-plural=\"true\" in the "
112 "default translation. Use TranslationResource::get() or "
113 "TranslationResource::getWithDefault() instead.");
114 return translUnit->getSourceText();
116 return translUnit->getTranslation(cardinalNumber);
123 const string& basicId,
const string& defaultValue,
int index)
const
125 const string result =
get(basicId, index);
127 return result.empty() ? defaultValue : result;
131 intType cardinalNumber,
const string& basicId,
const string& defaultValue,
134 const string result =
getPlural(cardinalNumber, basicId, index);
136 return result.empty() ? defaultValue : result;
141 vector<string> result;
142 decltype(_map)::const_iterator it;
145 (it = _map.find(std::make_pair(
name, index))) != _map.end();
147 const auto& transUnit = it->second;
149 const string targetText = transUnit->getTargetText(0);
151 targetText.empty() ? transUnit->getSourceText() : targetText);
159 std::size_t index = 0;
161 while (_map.find(std::make_pair(
name, index)) != _map.end()) {
170 if (ctx.argc < 1 || ctx.argc > 2) {
171 ctx.runtimeError(
"TranslationResource.get(basicId[, index])");
174 const auto basicId = ctx.requireArg<std::string>(0);
175 const auto index = ctx.getArg<
int>(1);
177 return ctx.to_nasal(tr.
get(std::move(basicId), index));
182 if (ctx.argc < 2 || ctx.argc > 3) {
184 "TranslationResource.getPlural(cardinalNumber, basicId[, index])");
188 const auto basicId = ctx.requireArg<std::string>(1);
189 const auto index = ctx.getArg<
int>(2);
191 return ctx.to_nasal(tr.
getPlural(cardinalNumber, std::move(basicId),
196 const nasal::CallContext& ctx)
198 if (ctx.argc < 2 || ctx.argc > 3) {
199 ctx.runtimeError(
"TranslationResource.getWithDefault(basicId, "
200 "defaultValue[, index])");
203 const auto basicId = ctx.requireArg<std::string>(0);
204 const auto defaultValue = ctx.requireArg<std::string>(1);
205 const auto index = ctx.getArg<
int>(2);
208 tr.
getWithDefault(std::move(basicId), std::move(defaultValue), index));
212 const nasal::CallContext& ctx)
214 if (ctx.argc < 3 || ctx.argc > 4) {
216 "TranslationResource.getPluralWithDefault(cardinalNumber, "
217 "basicId, defaultValue[, index])");
221 const auto basicId = ctx.requireArg<std::string>(1);
222 const auto defaultValue = ctx.requireArg<std::string>(2);
223 const auto index = ctx.getArg<
int>(3);
227 std::move(defaultValue), index));
231 const nasal::CallContext& ctx)
233 if (ctx.argc < 1 || ctx.argc > 2) {
235 "TranslationResource.translationUnit(basicId[, index])");
238 const auto basicId = ctx.requireArg<std::string>(0);
239 const auto index = ctx.getArg<
int>(1);
248 using TranslationResourceRef = std::shared_ptr<TranslationResource>;
249 using NasalTranslationResource = nasal::Ghost<TranslationResourceRef>;
251 NasalTranslationResource::init(
"TranslationResource")
252 .method(
"get", &
f_get)
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)
static naRef f_getPluralWithDefault(const TranslationResource &tr, const nasal::CallContext &ctx)
static naRef f_translationUnit(const TranslationResource &tr, const nasal::CallContext &ctx)
static naRef f_getWithDefault(const TranslationResource &tr, const nasal::CallContext &ctx)
static naRef f_getPlural(const TranslationResource &tr, const nasal::CallContext &ctx)
static naRef f_get(const TranslationResource &tr, const nasal::CallContext &ctx)
Container class for related translation units.
Container class for a string and its translation.
Class that holds translation units within a resource (“context”)
flightgear::LanguageInfo::intType intType
std::shared_ptr< TranslationUnit > translationUnit(const std::string &name, int index=0) const
Return a shared pointer to a TranslationUnit.
TranslationResource()=delete
std::string getWithDefault(const std::string &basicId, const std::string &defaultValue, int index=0) const
Get a single translation, with default for missing or empty strings.
static void setupGhost()
Set up a Nasal type that wraps TranslationResource.
std::vector< std::string > getAll(const std::string &name) const
Get translations for all strings with a given element name.
void setFirstTargetText(std::string name, int index, std::string targetText)
Set the first target text of a translation unit.
void setTargetTexts(std::string name, int index, std::vector< std::string > targetTexts)
Set all target texts of a translation unit.
std::string getPlural(intType cardinalNumber, const std::string &basicId, int index=0) const
Same as get(), but for a string that has plural forms.
std::string getPluralWithDefault(intType cardinalNumber, 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 get(const std::string &basicId, int index=0) const
Get a single translation.
void addTranslationUnit(std::string name, int index, std::string sourceText, bool hasPlural=false)
Add a translation unit to the TranslationResource.
std::size_t getCount(const std::string &name) const
Get the number of translated strings with the given element name.
Class holding a source string and its translation in a language.