16#include <simgear/debug/logstream.hxx>
17#include <simgear/nasal/cppbind/Ghost.hxx>
18#include <simgear/nasal/cppbind/NasalCallContext.hxx>
28 const std::vector<std::string> targetTexts,
30 : _sourceText(std::move(sourceText)),
31 _targetTexts(std::move(targetTexts)),
42 _sourceText = std::move(text);
47 if (
static_cast<std::size_t
>(pluralFormIndex) >= _targetTexts.size()) {
51 return _targetTexts[pluralFormIndex];
56 return _targetTexts.size();
61 if (
static_cast<std::size_t
>(pluralFormIndex) >= _targetTexts.size()) {
62 _targetTexts.resize(pluralFormIndex + 1);
65 _targetTexts[pluralFormIndex] = std::move(text);
70 _targetTexts = std::move(texts);
80 _hasPlural = hasPlural;
88 if (nbTargetTexts == 0) {
104 SG_LOG(SG_GENERAL, SG_WARN,
105 "TranslationUnit::getTranslation(intType cardinalNumber) called "
106 "for “" <<
getSourceText() <<
"”), however this string wasn't "
107 "declared with has-plural=\"true\" in the default translation");
112 if (nbTargetTexts == 0) {
116 const std::string languageId =
globals->get_locale()->getLanguageId();
117 const std::size_t pluralFormIndex =
119 assert(pluralFormIndex < nbTargetTexts);
131 nasal::CallContext ctx)
134 intType cardinalNumber;
140 "TranslationUnit has plural status 1, therefore its "
141 "getTranslation() method requires an argument");
147 "TranslationUnit has plural status 0, therefore its "
148 "getTranslation() method must be called with no argument");
150 cardinalNumber = ctx.requireArg<intType>(0);
154 "TranslationUnit.getTranslation() or "
155 "TranslationUnit.getTranslation(cardinalNumber)");
162 nasal::CallContext ctx)
165 ctx.runtimeError(
"TranslationUnit.getTargetText([index])");
168 const auto index = ctx.getArg<std::size_t>(0);
172 "cannot call getTargetText() on a TranslationUnit that has "
176 "invalid target text index %d: TranslationUnit only has %d "
186 using TranslationUnitRef = std::shared_ptr<TranslationUnit>;
187 using NasalTranslationUnit = nasal::Ghost<TranslationUnitRef>;
189 NasalTranslationUnit::init(
"TranslationUnit")
static naRef f_getTranslation(const TranslationUnit &translUnit, nasal::CallContext ctx)
static naRef f_getTargetText(const TranslationUnit &translUnit, nasal::CallContext ctx)
Container class for a string and its translation.
static std::size_t getPluralFormIndex(const std::string &languageId, intType number)
Return the index of the plural form to use for a number of “items”
Class holding a source string and its translation in a language.
std::string getTargetText(int pluralFormIndex=0) const
Get the target text (translated string) associated to pluralFormIndex.
std::string getTranslation() const
Get the target text of a non-plural translation unit.
bool getPluralStatus() const
Return the plural status of a translation unit.
std::size_t getNumberOfTargetTexts() const
Return the number of target texts (plural forms) present in the object.
std::string getSourceText() const
Return the source text of the object, i.e. the “engineering English” form.
TranslationUnit(const std::string sourceText={}, const std::vector< std::string > targetTexts={}, bool hasPlural=false)
void setPluralStatus(int hasPlural)
Set the plural status of a translation unit.
void setTargetTexts(std::vector< std::string > texts)
Set all target texts at once.
void setSourceText(std::string text)
Set the source text of the object, i.e. the “engineering English” form.
static void setupGhost()
Set up a Nasal type that wraps TranslationUnit.
void setTargetText(int pluralFormIndex, std::string text)
Set the target text for the specified plural form.
flightgear::LanguageInfo::intType intType
Class providing information on plural forms.
FlightGear Localization Support.