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"
34 Expression(std::string_view text, uint64_t n) : text_(text), n_(n) {}
36 std::optional<uint64_t> Evaluate() {
37 const uint64_t value = Ternary();
39 if (!ok_ || at_ != text_.size()) {
48 static constexpr int kDeepest = 32;
52 if (depth_ == kDeepest) {
57 uint64_t value = Or();
59 const uint64_t then = Ternary();
60 ok_ = ok_ && Take(
":");
61 const uint64_t otherwise = Ternary();
62 value = value != 0 ? then : otherwise;
69 uint64_t value = And();
71 const uint64_t right = And();
72 value = value != 0 || right != 0 ? 1 : 0;
78 uint64_t value = Equality();
80 const uint64_t right = Equality();
81 value = value != 0 && right != 0 ? 1 : 0;
87 uint64_t value = Comparison();
90 value = value == Comparison() ? 1 : 0;
91 }
else if (Take(
"!=")) {
92 value = value != Comparison() ? 1 : 0;
99 uint64_t Comparison() {
100 uint64_t value = Remainder();
104 value = value <= Remainder() ? 1 : 0;
105 }
else if (Take(
">=")) {
106 value = value >= Remainder() ? 1 : 0;
107 }
else if (Take(
"<")) {
108 value = value < Remainder() ? 1 : 0;
109 }
else if (Take(
">")) {
110 value = value > Remainder() ? 1 : 0;
117 uint64_t Remainder() {
118 uint64_t value = Primary();
120 const uint64_t by = Primary();
133 const uint64_t value = Ternary();
134 ok_ = ok_ && Take(
")");
140 const std::size_t start = at_;
141 while (at_ < text_.size() &&
142 std::isdigit(
static_cast<unsigned char>(text_[at_])) != 0) {
146 if (!absl::SimpleAtoi(text_.substr(start, at_ - start), &number)) {
153 while (at_ < text_.size() &&
154 std::isspace(
static_cast<unsigned char>(text_[at_])) != 0) {
159 bool Take(std::string_view token) {
161 if (!text_.substr(at_).starts_with(token)) {
168 std::string_view text_;
180 std::optional<std::string> plural;
181 std::vector<std::string> strings;
186absl::StatusOr<std::string> Unquote(std::string_view quoted) {
187 if (quoted.size() < 2 || quoted.front() !=
'"' || quoted.back() !=
'"') {
188 return absl::InvalidArgumentError(
"expected a string in quotes");
191 for (std::size_t i = 1; i + 1 < quoted.size(); ++i) {
192 const char c = quoted[i];
194 return absl::InvalidArgumentError(
"a quote that isn't escaped");
200 if (i + 2 >= quoted.size()) {
201 return absl::InvalidArgumentError(
"a string that ends in a backslash");
203 switch (quoted[++i]) {
220 return absl::InvalidArgumentError(
221 absl::StrCat(
"an escape there isn't: \\", quoted.substr(i, 1)));
227std::string_view Trimmed(std::string_view line) {
228 while (!line.empty() &&
229 std::isspace(
static_cast<unsigned char>(line.front())) != 0) {
230 line.remove_prefix(1);
232 while (!line.empty() &&
233 std::isspace(
static_cast<unsigned char>(line.back())) != 0) {
234 line.remove_suffix(1);
243absl::StatusOr<std::vector<Entry>> ReadEntries(std::string_view po) {
244 std::vector<Entry> entries;
247 bool started =
false;
252 std::string* last =
nullptr;
253 const auto finish = [&] {
255 entries.push_back(std::move(entry));
262 const auto start = [&] {
268 for (std::size_t at = 0; at <= po.size();) {
269 std::size_t end = po.find(
'\n', at);
270 if (end == std::string_view::npos) {
273 const std::string_view line = Trimmed(po.substr(at, end - at));
276 const auto error = [number](std::string_view why) {
277 return absl::InvalidArgumentError(
278 absl::StrCat(
"line ", number,
": ", why));
284 const bool complete = !entry.strings.empty();
285 if (line.front() ==
'#') {
288 }
else if (started) {
289 return error(
"a comment in the middle of an entry");
291 fuzzy = fuzzy || (line.starts_with(
"#,") &&
292 line.find(
"fuzzy") != std::string_view::npos);
295 if (line.front() ==
'"') {
296 if (last ==
nullptr) {
297 return error(
"a string that follows no keyword");
299 absl::StatusOr<std::string> more = Unquote(line);
301 return error(more.status().message());
306 const std::size_t space = line.find_first_of(
" \t");
307 if (space == std::string_view::npos) {
308 return error(absl::StrCat(
"a keyword without a string: ", line));
310 const std::string_view keyword = line.substr(0, space);
311 absl::StatusOr<std::string> text = Unquote(Trimmed(line.substr(space)));
313 return error(text.status().message());
316 if ((keyword ==
"msgctxt" || keyword ==
"msgid") && complete) {
319 if (keyword ==
"msgctxt") {
321 return error(
"a msgctxt in the middle of an entry");
324 entry.context = *std::move(text);
325 last = &entry.context;
326 }
else if (keyword ==
"msgid") {
328 return error(
"a second msgid in an entry");
334 entry.id = *std::move(text);
336 }
else if (keyword ==
"msgid_plural") {
337 if (!has_id || entry.plural || complete) {
338 return error(
"a msgid_plural that isn't after a msgid");
340 entry.plural = *std::move(text);
341 last = &*entry.plural;
342 }
else if (keyword ==
"msgstr") {
343 if (!has_id || entry.plural || complete) {
344 return error(
"a msgstr that isn't after a msgid");
346 entry.strings.push_back(*std::move(text));
347 last = &entry.strings.back();
348 }
else if (keyword.starts_with(
"msgstr[") && keyword.ends_with(
"]")) {
349 const std::string form = absl::StrCat(entry.strings.size());
350 if (!entry.plural || keyword.substr(7, keyword.size() - 8) != form) {
351 return error(absl::StrCat(
"expected msgstr[", form,
"]"));
353 entry.strings.push_back(*std::move(text));
354 last = &entry.strings.back();
356 return error(absl::StrCat(
"a keyword there isn't: ", keyword));
359 if (started && entry.strings.empty()) {
360 return absl::InvalidArgumentError(
"the last entry has no msgstr");
368std::bitset<10> Placeholders(std::string_view text) {
369 std::bitset<10> found;
370 for (std::size_t i = 0; i + 1 < text.size(); ++i) {
371 if (text[i] !=
'$') {
374 const char next = text[++i];
375 if (next >=
'0' && next <=
'9') {
376 found.set(
static_cast<std::size_t
>(next -
'0'));
382std::string Key(std::string_view context, std::string_view text) {
383 return context.empty() ? std::string(text)
384 : absl::StrCat(context,
"\x04", text);
388std::optional<std::string_view> HeaderField(std::string_view header,
389 std::string_view name) {
390 for (std::size_t start = 0; start < header.size();) {
391 std::size_t end = header.find(
'\n', start);
392 if (end == std::string_view::npos) {
395 const std::string_view line = header.substr(start, end - start);
397 if (line.starts_with(name) && line.substr(name.size()).starts_with(
":")) {
398 return Trimmed(line.substr(name.size() + 1));
407 const std::size_t count =
forms.find(
"nplurals=");
408 const std::size_t plural =
forms.find(
"plural=", count + 1);
409 if (count == std::string_view::npos || plural == std::string_view::npos) {
410 return absl::InvalidArgumentError(
411 absl::StrCat(
"Plural-Forms without nplurals= and plural=: ",
forms));
414 const std::string_view number =
415 Trimmed(
forms.substr(count + 9,
forms.find(
';', count) - count - 9));
416 if (!absl::SimpleAtoi(number, &rule.forms_) || rule.forms_ < 1) {
417 return absl::InvalidArgumentError(
418 absl::StrCat(
"nplurals isn't a number of forms: ", number));
420 std::string_view expression =
forms.substr(plural + 7);
421 expression = Trimmed(expression.substr(0, expression.find(
';')));
422 rule.plural_ = std::string(expression);
425 for (uint64_t n = 0; n < 1000; ++n) {
426 const std::optional<uint64_t> form = Expression(expression, n).Evaluate();
428 return absl::InvalidArgumentError(
429 absl::StrCat(
"a plural rule that can't be read: ", expression));
431 if (*form >=
static_cast<uint64_t
>(rule.forms_)) {
432 return absl::InvalidArgumentError(
433 absl::StrCat(
"the plural rule picks form ", *form,
" for ", n,
434 ", of ", rule.forms_));
441 const std::optional<uint64_t> form = Expression(plural_, n).Evaluate();
442 return form && *form < static_cast<uint64_t>(forms_) ?
static_cast<int>(*form)
447 absl::StatusOr<std::vector<Entry>> entries = ReadEntries(po);
449 return entries.status();
452 for (
const Entry& entry : *entries) {
454 if (entry.id.empty() && entry.context.empty()) {
455 if (
const std::optional<std::string_view> forms =
456 HeaderField(entry.strings.front(),
"Plural-Forms")) {
459 return rule.status();
461 catalog.plurals_ = *std::move(rule);
465 const std::bitset<10> english =
466 Placeholders(entry.id) | Placeholders(entry.plural.value_or(
""));
467 const bool translated =
469 std::ranges::none_of(entry.strings, &std::string::empty) &&
470 std::ranges::all_of(entry.strings, [&](
const std::string& text) {
471 return (Placeholders(text) & ~english).none();
474 catalog.translations_[Key(entry.context, entry.id)] = entry.strings;
481 absl::StatusOr<std::vector<Entry>> entries = ReadEntries(pot);
483 return entries.status();
486 for (
const Entry& entry : *entries) {
487 if (entry.id.empty() && entry.context.empty()) {
490 std::vector<std::string>& made =
491 catalog.translations_[Key(entry.context, entry.id)];
492 made.push_back(
Pseudo(entry.id));
494 made.push_back(
Pseudo(*entry.plural));
501 std::string_view text)
const {
502 const auto found = context.empty() ? translations_.find(text)
503 : translations_.find(Key(context, text));
504 return found != translations_.end() && found->second.size() == 1
505 ? found->second.front().c_str()
511 const auto found = context.empty() ? translations_.find(one)
512 : translations_.find(Key(context, one));
513 if (found == translations_.end() ||
514 static_cast<int>(found->second.size()) != plurals_.forms()) {
517 return found->second[
static_cast<std::size_t
>(plurals_.Form(n))].c_str();
520std::string
Pseudo(std::string_view english) {
523 static constexpr std::array<std::pair<char, std::string_view>, 20> kMarked = {
524 {{
'a',
"ă"}, {
'c',
"ç"}, {
'e',
"é"}, {
'i',
"î"}, {
'n',
"ñ"},
525 {
'o',
"ö"}, {
's',
"ș"}, {
't',
"ț"}, {
'u',
"ü"}, {
'y',
"ý"},
526 {
'A',
"Ă"}, {
'C',
"Ç"}, {
'E',
"É"}, {
'I',
"Î"}, {
'N',
"Ñ"},
527 {
'O',
"Ö"}, {
'S',
"Ș"}, {
'T',
"Ț"}, {
'U',
"Ü"}, {
'Y',
"Ý"}}};
528 std::string made =
"[";
529 std::size_t letters = 0;
530 for (std::size_t i = 0; i < english.size(); ++i) {
531 const char c = english[i];
533 if (c ==
'$' && i + 1 < english.size()) {
534 made += english.substr(i, 2);
538 if (std::isalpha(
static_cast<unsigned char>(c)) != 0) {
541 const auto marked = std::ranges::find(
542 kMarked, c, &std::pair<char, std::string_view>::first);
543 if (marked != kMarked.end()) {
544 made += marked->second;
550 absl::StrAppend(&made,
" ", std::string((letters + 2) / 3,
'~'),
"]");
559 std::optional<Catalog> catalog;
560 std::map<std::string, std::string, std::less<>> labels;
564 static InUse& in_use = *
new InUse;
571 return std::string(GetApplicationDirectory()) +
"po";
574absl::Status
UseLanguage(std::string_view code,
const std::string& folder) {
577 return absl::OkStatus();
579 const bool pseudo = code ==
kPseudo;
580 const std::string file = pseudo ?
"nanommo.pot" : absl::StrCat(code,
".po");
581 const absl::StatusOr<std::string> text =
582 ReadFile(absl::StrCat(folder,
"/", file));
584 return text.status();
586 absl::StatusOr<Catalog> catalog =
589 return absl::InvalidArgumentError(
590 absl::StrCat(code,
": ", catalog.status().message()));
592 Current().catalog = *std::move(catalog);
593 return absl::OkStatus();
596const char*
Tr(
const char* text) {
return Tr(
"", text); }
598const char*
Tr(std::string_view context,
const char* text) {
599 const std::optional<Catalog>& catalog = Current().catalog;
600 const char* found = catalog ? catalog->Find(context, text) :
nullptr;
601 return found !=
nullptr ? found : text;
604const char*
TrN(
const char* one,
const char* many, uint64_t n) {
605 return TrN(
"", one, many, n);
608const char*
TrN(std::string_view context,
const char* one,
const char* many,
610 const std::optional<Catalog>& catalog = Current().catalog;
611 const char* found = catalog ? catalog->Find(context, one, n) :
nullptr;
612 if (found !=
nullptr) {
615 return n == 1 ? one : many;
619 const std::string_view all = label;
620 const std::string_view shown = all.substr(0, all.find(
"##"));
621 const std::optional<Catalog>& catalog = Current().catalog;
622 const char* translated =
623 catalog && !shown.empty() ? catalog->Find(
"", shown) :
nullptr;
624 if (translated ==
nullptr) {
627 auto& labels = Current().labels;
628 auto made = labels.find(all);
629 if (made == labels.end()) {
630 made = labels.emplace(all, absl::StrCat(translated,
"###", all)).first;
632 return made->second.c_str();
static absl::StatusOr< Catalog > Parse(std::string_view po)
const char * Find(std::string_view context, std::string_view text) const
The translation, or null for one there isn't.
static absl::StatusOr< Catalog > PseudoFrom(std::string_view pot)
static absl::StatusOr< PluralRule > Parse(std::string_view forms)
int Form(uint64_t n) const
The form for the count, from 0.
PluralRule()=default
English's: one form for 1, and the other for any other count.
std::string Pseudo(std::string_view english)
The English made over for the pseudo-language: "[Șéțțîñgș ~~~]".
const char * Tr(const char *text)
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 * TrLabel(const char *label)
constexpr std::string_view kPseudo
The pseudo-language's code: it's made from the template, nanommo.pot.
absl::StatusOr< std::string > ReadFile(const std::string &path)
std::string LanguageFolder()
Where the PO files are, next to the client.
absl::Status UseLanguage(std::string_view code, const std::string &folder)