HashTable 應用樣本
static const char *
maybe_translate_message (const char *msg)
{
char *s;
const char *ret;
static GHashTable *hash = NULL;
if (hash == NULL) {
hash = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (hash, "login:", _("Username:"));
g_hash_table_insert (hash, "Username:", _("Username:"));
g_hash_table_insert (hash, "username:", _("Username:"));
g_hash_table_insert (hash, "Password:", _("Password:"));
g_hash_table_insert (hash, "password:", _("Password:"));
g_hash_table_insert (hash, "No password supplied", _("No password supplied"));
g_hash_table_insert (hash, "Password unchanged", _("Password unchanged"));
g_hash_table_insert (hash, "Can not get username", _("Can not get username"));
g_hash_table_insert (hash, "Retype new UNIX password:", _("Retype new UNIX password:"));
g_hash_table_insert (hash, "Enter new UNIX password:", _("Enter new UNIX password:"));
g_hash_table_insert (hash, "(current) UNIX password:", _("(current) UNIX password:"));
g_hash_table_insert (hash, "Error while changing NIS password.", _("Error while changing NIS password."));
g_hash_table_insert (hash, "You must choose a longer password", _("You must choose a longer password"));
g_hash_table_insert (hash, "Sorry, passwords do not match", _("Sorry, passwords do not match"));
}
s = g_strstrip (g_strdup (msg));
ret = g_hash_table_lookup (hash, s);
g_free (s);
if (ret != NULL) {
return ret;
} else {
return msg;
}
}