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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262// Late City Wire localisation: typed parameters remain locale-neutral in the store and resolve only// when a front end needs display text.#include <array>#include <cstdio>#include <filesystem>#include <fstream>#include <set>#include <string>#include "sc2k/ui/notification_format.hpp"#include "test_harness.hpp"
namespace fs = std::filesystem;using namespace sc2k;
static void write_file(const fs::path& path, const std::string& text) { std::ofstream file(path, std::ios::trunc); file << text;}
static ui::Notification make_notification() { ui::Notification notification; notification.titleKey = "wire.title"; notification.descriptionKey = "wire.description"; notification.parameters = { {"city", ui::NotificationLiteral{"Port Rouge"}}, {"plant", ui::NotificationMessageKey{"plant.coal"}}, {"cost", std::int64_t{2500000}, ui::NotificationParameterFormat::Money}, {"population", std::int64_t{1234567}, ui::NotificationParameterFormat::Grouped}, {"rate", std::int64_t{650}, ui::NotificationParameterFormat::PercentBasisPoints}, }; notification.actions[0].labelKey = "wire.action"; notification.actionCount = 1; return notification;}
static std::string variant_key(std::string_view base, std::uint8_t index) { return index == 0 ? std::string(base) : std::string(base) + ".alt" + std::to_string(index);}
static void check_long_finance_copy_resolves(const ui::Notification& notification) { const ui::FormattedNotification formatted = ui::format_notification(notification); CHECK(!formatted.title.empty()); CHECK(!formatted.description.empty()); CHECK(formatted.title.find('{') == std::string::npos); CHECK(formatted.description.find('{') == std::string::npos); CHECK(formatted.description.find('}') == std::string::npos); for (std::size_t index = 0; index < notification.actionCount; ++index) { const std::string action = ui::format_notification_action(notification.actions[index]); CHECK(!action.empty()); CHECK(action.find('{') == std::string::npos); }}
static void shipped_catalogues_format_parameters_and_cover_every_headline_variant() { ui::Notification plant; plant.titleKey = "citywire.plant.aging.title"; plant.descriptionKey = "citywire.plant.aging.description"; plant.parameters = { {"plant", ui::NotificationMessageKey{"plant.microwave"}}, {"years", std::int64_t{49}}, };
CHECK(i18n::load_locale("assets/lang", "en")); CHECK(ui::format_notification(plant).title == "Microwave power station nearing retirement"); CHECK(ui::format_notification(plant).description == "Microwave generation at this site has operated for 49 years and is nearing the end of its service life. Replace or rebuild the power station before it closes."); CHECK(ui::format_notification(plant).description.find('{') == std::string::npos); ui::Notification bond; bond.titleKey = "citywire.finance.bond_issued.title"; bond.descriptionKey = "citywire.finance.bond_issued.description"; bond.parameters = { {"debt_id", std::int64_t{7}, ui::NotificationParameterFormat::Grouped}, {"rate", std::int64_t{575}, ui::NotificationParameterFormat::PercentBasisPoints}, {"principal", std::int64_t{30000}, ui::NotificationParameterFormat::Money}, {"outstanding", std::int64_t{2}}, {"funds", std::int64_t{50000}, ui::NotificationParameterFormat::Money}, }; CHECK(ui::format_notification(bond).title == "Bond 7 issued at 5.75%"); CHECK(ui::format_notification(bond).description.find("$30,000") != std::string::npos); ui::Notification support; support.titleKey = "citywire.finance.support.title"; support.descriptionKey = "citywire.finance.support.description"; support.parameters = { {"due", std::int64_t{25000}, ui::NotificationParameterFormat::Money}, {"treasury", std::int64_t{-5000}, ui::NotificationParameterFormat::Money}, {"injection", std::int64_t{40000}, ui::NotificationParameterFormat::Money}, {"reserve", std::int64_t{10000}, ui::NotificationParameterFormat::Money}, {"runway_months", std::int64_t{12}}, {"supervision_months", std::int64_t{60}}, {"eligible_month", ui::NotificationMessageKey{"date.mon.0"}}, {"eligible_year", std::int64_t{2015}}, }; support.actions[0].labelKey = "citywire.action.accept_support"; support.actions[1].labelKey = "citywire.action.reject_support"; support.actionCount = 2; const ui::FormattedNotification englishSupport = ui::format_notification(support); CHECK(englishSupport.title == "Treasury default: a decision is required"); CHECK(englishSupport.description.find("$25,000") != std::string::npos); CHECK(englishSupport.description.find("Jan 2015") != std::string::npos); CHECK(englishSupport.description.find('{') == std::string::npos); CHECK(ui::format_notification_action(support.actions[0]) == "Accept government assistance"); CHECK(ui::format_notification_action(support.actions[1]) == "Reject and resign"); ui::Notification longSupport = support; longSupport.parameters = { {"due", std::int64_t{2000000000}, ui::NotificationParameterFormat::Money}, {"treasury", std::int64_t{-2147483648}, ui::NotificationParameterFormat::Money}, {"injection", std::int64_t{4147493833LL}, ui::NotificationParameterFormat::Money}, {"reserve", std::int64_t{2000000000}, ui::NotificationParameterFormat::Money}, {"runway_months", std::int64_t{12}}, {"supervision_months", std::int64_t{60}}, {"eligible_month", ui::NotificationMessageKey{"date.mon.11"}}, {"eligible_year", std::int64_t{9999}}, }; check_long_finance_copy_resolves(longSupport); static constexpr std::array<const char*, 7> kPlantKeys = { "plant.gas", "plant.oil", "plant.nuclear", "plant.solar", "plant.microwave", "plant.fusion", "plant.coal"}; for (const char* plantKey : kPlantKeys) { plant.parameters[0].value = ui::NotificationMessageKey{plantKey}; const ui::FormattedNotification formatted = ui::format_notification(plant); CHECK(formatted.title.find(" power station nearing retirement") != std::string::npos); CHECK(formatted.description.find(" generation at this site") != std::string::npos); } plant.parameters[0].value = ui::NotificationMessageKey{"plant.microwave"}; for (const ui::NotificationTextVariants& family : ui::kNotificationHeadlineVariantFamilies) for (std::uint8_t index = 0; index < family.count; ++index) { const std::string key = variant_key(family.baseKey, index); CHECK(i18n::tr(key) != key); } for (const ui::NotificationTextVariants& family : ui::kNotificationDescriptionVariantFamilies) for (std::uint8_t index = 0; index < family.count; ++index) { const std::string key = variant_key(family.baseKey, index); CHECK(i18n::tr(key) != key); }
CHECK(i18n::load_locale("assets/lang", "uk")); CHECK(ui::format_notification(plant).title == "Електростанція «Мікрохвильова» наближається до пенсії"); CHECK(ui::format_notification(plant).description.find('{') == std::string::npos); CHECK(ui::format_notification(bond).title == "Облігацію 7 випущено під 5,75%"); const ui::FormattedNotification ukrainianSupport = ui::format_notification(support); CHECK(ukrainianSupport.title == "Дефолт казни: потрібне рішення"); CHECK(ukrainianSupport.description.find("Січ 2015") != std::string::npos); CHECK(ukrainianSupport.description.find('{') == std::string::npos); CHECK(ui::format_notification_action(support.actions[0]) == "Прийняти державну допомогу"); CHECK(ui::format_notification_action(support.actions[1]) == "Відхилити й піти у відставку"); check_long_finance_copy_resolves(longSupport); for (const ui::NotificationTextVariants& family : ui::kNotificationHeadlineVariantFamilies) for (std::uint8_t index = 0; index < family.count; ++index) { const std::string key = variant_key(family.baseKey, index); CHECK(i18n::tr(key) != key); } for (const ui::NotificationTextVariants& family : ui::kNotificationDescriptionVariantFamilies) for (std::uint8_t index = 0; index < family.count; ++index) { const std::string key = variant_key(family.baseKey, index); CHECK(i18n::tr(key) != key); }
// Historical notices derive variation from persisted facts, never RNG or wall-clock state. std::set<std::string> selected; for (int index = 0; index < 12; ++index) { ui::Notification season; season.titleKey = "desk.sports.champ.head"; season.createdMonth = 400 + index; season.parameters = { {"team", ui::NotificationLiteral{"Club " + std::to_string(index)}}, {"wins", std::int64_t{20 + index}}, }; const std::string key = ui::notification_headline_key(season); CHECK(key == ui::notification_headline_key(season)); selected.insert(key); } CHECK(selected.size() >= 4);
CHECK(i18n::load_locale("assets/lang", "en")); ui::Notification sportsBody; sportsBody.titleKey = "desk.sports.win.head"; sportsBody.descriptionKey = "desk.sports.win.body"; sportsBody.createdMonth = 420; sportsBody.parameters = { {"team", ui::NotificationLiteral{"Harbour Lights"}}, {"sport", ui::NotificationMessageKey{"sport.hockey"}}, {"w", std::int64_t{24}}, {"l", std::int64_t{16}}, }; const std::string bodyKey = ui::notification_description_key(sportsBody); CHECK(bodyKey == ui::notification_description_key(sportsBody)); CHECK(bodyKey.starts_with("desk.sports.win.body")); const ui::FormattedNotification sports = ui::format_notification(sportsBody); CHECK(sports.description.find("24-16") != std::string::npos); CHECK(sports.description.find("Hockey") != std::string::npos); CHECK(sports.description.find("parade") == std::string::npos);
ui::Notification live; live.titleKey = "desk.firecover.head"; live.createdMonth = 96; live.stableKey = ui::NotificationKey{ui::NotificationKeyDomain::Condition, 3}; live.parameters = {{"value", std::int64_t{25}}}; const std::string liveKey = ui::notification_headline_key(live); live.parameters[0].value = std::int64_t{40}; CHECK(ui::notification_headline_key(live) == liveKey);}
int main() { const fs::path dir = fs::temp_directory_path() / "sc2k_notification_format_test"; fs::create_directories(dir); write_file(dir / "en.txt", "_meta.group = ,\n" "_meta.money = ${n}\n" "_meta.decimal = .\n" "_meta.percent = {n}%\n" "wire.title = {city}: {plant}\n" "wire.description = Cost {cost}; population {population}; rate {rate}\n" "wire.action = Inspect\n" "plant.coal = coal power station\n" "city.default_name = The City\n"); write_file(dir / "uk.txt", "_meta.group = \xC2\xA0\n" "_meta.money = {n} $\n" "_meta.decimal = ,\n" "_meta.percent = {n}%\n" "wire.description = {population}; {cost}; {rate}\n");
CHECK(i18n::load_locale(dir.string(), "en")); const ui::Notification notification = make_notification(); const ui::FormattedNotification english{ "Port Rouge: coal power station", "Cost $2,500,000; population 1,234,567; rate 6.5%"}; CHECK(ui::format_notification(notification) == english); CHECK(ui::format_notification_action(notification.actions[0]) == "Inspect");
// Existing records re-resolve after a language change; literal city names stay literal while // nested message keys and typed numeric values use active-locale fallback and formatting. CHECK(i18n::load_locale(dir.string(), "uk")); const ui::FormattedNotification ukrainian{ "Port Rouge: coal power station", "1\xC2\xA0" "234\xC2\xA0" "567; 2\xC2\xA0" "500\xC2\xA0" "000 $; 6,5%"}; CHECK(ui::format_notification(notification) == ukrainian);
ui::Notification unnamed = notification; unnamed.parameters[0].value = ui::NotificationMessageKey{"city.default_name"}; CHECK(ui::format_notification(unnamed).title == "The City: coal power station"); unnamed.descriptionKey.clear(); CHECK(ui::format_notification(unnamed).description.empty());
fs::remove_all(dir); shipped_catalogues_format_parameters_and_cover_every_headline_variant(); std::printf("notification format: %s (%d failures)\n", failures ? "FAIL" : "PASS", failures); return failures ? 1 : 0;}