From 6babcafbec70cb9f8b1e0a410d6e13bb54b2a457 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Fri, 1 Aug 2014 17:11:33 +0200 Subject: [PATCH] Geoip: Add a fallback to default language (en) in case of unsuccessful lookup with user's language. --- dlls/geoip/geoip_util.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/dlls/geoip/geoip_util.cpp b/dlls/geoip/geoip_util.cpp index 4c675905..63b2d25c 100644 --- a/dlls/geoip/geoip_util.cpp +++ b/dlls/geoip/geoip_util.cpp @@ -73,7 +73,7 @@ bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result) int gai_error = 0, mmdb_error = 0; MMDB_lookup_result_s lookup = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error); - if (gai_error != 0 || MMDB_SUCCESS != mmdb_error || !lookup.found_entry) + if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !lookup.found_entry) { return false; } @@ -83,7 +83,30 @@ bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result) if (!entry_data.has_data) { - return false; + size_t i = 0; + + // Dirty fall back to default language ("en") in case provided user's language is not localized. + + // Searh "names" position. + while (strcmp(path[i++], "names")); + + // No localized entry or we use already default language. + if (!*path[i] || !strcmp(path[i], "en")) + { + return false; + } + + // Overwrite user's language. + path[i] = "en"; + + // Try again. + gai_error = mmdb_error = 0; + MMDB_aget_value(&lookup.entry, &entry_data, path); + + if (!entry_data.has_data) + { + return false; + } } *result = entry_data;