FlightGear next
TranslationResource.hxx
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#pragma once
10
11#include <cstddef>
12#include <map>
13#include <memory>
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "LanguageInfo.hxx"
19#include "TranslationUnit.hxx"
20
29
31{
32public:
34
41 explicit TranslationResource(std::string name) noexcept;
55 void addTranslationUnit(std::string name, int index, std::string sourceText,
56 bool hasPlural = false);
68 void setFirstTargetText(std::string name, int index,
69 std::string targetText);
79 void setTargetTexts(std::string name, int index,
80 std::vector<std::string> targetTexts);
81
91 std::shared_ptr<TranslationUnit>
92 translationUnit(const std::string& name, int index = 0) const;
93
110 std::string get(const std::string& basicId, int index = 0) const;
120 std::string getPlural(intType cardinalNumber, const std::string& basicId,
121 int index = 0) const;
133 std::string getWithDefault(const std::string& basicId,
134 const std::string& defaultValue,
135 int index = 0) const;
146 std::string getPluralWithDefault(intType cardinalNumber,
147 const std::string& basicId,
148 const std::string& defaultValue,
149 int index = 0) const;
158 std::vector<std::string> getAll(const std::string& name) const;
168 std::size_t getCount(const std::string& name) const;
169
173 static void setupGhost();
174
175private:
177 std::string _name;
178 // In the default translation files, this corresponds to a node name and
179 // its index.
180 using KeyType = std::pair<std::string, int>;
181 using TranslationUnitRef = std::shared_ptr<TranslationUnit>;
182 std::map<KeyType, TranslationUnitRef> _map;
183};
Information on plural forms for the supported languages.
Container class for a string and its translation.
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.
const char * name