Add test for l10n="time" gschema attribute

This is a copy of the existing test_l10n, modified to use LC_TIME
instead of LC_MESSAGES. It's not safe as each call to g_setenv() or
setlocale() could cause the test to crash; there is no safe way to
change a threaded process's environment, and a threaded process's locale
can only be safely changed using uselocale(), not with setlocale().

The calls to g_setenv() are definitely not needed on Linux. I wonder
whether removing these will break the test on other platforms?

The calls to setlocale() should be replaced by a dance of
uselocale() -> duplocale() -> newlocale() -> uselocale() on Linux. But
this is not portable and this is a cross-platform test. We would have to
make the test platform-specific to do this. macOS and at least FreeBSD
provide these functions via xlocale.h, but this isn't portable.
This commit is contained in:
Michael Catanzaro 2023-07-21 13:48:34 -05:00
parent 34e4c4af77
commit 92de7298c8
3 changed files with 57 additions and 0 deletions

View File

@ -15,3 +15,6 @@ msgstr "\"Unbenannt\""
msgctxt "keyboard label"
msgid "\"BackSpace\""
msgstr "\"Löschen\""
msgid "\"12:00 AM\""
msgstr "\"00:00\""

View File

@ -853,6 +853,56 @@ test_l10n_context (void)
g_object_unref (settings);
}
/* Test use of l10n="time" and LC_TIME. */
static void
test_l10n_time (void)
{
GSettings *settings;
gchar *str;
gchar *locale;
g_test_summary ("Test that l10n='time' attribute uses the correct category for translations");
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2575");
bindtextdomain ("test", locale_dir);
bind_textdomain_codeset ("test", "UTF-8");
locale = g_strdup (setlocale (LC_TIME, NULL));
settings = g_settings_new ("org.gtk.test.localized");
g_setenv ("LC_TIME", "C", TRUE);
setlocale (LC_TIME, "C");
str = g_settings_get_string (settings, "midnight");
g_setenv ("LC_TIME", locale, TRUE);
setlocale (LC_TIME, locale);
g_assert_cmpstr (str, ==, "12:00 AM");
g_free (str);
str = NULL;
g_setenv ("LC_TIME", "de_DE.UTF-8", TRUE);
setlocale (LC_TIME, "de_DE.UTF-8");
/* Only do the test if translation is actually working... */
if (g_str_equal (dgettext ("test", "\"12:00 AM\""), "\"00:00\""))
{
str = g_settings_get_string (settings, "midnight");
g_assert_cmpstr (str, ==, "00:00");
g_free (str);
str = NULL;
}
else
{
g_test_skip ("translation is not working");
}
g_setenv ("LC_TIME", locale, TRUE);
setlocale (LC_TIME, locale);
g_free (locale);
g_object_unref (settings);
}
enum
{
PROP_0,
@ -3109,6 +3159,7 @@ main (int argc, char *argv[])
g_test_add_func ("/gsettings/l10n", test_l10n);
g_test_add_func ("/gsettings/l10n-context", test_l10n_context);
g_test_add_func ("/gsettings/l10n-time", test_l10n_time);
g_test_add_func ("/gsettings/delay-apply", test_delay_apply);
g_test_add_func ("/gsettings/delay-revert", test_delay_revert);

View File

@ -83,6 +83,9 @@
<key name="backspace" type="s">
<default l10n="messages" context="keyboard label">"BackSpace"</default>
</key>
<key name="midnight" type="s">
<default l10n="time">"12:00 AM"</default>
</key>
</schema>
<schema id="org.gtk.test.binding" path="/tests/binding/">