9#include "absl/strings/str_cat.h"
18template <
typename Table>
19const auto* Find(
const Table& table, uint32_t
id) {
20 const auto found = table.find(
id);
21 return found != table.end() ? &found->second :
nullptr;
24std::string
Translated(
const protocol::Message& message,
30std::optional<std::string> NameIn(
const protocol::Arg& arg,
32 switch (arg.value_case()) {
33 case protocol::Arg::kItem:
34 if (
const auto* item = Find(content.items.by_id, arg.item())) {
35 return arg.count() == 0 ?
NameOf(*item) :
NameOf(*item, arg.count());
38 case protocol::Arg::kMonster:
39 if (
const auto* kind = Find(content.monsters.kinds, arg.monster())) {
40 return arg.count() == 0 ?
NameOf(*kind) :
NameOf(*kind, arg.count());
43 case protocol::Arg::kNpc:
44 if (
const auto* kind = Find(content.npcs.kinds, arg.npc())) {
48 case protocol::Arg::kQuest:
49 if (
const auto* quest = Find(content.quests, arg.quest())) {
53 case protocol::Arg::kSkill:
54 if (
const auto* skill = Find(content.skills.by_id, arg.skill())) {
58 case protocol::Arg::kFaction:
59 if (
const auto* faction = Find(content.factions, arg.faction())) {
70std::string
Shown(
const protocol::Arg& arg,
const Content* content) {
71 switch (arg.value_case()) {
72 case protocol::Arg::kNumber:
73 return absl::StrCat(arg.number());
74 case protocol::Arg::kName:
76 case protocol::Arg::kPhrase:
78 case protocol::Arg::kSlot:
80 case protocol::Arg::VALUE_NOT_SET:
85 std::optional<std::string> name;
86 if (content !=
nullptr) {
87 name = NameIn(arg, *content);
89 return name ? *std::move(name) :
Tr(
"something");
92std::string
Translated(
const protocol::Message& message,
94 std::vector<std::string> args;
95 args.reserve(message.args_size());
96 for (
const protocol::Arg& arg : message.args()) {
97 args.push_back(
Shown(arg, content));
99 const char* text = message.plural().empty()
100 ?
Tr(message.text().c_str())
101 :
TrN(message.text().c_str(), message.plural().c_str(),
103 return Fill(text, args);
std::string Translated(const protocol::Message &message, const Content &content)
const char * Tr(const char *text)
std::string Fill(std::string_view text, std::span< const std::string > args)
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 * NameOf(const items::Item &item)
std::string Shown(const char *label)
The part of an ImGui label that's shown: what's before any "##".