/* * splinter -- message catalogues. * * The point of these tests: a translation is only useful if it is complete, and * a missing key must be visible instead of silently empty. So they check the * real catalogues in share/locale, including that de.msg covers every key * en.msg has. */ #include #include #include #include #include "splinter.h" #include "i18n.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) int main(void) { struct sp_buf err; size_t i, n_en; if (sp_buf_init(&err) != 0) return 1; sp_i18n_add_dir("share/locale"); /* ---- English: the fallback ---- */ CHECK(sp_i18n_load(&err, "en") == 0); CHECK(strcmp(sp_i18n_lang(), "en") == 0); CHECK(strcmp(sp_t("cmd.available"), "available:") == 0); CHECK(strcmp(sp_t("ui.ready"), "ready. Enter sends, Ctrl-D quits.") == 0); /* a key that exists nowhere is shown as itself, never as an empty line */ CHECK(strcmp(sp_t("does.not.exist"), "does.not.exist") == 0); CHECK(strcmp(sp_t(NULL), "") == 0); /* ---- German ---- */ CHECK(sp_i18n_load(&err, "de") == 0); CHECK(strcmp(sp_i18n_lang(), "de") == 0); CHECK(strcmp(sp_t("cmd.available"), "verfuegbar:") == 0); CHECK(strcmp(sp_t("ui.ready"), "bereit. Enter sendet, Ctrl-D beendet.") == 0); /* format strings keep their placeholders, so printf still works */ CHECK(strstr(sp_t("model.probe_start"), "%zu") != NULL); /* ---- completeness: every English key has a German one ---- */ n_en = (size_t)sp_i18n_fallback_count(); CHECK(n_en > 20); for (i = 0; i < n_en; i++) { const char *key = sp_i18n_fallback_key(i); const char *val; if (key == NULL) continue; val = sp_t(key); if (val == NULL || strcmp(val, key) == 0) { fprintf(stderr, "FAIL missing translation: %s\n", key); failures++; } } /* ---- an unknown language falls back to English, without failing ---- */ sp_buf_clear(&err); CHECK(sp_i18n_load(&err, "xx") == 0); CHECK(strcmp(sp_t("cmd.available"), "available:") == 0); /* ---- a catalogue is data: it may not change what printf reads ---- */ { char dir[] = "/var/tmp/splinter-i18n-XXXXXX"; char path[128]; FILE *f; CHECK(mkdtemp(dir) != NULL); snprintf(path, sizeof(path), "%s/zz.msg", dir); f = fopen(path, "w"); CHECK(f != NULL); if (f != NULL) { fputs("cmd.available=%s%s%s\n" "t.bad=hi %s\n" "t.ok=hallo %d\n" "t.n=x%n\n" "t.pos=%1$s\n", f); fclose(f); } sp_i18n_add_dir(dir); CHECK(sp_i18n_load(&err, "zz") == 0); CHECK(strcmp(sp_t("cmd.available"), "available:") == 0); CHECK(strcmp(sp_td("t.bad", "hi %d"), "hi %d") == 0); CHECK(strcmp(sp_td("t.ok", "hi %d"), "hallo %d") == 0); CHECK(strcmp(sp_td("t.n", "x"), "x") == 0); CHECK(strcmp(sp_t("t.n"), "t.n") == 0); CHECK(strcmp(sp_td("t.pos", "%s"), "%s") == 0); CHECK(remove(path) == 0); CHECK(rmdir(dir) == 0); } sp_buf_free(&err); if (failures == 0) printf("test_i18n: ok\n"); return failures == 0 ? 0 : 1; }