nanoMMO 0.97.8Test coverage / Every file / client/translations.cc

Created: 2026-11-26 01:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/client/translations.cc
Line
Count
Source
1
#include "client/translations.h"
2
3
#include <raylib.h>
4
5
#include <algorithm>
6
#include <array>
7
#include <bitset>
8
#include <cctype>
9
#include <cstddef>
10
#include <cstdint>
11
#include <functional>
12
#include <map>
13
#include <optional>
14
#include <string>
15
#include <string_view>
16
#include <utility>
17
#include <vector>
18
19
#include "absl/status/status.h"
20
#include "absl/status/statusor.h"
21
#include "absl/strings/numbers.h"
22
#include "absl/strings/str_cat.h"
23
#include "content/text.h"
24
25
namespace nanommo {
26
27
namespace {
28
29
// Works out a Plural-Forms expression for a count, reading it as it goes, from
30
// the loosest of C's operators to the tightest. One it can't read, or that
31
// divides by nothing, isn't ok.
32
class Expression {
33
 public:
34
10.0k
  Expression(std::string_view text, uint64_t n) : text_(text), n_(n) {}
35
36
10.0k
  std::optional<uint64_t> Evaluate() {
37
10.0k
    const uint64_t value = Ternary();
38
10.0k
    Skip();
39
10.0k
    if (!ok_ || 
at_ != text_.size()10.0k
) {
40
3
      return std::nullopt;
41
3
    }
42
10.0k
    return value;
43
10.0k
  }
44
45
 private:
46
  // Expressions inside expressions, in brackets or after a ?, deeper than any
47
  // language's rule goes, and nowhere near deep enough to run out of stack.
48
  static constexpr int kDeepest = 32;
49
50
  // condition ? then : otherwise, which goes to the right.
51
73.1k
  uint64_t Ternary() {
52
73.1k
    if (depth_ == kDeepest) {
53
0
      ok_ = false;
54
0
      return 0;
55
0
    }
56
73.1k
    ++depth_;
57
73.1k
    uint64_t value = Or();
58
73.1k
    if (Take("?")) {
59
18.0k
      const uint64_t then = Ternary();
60
18.0k
      ok_ = ok_ && Take(":");
61
18.0k
      const uint64_t otherwise = Ternary();
62
18.0k
      value = value != 0 ? 
then1.74k
:
otherwise16.2k
;
63
18.0k
    }
64
73.1k
    --depth_;
65
73.1k
    return value;
66
73.1k
  }
67
68
73.1k
  uint64_t Or() {
69
73.1k
    uint64_t value = And();
70
82.1k
    while (Take("||")) {
71
9.01k
      const uint64_t right = And();
72
9.01k
      value = value != 0 || 
right != 09.00k
?
11.72k
:
07.28k
;
73
9.01k
    }
74
73.1k
    return value;
75
73.1k
  }
76
77
82.1k
  uint64_t And() {
78
82.1k
    uint64_t value = Equality();
79
91.1k
    while (Take("&&")) {
80
9.01k
      const uint64_t right = Equality();
81
9.01k
      value = value != 0 && 
right != 08.92k
?
11.71k
:
07.29k
;
82
9.01k
    }
83
82.1k
    return value;
84
82.1k
  }
85
86
91.1k
  uint64_t Equality() {
87
91.1k
    uint64_t value = Comparison();
88
109k
    while (true) {
89
109k
      if (Take("==")) {
90
18.0k
        value = value == Comparison() ? 
123
:
018.0k
;
91
91.1k
      } else if (Take("!=")) {
92
3
        value = value != Comparison() ? 
12
:
01
;
93
91.1k
      } else {
94
91.1k
        return value;
95
91.1k
      }
96
109k
    }
97
91.1k
  }
98
99
109k
  uint64_t Comparison() {
100
109k
    uint64_t value = Remainder();
101
127k
    while (true) {
102
      // The two-letter ones first, or "<=" would read as "<".
103
127k
      if (Take("<=")) {
104
0
        value = value <= Remainder() ? 1 : 0;
105
127k
      } else if (Take(">=")) {
106
0
        value = value >= Remainder() ? 1 : 0;
107
127k
      } else if (Take("<")) {
108
9.01k
        value = value < Remainder() ? 
11.81k
:
07.20k
;
109
118k
      } else if (Take(">")) {
110
9.01k
        value = value > Remainder() ? 
18.92k
:
093
;
111
109k
      } else {
112
109k
        return value;
113
109k
      }
114
127k
    }
115
109k
  }
116
117
127k
  uint64_t Remainder() {
118
127k
    uint64_t value = Primary();
119
145k
    while (Take("%")) {
120
18.0k
      const uint64_t by = Primary();
121
18.0k
      if (by == 0) {
122
1
        ok_ = false;
123
1
        return 0;
124
1
      }
125
18.0k
      value %= by;
126
18.0k
    }
127
127k
    return value;
128
127k
  }
129
130
  // n, a number, or an expression in brackets.
131
145k
  uint64_t Primary() {
132
145k
    if (Take("(")) {
133
27.0k
      const uint64_t value = Ternary();
134
27.0k
      ok_ = ok_ && Take(")");
135
27.0k
      return value;
136
27.0k
    }
137
118k
    if (Take("n")) {
138
36.0k
      return n_;
139
36.0k
    }
140
82.1k
    const std::size_t start = at_;
141
209k
    while (at_ < text_.size() &&
142
209k
           
std::isdigit(static_cast<unsigned char>(text_[at_])) != 0208k
) {
143
127k
      ++at_;
144
127k
    }
145
82.1k
    uint64_t number = 0;
146
82.1k
    if (!absl::SimpleAtoi(text_.substr(start, at_ - start), &number)) {
147
1
      ok_ = false;
148
1
    }
149
82.1k
    return number;
150
118k
  }
151
152
1.41M
  void Skip() {
153
1.55M
    while (at_ < text_.size() &&
154
1.55M
           
std::isspace(static_cast<unsigned char>(text_[at_])) != 01.44M
) {
155
144k
      ++at_;
156
144k
    }
157
1.41M
  }
158
159
1.40M
  bool Take(std::string_view token) {
160
1.40M
    Skip();
161
1.40M
    if (!text_.substr(at_).starts_with(token)) {
162
1.20M
      return false;
163
1.20M
    }
164
198k
    at_ += token.size();
165
198k
    return true;
166
1.40M
  }
167
168
  std::string_view text_;
169
  uint64_t n_;
170
  std::size_t at_ = 0;
171
  int depth_ = 0;
172
  bool ok_ = true;
173
};
174
175
// What a PO file says of one string: its context, its English, and its
176
// English plural, with a translation for each form, or one.
177
struct Entry {
178
  std::string context;
179
  std::string id;
180
  std::optional<std::string> plural;
181
  std::vector<std::string> strings;
182
  bool fuzzy = false;
183
};
184
185
// A quoted string, as PO files and C write them, with its escapes.
186
6.22k
absl::StatusOr<std::string> Unquote(std::string_view quoted) {
187
6.22k
  if (quoted.size() < 2 || quoted.front() != '"' || 
quoted.back() != '"'6.22k
) {
188
1
    return absl::InvalidArgumentError("expected a string in quotes");
189
1
  }
190
6.22k
  std::string text;
191
120k
  for (std::size_t i = 1; i + 1 < quoted.size(); 
++i114k
) {
192
114k
    const char c = quoted[i];
193
114k
    if (c == '"') {
194
0
      return absl::InvalidArgumentError("a quote that isn't escaped");
195
0
    }
196
114k
    if (c != '\\') {
197
114k
      text += c;
198
114k
      continue;
199
114k
    }
200
64
    if (i + 2 >= quoted.size()) {
201
0
      return absl::InvalidArgumentError("a string that ends in a backslash");
202
0
    }
203
64
    switch (quoted[++i]) {
204
47
      case 'n':
205
47
        text += '\n';
206
47
        break;
207
0
      case 't':
208
0
        text += '\t';
209
0
        break;
210
0
      case 'r':
211
0
        text += '\r';
212
0
        break;
213
16
      case '"':
214
16
        text += '"';
215
16
        break;
216
0
      case '\\':
217
0
        text += '\\';
218
0
        break;
219
1
      default:
220
1
        return absl::InvalidArgumentError(
221
1
            absl::StrCat("an escape there isn't: \\", quoted.substr(i, 1)));
222
64
    }
223
64
  }
224
6.21k
  return text;
225
6.22k
}
226
227
18.4k
std::string_view Trimmed(std::string_view line) {
228
24.6k
  while (!line.empty() &&
229
24.6k
         
std::isspace(static_cast<unsigned char>(line.front())) != 021.9k
) {
230
6.18k
    line.remove_prefix(1);
231
6.18k
  }
232
18.4k
  while (!line.empty() &&
233
18.4k
         
std::isspace(static_cast<unsigned char>(line.back())) != 015.7k
) {
234
0
    line.remove_suffix(1);
235
0
  }
236
18.4k
  return line;
237
18.4k
}
238
239
// Each entry in the file, the header first, as it has them: a keyword and its
240
// string on a line, more of the string on the lines after it, in quotes, and
241
// comments, of which only "#, fuzzy" matters here. Obsolete entries, "#~",
242
// are comments too.
243
14
absl::StatusOr<std::vector<Entry>> ReadEntries(std::string_view po) {
244
14
  std::vector<Entry> entries;
245
14
  Entry entry;
246
  // Whether the entry has its msgctxt or its msgid yet, and its msgid.
247
14
  bool started = false;
248
14
  bool has_id = false;
249
  // A "#, fuzzy" for the entry to come.
250
14
  bool fuzzy = false;
251
  // Where the next line in quotes goes on.
252
14
  std::string* last = nullptr;
253
2.67k
  const auto finish = [&] {
254
2.67k
    if (started) {
255
2.66k
      entries.push_back(std::move(entry));
256
2.66k
    }
257
2.67k
    entry = Entry();
258
2.67k
    started = false;
259
2.67k
    has_id = false;
260
2.67k
    last = nullptr;
261
2.67k
  };
262
2.66k
  const auto start = [&] {
263
2.66k
    entry.fuzzy = fuzzy;
264
2.66k
    fuzzy = false;
265
2.66k
    started = true;
266
2.66k
  };
267
14
  int number = 0;
268
12.2k
  for (std::size_t at = 0; at <= po.size();) {
269
12.2k
    std::size_t end = po.find('\n', at);
270
12.2k
    if (end == std::string_view::npos) {
271
11
      end = po.size();
272
11
    }
273
12.2k
    const std::string_view line = Trimmed(po.substr(at, end - at));
274
12.2k
    at = end + 1;
275
12.2k
    ++number;
276
12.2k
    const auto error = [number](std::string_view why) {
277
3
      return absl::InvalidArgumentError(
278
3
          absl::StrCat("line ", number, ": ", why));
279
3
    };
280
12.2k
    if (line.empty()) {
281
2.67k
      continue;
282
2.67k
    }
283
    // A comment after an entry's strings is the next one's.
284
9.58k
    const bool complete = !entry.strings.empty();
285
9.58k
    if (line.front() == '#') {
286
3.35k
      if (complete) {
287
2.63k
        finish();
288
2.63k
      } else 
if (725
started725
) {
289
0
        return error("a comment in the middle of an entry");
290
0
      }
291
3.35k
      fuzzy = fuzzy || (line.starts_with("#,") &&
292
3.35k
                        
line.find("fuzzy") != std::string_view::npos3
);
293
3.35k
      continue;
294
3.35k
    }
295
6.22k
    if (line.front() == '"') {
296
47
      if (last == nullptr) {
297
0
        return error("a string that follows no keyword");
298
0
      }
299
47
      absl::StatusOr<std::string> more = Unquote(line);
300
47
      if (!more.ok()) {
301
0
        return error(more.status().message());
302
0
      }
303
47
      *last += *more;
304
47
      continue;
305
47
    }
306
6.17k
    const std::size_t space = line.find_first_of(" \t");
307
6.17k
    if (space == std::string_view::npos) {
308
0
      return error(absl::StrCat("a keyword without a string: ", line));
309
0
    }
310
6.17k
    const std::string_view keyword = line.substr(0, space);
311
6.17k
    absl::StatusOr<std::string> text = Unquote(Trimmed(line.substr(space)));
312
6.17k
    if (!text.ok()) {
313
2
      return error(text.status().message());
314
2
    }
315
    // A new entry starts at its context, or at its English.
316
6.17k
    if ((keyword == "msgctxt" || 
keyword == "msgid"5.69k
) &&
complete3.15k
) {
317
26
      finish();
318
26
    }
319
6.17k
    if (keyword == "msgctxt") {
320
482
      if (started) {
321
0
        return error("a msgctxt in the middle of an entry");
322
0
      }
323
482
      start();
324
482
      entry.context = *std::move(text);
325
482
      last = &entry.context;
326
5.69k
    } else if (keyword == "msgid") {
327
2.66k
      if (has_id) {
328
0
        return error("a second msgid in an entry");
329
0
      }
330
2.66k
      if (!started) {
331
2.18k
        start();
332
2.18k
      }
333
2.66k
      has_id = true;
334
2.66k
      entry.id = *std::move(text);
335
2.66k
      last = &entry.id;
336
3.02k
    } else if (keyword == "msgid_plural") {
337
135
      if (!has_id || entry.plural || complete) {
338
0
        return error("a msgid_plural that isn't after a msgid");
339
0
      }
340
135
      entry.plural = *std::move(text);
341
135
      last = &*entry.plural;
342
2.88k
    } else if (keyword == "msgstr") {
343
2.53k
      if (!has_id || 
entry.plural2.53k
||
complete2.53k
) {
344
1
        return error("a msgstr that isn't after a msgid");
345
1
      }
346
2.53k
      entry.strings.push_back(*std::move(text));
347
2.53k
      last = &entry.strings.back();
348
2.53k
    } else 
if (353
keyword.starts_with("msgstr[")353
&&
keyword.ends_with("]")353
) {
349
353
      const std::string form = absl::StrCat(entry.strings.size());
350
353
      if (!entry.plural || keyword.substr(7, keyword.size() - 8) != form) {
351
0
        return error(absl::StrCat("expected msgstr[", form, "]"));
352
0
      }
353
353
      entry.strings.push_back(*std::move(text));
354
353
      last = &entry.strings.back();
355
353
    } else {
356
0
      return error(absl::StrCat("a keyword there isn't: ", keyword));
357
0
    }
358
6.17k
  }
359
11
  if (started && 
entry.strings.empty()8
) {
360
1
    return absl::InvalidArgumentError("the last entry has no msgstr");
361
1
  }
362
10
  finish();
363
10
  return entries;
364
11
}
365
366
// The placeholders a string has, $0 to $9, as absl::Substitute reads them,
367
// for whom $$ is a dollar.
368
4.98k
std::bitset<10> Placeholders(std::string_view text) {
369
4.98k
  std::bitset<10> found;
370
84.7k
  for (std::size_t i = 0; i + 1 < text.size(); 
++i79.7k
) {
371
79.7k
    if (text[i] != '$') {
372
78.2k
      continue;
373
78.2k
    }
374
1.50k
    const char next = text[++i];
375
1.50k
    if (next >= '0' && next <= '9') {
376
1.50k
      found.set(static_cast<std::size_t>(next - '0'));
377
1.50k
    }
378
1.50k
  }
379
4.98k
  return found;
380
4.98k
}
381
382
2.65k
std::string Key(std::string_view context, std::string_view text) {
383
2.65k
  return context.empty() ? 
std::string(text)2.16k
384
2.65k
                         : 
absl::StrCat(context, "\x04", text)491
;
385
2.65k
}
386
387
// A header's field, such as "Plural-Forms", or nothing.
388
std::optional<std::string_view> HeaderField(std::string_view header,
389
8
                                            std::string_view name) {
390
28
  for (std::size_t start = 0; start < header.size();) {
391
28
    std::size_t end = header.find('\n', start);
392
28
    if (end == std::string_view::npos) {
393
0
      end = header.size();
394
0
    }
395
28
    const std::string_view line = header.substr(start, end - start);
396
28
    start = end + 1;
397
28
    if (line.starts_with(name) && 
line.substr(name.size()).starts_with(":")8
) {
398
8
      return Trimmed(line.substr(name.size() + 1));
399
8
    }
400
28
  }
401
0
  return std::nullopt;
402
8
}
403
404
}  // namespace
405
406
14
absl::StatusOr<PluralRule> PluralRule::Parse(std::string_view forms) {
407
14
  const std::size_t count = forms.find("nplurals=");
408
14
  const std::size_t plural = forms.find("plural=", count + 1);
409
14
  if (count == std::string_view::npos || plural == std::string_view::npos) {
410
0
    return absl::InvalidArgumentError(
411
0
        absl::StrCat("Plural-Forms without nplurals= and plural=: ", forms));
412
0
  }
413
14
  PluralRule rule;
414
14
  const std::string_view number =
415
14
      Trimmed(forms.substr(count + 9, forms.find(';', count) - count - 9));
416
14
  if (!absl::SimpleAtoi(number, &rule.forms_) || rule.forms_ < 1) {
417
0
    return absl::InvalidArgumentError(
418
0
        absl::StrCat("nplurals isn't a number of forms: ", number));
419
0
  }
420
14
  std::string_view expression = forms.substr(plural + 7);
421
14
  expression = Trimmed(expression.substr(0, expression.find(';')));
422
14
  rule.plural_ = std::string(expression);
423
  // Any count goes round to one of these, as the rules take n % 100 or
424
  // n % 10 and no more.
425
10.0k
  for (uint64_t n = 0; n < 1000; 
++n10.0k
) {
426
10.0k
    const std::optional<uint64_t> form = Expression(expression, n).Evaluate();
427
10.0k
    if (!form) {
428
3
      return absl::InvalidArgumentError(
429
3
          absl::StrCat("a plural rule that can't be read: ", expression));
430
3
    }
431
10.0k
    if (*form >= static_cast<uint64_t>(rule.forms_)) {
432
1
      return absl::InvalidArgumentError(
433
1
          absl::StrCat("the plural rule picks form ", *form, " for ", n,
434
1
                       ", of ", rule.forms_));
435
1
    }
436
10.0k
  }
437
10
  return rule;
438
14
}
439
440
16
int PluralRule::Form(uint64_t n) const {
441
16
  const std::optional<uint64_t> form = Expression(plural_, n).Evaluate();
442
16
  return form && *form < static_cast<uint64_t>(forms_) ? static_cast<int>(*form)
443
16
                                                       : 
00
;
444
16
}
445
446
12
absl::StatusOr<Catalog> Catalog::Parse(std::string_view po) {
447
12
  absl::StatusOr<std::vector<Entry>> entries = ReadEntries(po);
448
12
  if (!entries.ok()) {
449
4
    return entries.status();
450
4
  }
451
8
  Catalog catalog;
452
1.61k
  for (const Entry& entry : *entries) {
453
    // The header, which is the translation of nothing.
454
1.61k
    if (entry.id.empty() && 
entry.context.empty()8
) {
455
8
      if (const std::optional<std::string_view> forms =
456
8
              HeaderField(entry.strings.front(), "Plural-Forms")) {
457
8
        absl::StatusOr<PluralRule> rule = PluralRule::Parse(*forms);
458
8
        if (!rule.ok()) {
459
0
          return rule.status();
460
0
        }
461
8
        catalog.plurals_ = *std::move(rule);
462
8
      }
463
8
      continue;
464
8
    }
465
1.60k
    const std::bitset<10> english =
466
1.60k
        Placeholders(entry.id) | Placeholders(entry.plural.value_or(""));
467
1.60k
    const bool translated =
468
1.60k
        !entry.fuzzy &&
469
1.60k
        
std::ranges::none_of(entry.strings, &std::string::empty)1.60k
&&
470
1.76k
        
std::ranges::all_of(entry.strings, [&](const std::string& text) 1.60k
{
471
1.76k
          return (Placeholders(text) & ~english).none();
472
1.76k
        });
473
1.60k
    if (translated) {
474
1.59k
      catalog.translations_[Key(entry.context, entry.id)] = entry.strings;
475
1.59k
    }
476
1.60k
  }
477
8
  return catalog;
478
8
}
479
480
2
absl::StatusOr<Catalog> Catalog::PseudoFrom(std::string_view pot) {
481
2
  absl::StatusOr<std::vector<Entry>> entries = ReadEntries(pot);
482
2
  if (!entries.ok()) {
483
0
    return entries.status();
484
0
  }
485
2
  Catalog catalog;
486
1.05k
  for (const Entry& entry : *entries) {
487
1.05k
    if (entry.id.empty() && 
entry.context.empty()2
) {
488
2
      continue;
489
2
    }
490
1.05k
    std::vector<std::string>& made =
491
1.05k
        catalog.translations_[Key(entry.context, entry.id)];
492
1.05k
    made.push_back(Pseudo(entry.id));
493
1.05k
    if (entry.plural) {
494
52
      made.push_back(Pseudo(*entry.plural));
495
52
    }
496
1.05k
  }
497
2
  return catalog;
498
2
}
499
500
const char* Catalog::Find(std::string_view context,
501
28
                          std::string_view text) const {
502
28
  const auto found = context.empty() ? 
translations_.find(text)23
503
28
                                     : 
translations_.find(Key(context, text))5
;
504
28
  return found != translations_.end() && 
found->second.size() == 120
505
28
             ? 
found->second.front().c_str()20
506
28
             : 
nullptr8
;
507
28
}
508
509
const char* Catalog::Find(std::string_view context, std::string_view one,
510
8
                          uint64_t n) const {
511
8
  const auto found = context.empty() ? 
translations_.find(one)4
512
8
                                     : 
translations_.find(Key(context, one))4
;
513
8
  if (found == translations_.end() ||
514
8
      static_cast<int>(found->second.size()) != plurals_.forms()) {
515
0
    return nullptr;
516
0
  }
517
8
  return found->second[static_cast<std::size_t>(plurals_.Form(n))].c_str();
518
8
}
519
520
1.10k
std::string Pseudo(std::string_view english) {
521
  // Romanian's own marks where it has them, so that the fonts show they've
522
  // got them, and another of Latin's elsewhere.
523
1.10k
  static constexpr std::array<std::pair<char, std::string_view>, 20> kMarked = {
524
1.10k
      {{'a', "ă"}, {'c', "ç"}, {'e', "é"}, {'i', "î"}, {'n', "ñ"},
525
1.10k
       {'o', "ö"}, {'s', "ș"}, {'t', "ț"}, {'u', "ü"}, {'y', "ý"},
526
1.10k
       {'A', "Ă"}, {'C', "Ç"}, {'E', "É"}, {'I', "Î"}, {'N', "Ñ"},
527
1.10k
       {'O', "Ö"}, {'S', "Ș"}, {'T', "Ț"}, {'U', "Ü"}, {'Y', "Ý"}}};
528
1.10k
  std::string made = "[";
529
1.10k
  std::size_t letters = 0;
530
25.2k
  for (std::size_t i = 0; i < english.size(); 
++i24.1k
) {
531
24.1k
    const char c = english[i];
532
    // A placeholder stays as it is, for absl::Substitute.
533
24.1k
    if (c == '$' && 
i + 1 < english.size()453
) {
534
453
      made += english.substr(i, 2);
535
453
      ++i;
536
453
      continue;
537
453
    }
538
23.6k
    if (std::isalpha(static_cast<unsigned char>(c)) != 0) {
539
18.6k
      ++letters;
540
18.6k
    }
541
23.6k
    const auto marked = std::ranges::find(
542
23.6k
        kMarked, c, &std::pair<char, std::string_view>::first);
543
23.6k
    if (marked != kMarked.end()) {
544
12.2k
      made += marked->second;
545
12.2k
    } else {
546
11.4k
      made += c;
547
11.4k
    }
548
23.6k
  }
549
  // A third longer, as many languages are longer than English.
550
1.10k
  absl::StrAppend(&made, " ", std::string((letters + 2) / 3, '~'), "]");
551
1.10k
  return made;
552
1.10k
}
553
554
namespace {
555
556
// The language in use, and the labels TrLabel has made for it, which ImGui
557
// only reads, so each is made once.
558
struct InUse {
559
  std::optional<Catalog> catalog;
560
  std::map<std::string, std::string, std::less<>> labels;
561
};
562
563
75
InUse& Current() {
564
75
  static InUse& in_use = *new InUse;
565
75
  return in_use;
566
75
}
567
568
}  // namespace
569
570
0
std::string LanguageFolder() {
571
0
  return std::string(GetApplicationDirectory()) + "po";
572
0
}
573
574
15
absl::Status UseLanguage(std::string_view code, const std::string& folder) {
575
15
  Current() = InUse();
576
15
  if (code.empty()) {
577
9
    return absl::OkStatus();
578
9
  }
579
6
  const bool pseudo = code == kPseudo;
580
6
  const std::string file = pseudo ? 
"nanommo.pot"1
:
absl::StrCat(code, ".po")5
;
581
6
  const absl::StatusOr<std::string> text =
582
6
      ReadFile(absl::StrCat(folder, "/", file));
583
6
  if (!text.ok()) {
584
1
    return text.status();
585
1
  }
586
5
  absl::StatusOr<Catalog> catalog =
587
5
      pseudo ? 
Catalog::PseudoFrom(*text)1
:
Catalog::Parse(*text)4
;
588
5
  if (!catalog.ok()) {
589
0
    return absl::InvalidArgumentError(
590
0
        absl::StrCat(code, ": ", catalog.status().message()));
591
0
  }
592
5
  Current().catalog = *std::move(catalog);
593
5
  return absl::OkStatus();
594
5
}
595
596
34
const char* Tr(const char* text) { return Tr("", text); }
597
598
43
const char* Tr(std::string_view context, const char* text) {
599
43
  const std::optional<Catalog>& catalog = Current().catalog;
600
43
  const char* found = catalog ? 
catalog->Find(context, text)18
:
nullptr25
;
601
43
  return found != nullptr ? 
found15
:
text28
;
602
43
}
603
604
3
const char* TrN(const char* one, const char* many, uint64_t n) {
605
3
  return TrN("", one, many, n);
606
3
}
607
608
const char* TrN(std::string_view context, const char* one, const char* many,
609
9
                uint64_t n) {
610
9
  const std::optional<Catalog>& catalog = Current().catalog;
611
9
  const char* found = catalog ? 
catalog->Find(context, one, n)5
:
nullptr4
;
612
9
  if (found != nullptr) {
613
5
    return found;
614
5
  }
615
4
  return n == 1 ? 
one2
:
many2
;
616
9
}
617
618
2
const char* TrLabel(const char* label) {
619
2
  const std::string_view all = label;
620
2
  const std::string_view shown = all.substr(0, all.find("##"));
621
2
  const std::optional<Catalog>& catalog = Current().catalog;
622
2
  const char* translated =
623
2
      catalog && 
!shown.empty()1
?
catalog->Find("", shown)1
:
nullptr1
;
624
2
  if (translated == nullptr) {
625
1
    return label;
626
1
  }
627
1
  auto& labels = Current().labels;
628
1
  auto made = labels.find(all);
629
1
  if (made == labels.end()) {
630
1
    made = labels.emplace(all, absl::StrCat(translated, "###", all)).first;
631
1
  }
632
1
  return made->second.c_str();
633
2
}
634
635
}  // namespace nanommo