// (Triforce) The Insert menu's languages (vk_menu_i18n.h). // // menu_i18n.h holds one table a language: each English text of vk_menu.cpp and its translation. A text is looked up whole; // a text the menu made with snprintf is matched against the table's texts that hold printf conversions ("%d of %d") and // put together from the translated format, its %s parts looked up in turn. A text the table does not have stays English. // The table's pointers live for the process; a text made from a format is kept in a cache of two generations, the older let // go when the newer passes 4096 texts or the language changes, so no pointer handed out during a frame is freed in it. // The game's own language (the Game language row, language.txt) is vk_menu.cpp's (lang_load, lang_store, game_language.h): // this file only follows the one the game runs in, for Auto, and names the languages by their own names. #include "vk_menu_i18n.h" #include #include #include #include #include #include #include #include #include #include #include "game_language.h" #include "menu_i18n.h" #include "vk_menu.h" namespace mkwvk::i18n { namespace { /// A printf conversion at f (after its '%'): its length, and its kind ('s' text, 'n' a number, 'c' a character, '%'), or 0 size_t conversion(const char* f, char& kind) { size_t i = 0; while (f[i] && strchr("-+#0", f[i])) i++; // (not ' ': "80% suits" is a percent sign, not a "% s") while (f[i] >= '0' && f[i] <= '9') i++; if (f[i] == '.') { i++; while (f[i] >= '0' && f[i] <= '9') i++; } while (f[i] == 'l' || f[i] == 'h' || f[i] == 'z') i++; switch (f[i]) { case 's': kind = 's'; return i + 1; case 'd': case 'u': case 'f': case 'x': case 'g': kind = 'n'; return i + 1; case 'c': kind = 'c'; return i + 1; case '%': kind = '%'; return i + 1; default: return 0; } } /// Whether text t is what format f prints; its conversions' texts into args bool match_format(const char* f, const char* t, std::vector& args) { while (*f) { if (*f != '%') { if (*f != *t) return false; f++, t++; continue; } char kind = 0; const size_t n = conversion(f + 1, kind); if (!n) { // (a lone '%': a plain character) if (*t != '%') return false; f++, t++; continue; } f += 1 + n; if (kind == '%') { if (*t != '%') return false; t++; continue; } if (kind == 'c') { if (!*t) return false; args.emplace_back(t, 1); if (match_format(f, t + 1, args)) return true; args.pop_back(); return false; } // (a text or a number: each length from the shortest, the rest of the format matched after it) for (size_t len = 1; t[len - 1]; len++) { if (kind == 'n' && !strchr("0123456789.-+x", t[len - 1])) break; args.emplace_back(t, len); if (match_format(f, t + len, args)) return true; args.pop_back(); } return false; } return !*t; } std::string fill_format(const char* fmt, const std::vector& args) { std::string out; size_t a = 0; for (const char* f = fmt; *f;) { char kind = 0; const size_t n = *f == '%' ? conversion(f + 1, kind) : 0; if (!n) { out += *f++; continue; } f += 1 + n; if (kind == '%') out += '%'; else if (a < args.size()) out += kind == 's' ? tr_text(args[a++]) : args[a++]; } return out; } struct Table { std::unordered_map exact; // (keys: the table's own texts, which live for the process) std::vector formats; // (the entries with printf conversions) }; /// A language's table, made at its first use. A lookup after that takes no lock and makes no string (call_once's check only) const Table& table(int lang) { static std::unique_ptr t[menu_i18n::kLangCount]; static std::once_flag made[menu_i18n::kLangCount]; std::call_once(made[lang], [lang] { t[lang] = std::make_unique
(); const menu_i18n::Lang& l = menu_i18n::kLangs[lang]; for (int i = 0; i < l.count; i++) { const menu_i18n::Entry& e = l.entries[i]; t[lang]->exact.emplace(e.en, e.text); for (const char* p = strchr(e.en, '%'); p; p = strchr(p + 1, '%')) { char kind = 0; if (conversion(p + 1, kind) && kind != '%') { t[lang]->formats.push_back(&e); break; } } } }); return *t[lang]; } bool same_code(const char* a, const char* b) { // (as host/nn_applet.c compares: pt-br is pt-BR) for (; *a && *b; a++, b++) if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return false; return !*a && !*b; } /// The names the language rows showed with letters past Latin (a lead byte from U+0280 on: Cyrillic, Hangul, kana, Han), for /// names_shown_since: each seen once (by the tables' pointers, which live for the process), handed out once std::mutex shown_mu; std::vector shown_seen; std::vector> shown_new; void shown(const char* code, const char* name) { bool latin = true; for (const char* p = name; *p; p++) latin &= (unsigned char)*p < 0xCA; if (latin) return; std::lock_guard lock(shown_mu); if (std::find(shown_seen.begin(), shown_seen.end(), name) != shown_seen.end()) return; shown_seen.push_back(name), shown_new.emplace_back(code, name); } /// Auto's language: the menu's table of the language the game runs in, its code, else its first part (fr-CA: fr, en-GB: en), /// else English. Set once, at the menu's start (set_game_started) std::atomic auto_lang{0}; } // namespace void set_game_started(int game_language) { if (game_language < 0 || game_language >= game_language::kCount) game_language = game_language::kDefault; const std::string code = game_language::kLanguages[game_language].code; const std::string first = code.substr(0, code.find('-')); int found = 0; for (int i = 0; i < menu_i18n::kLangCount && !found; i++) if (same_code(code.c_str(), menu_i18n::kLangs[i].code)) found = i; for (int i = 0; i < menu_i18n::kLangCount && !found; i++) if (same_code(first.c_str(), menu_i18n::kLangs[i].code)) found = i; auto_lang.store(found, std::memory_order_relaxed); } const char* game_lang_name(int game_language) { const int i = std::clamp(game_language, 0, game_language::kCount - 1); shown(game_language::kLanguages[i].code, game_language::kOwnNames[i]); return game_language::kOwnNames[i]; } std::vector> names_shown_since() { std::lock_guard lock(shown_mu); return std::exchange(shown_new, {}); } std::vector texts_of(const char* code) { std::vector out; for (const menu_i18n::Lang& l : menu_i18n::kLangs) if (same_code(code, l.code)) for (int i = 0; i < l.count; i++) out.push_back(l.entries[i].text); return out; } int lang_now() { if (opts.menu_lang > 0) return std::clamp(opts.menu_lang - 1, 0, menu_i18n::kLangCount - 1); return auto_lang.load(std::memory_order_relaxed); } const char* lang_code() { return menu_i18n::kLangs[lang_now()].code; } int plural(int n) { const std::string code = lang_code(); const int ten = n % 10, hundred = n % 100; const bool few = ten >= 2 && ten <= 4 && !(hundred >= 12 && hundred <= 14); if (code == "ru") return ten == 1 && hundred != 11 ? 0 : few ? 1 : 2; // (1, 21: параметр; 2-4: параметра; 5, 11: параметров) if (code == "pl") return n == 1 ? 0 : few ? 1 : 2; // (1: ustawienie; 2-4, 22: ustawienia; 5, 12: ustawień) return n == 1 ? 0 : 1; } int lang_count() { return menu_i18n::kLangCount; } const char* lang_name(int index) { const menu_i18n::Lang& l = menu_i18n::kLangs[std::clamp(index, 0, menu_i18n::kLangCount - 1)]; shown(l.code, l.name); return l.name; } std::string tr_text(const std::string& s) { const int lang = lang_now(); if (s.empty() || !lang) return s; const Table& t = table(lang); if (auto it = t.exact.find(s); it != t.exact.end()) return it->second; for (const menu_i18n::Entry* e : t.formats) { std::vector args; if (match_format(e->en, s.c_str(), args)) return fill_format(e->text, args); } return s; } const char* tr(const char* s) { const int lang = lang_now(); if (!s || !*s || !lang) return s; const Table& t = table(lang); if (auto it = t.exact.find(s); it != t.exact.end()) return it->second; if (t.formats.empty()) return s; // (a text made from a format: put together once, kept. Two generations: when the newer one passes 4096 texts it becomes // the older and the older is let go, so a pointer handed out lives through 4096 more texts at least, a frame's many times) static std::unordered_map gen[2]; static int newer = 0, made_for = -1; static std::mutex mu; std::lock_guard lk(mu); if (made_for != lang) gen[0].clear(), gen[1].clear(), made_for = lang; auto it = gen[newer].find(s); if (it == gen[newer].end()) { auto old = gen[newer ^ 1].find(s); std::string text = old != gen[newer ^ 1].end() ? old->second : tr_text(s); if (gen[newer].size() >= 4096) newer ^= 1, gen[newer].clear(); it = gen[newer].emplace(s, std::move(text)).first; } return it->second == s ? s : it->second.c_str(); } } // namespace mkwvk::i18n