Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120// tests/test_i18n.cpp — the string-catalog layer (sc2k/i18n.hpp): parsing, lookup fallback,// interpolation, CLDR plural categories, and locale number/money formatting.//// Catalogs are written to a scratch dir under the build tree so the test is self-contained —// it exercises the real file loader, not a mocked map.#include <cstdio>#include <filesystem>#include <fstream>#include <string>#include "sc2k/i18n.hpp"#include "test_harness.hpp"
namespace fs = std::filesystem;using namespace sc2k;
static void write_file(const fs::path& p, const std::string& text) { std::ofstream f(p, std::ios::trunc); f << text;}
int main() { const fs::path dir = fs::temp_directory_path() / "sc2k_i18n_test"; fs::create_directories(dir);
write_file(dir / "en.txt", "# English reference catalog\n" "_meta.name = English\n" "_meta.group = ,\n" "_meta.money = ${n}\n" "_meta.decimal = .\n" "_meta.percent = {n}%\n" "_meta.units = imperial\n" "menu.file.load = &Load City\\tCtrl+L\n" "greet = Hello, {who}!\n" "trees.one = {n} tree\n" "trees.other = {n} trees\n" "only.english = fallback text\n"); write_file(dir / "uk.txt", "_meta.name = \xD0\xA3\xD0\xBA\xD1\x80\xD0\xB0\xD1\x97\xD0\xBD\xD1\x81\xD1\x8C\xD0\xBA\xD0\xB0\n" "_meta.group = \xC2\xA0\n" // NBSP separator survives value trimming "_meta.money = {n} $\n" "_meta.decimal = ,\n" "_meta.percent = {n}%\n" "_meta.units = metric\n" "trees.one = {n} \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2\xD0\xBE\n" "trees.few = {n} \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2\xD0\xB0\n" "trees.many = {n} \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2\n");
// ── No catalog loaded: every key falls through to itself (raw-token passthrough) ───────── CHECK(i18n::tr("BULLDOZE") == "BULLDOZE");
// ── English: parse, escapes, interpolation, plurals, formatting ────────────────────────── CHECK(i18n::load_locale(dir.string(), "en")); CHECK(i18n::active_locale() == "en"); CHECK(i18n::tr("menu.file.load") == "&Load City\tCtrl+L"); // \t escape unescaped CHECK(i18n::tr("no.such.key") == "no.such.key"); // miss → visible raw key CHECK(i18n::trf("greet", {{"who", "Mayor"}}) == "Hello, Mayor!"); CHECK(i18n::trn("trees", 1) == "1 tree"); CHECK(i18n::trn("trees", 42) == "42 trees"); CHECK(i18n::group_number(1234567) == "1,234,567"); CHECK(i18n::group_number(-1234) == "-1,234"); CHECK(i18n::group_number(999) == "999"); CHECK(i18n::format_money(2500000) == "$2,500,000"); CHECK(i18n::format_money(-2500) == "-$2,500"); // sign leads the whole expression CHECK(i18n::format_percentage_basis_points(650) == "6.5%"); CHECK(i18n::format_percentage_basis_points(625) == "6.25%"); CHECK(i18n::format_percentage_basis_points(600) == "6%"); CHECK(!i18n::metric()); CHECK(i18n::f_to_c(50) == 10); CHECK(i18n::f_to_c(32) == 0); CHECK(i18n::mph_to_kmh(10) == 16);
// ── Plural categories ───────────────────────────────────────────────────────────────────── CHECK(std::string(i18n::plural_category("en", 1)) == "one"); CHECK(std::string(i18n::plural_category("en", 0)) == "other"); CHECK(std::string(i18n::plural_category("fr", 0)) == "one"); // Ukrainian: 1→one, 21→one, 11→many, 2→few, 22→few, 12→many, 5→many, 100→many CHECK(std::string(i18n::plural_category("uk", 1)) == "one"); CHECK(std::string(i18n::plural_category("uk", 21)) == "one"); CHECK(std::string(i18n::plural_category("uk", 11)) == "many"); CHECK(std::string(i18n::plural_category("uk", 2)) == "few"); CHECK(std::string(i18n::plural_category("uk", 22)) == "few"); CHECK(std::string(i18n::plural_category("uk", 12)) == "many"); CHECK(std::string(i18n::plural_category("uk", 5)) == "many"); CHECK(std::string(i18n::plural_category("uk", 100)) == "many");
// ── Ukrainian active: own strings, English fallback, NBSP grouping, uk plurals ──────────── CHECK(i18n::load_locale(dir.string(), "uk")); CHECK(i18n::active_locale() == "uk"); CHECK(i18n::tr("_meta.name") == "\xD0\xA3\xD0\xBA\xD1\x80\xD0\xB0\xD1\x97\xD0\xBD\xD1\x81\xD1\x8C\xD0\xBA\xD0\xB0"); CHECK(i18n::tr("only.english") == "fallback text"); // active-miss → English CHECK(i18n::tr("no.such.key") == "no.such.key"); // both-miss → raw key CHECK(i18n::trn("trees", 1) == "1 \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2\xD0\xBE"); CHECK(i18n::trn("trees", 3) == "3 \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2\xD0\xB0"); CHECK(i18n::trn("trees", 15) == "15 \xD0\xB4\xD0\xB5\xD1\x80\xD0\xB5\xD0\xB2"); CHECK(i18n::group_number(1234567) == "1\xC2\xA0" "234\xC2\xA0" "567"); CHECK(i18n::format_money(1000) == "1\xC2\xA0" "000 $"); CHECK(i18n::format_percentage_basis_points(650) == "6,5%"); CHECK(i18n::metric()); // A key family absent from both catalogs degrades to the raw key, never crashes CHECK(i18n::trn("trees.en_only", 2) == "trees.en_only");
// ── Failed load keeps the previous locale (bad LANGUAGE= stays usable) ──────────────────── CHECK(!i18n::load_locale(dir.string(), "xx")); CHECK(i18n::active_locale() == "uk");
// ── available_locales lists both files with display names ───────────────────────────────── const auto locs = i18n::available_locales(dir.string()); CHECK(locs.size() == 2); CHECK(locs[0].first == "en" && locs[0].second == "English"); CHECK(locs[1].first == "uk");
fs::remove_all(dir); std::printf("test_i18n: %s (%d failures)\n", failures ? "FAIL" : "PASS", failures); return failures ? 1 : 0;}