sourcemod icon indicating copy to clipboard operation
sourcemod copied to clipboard

fix geoip chi and zho not return chinese translation

Open Lxeon opened this issue 6 months ago • 6 comments

GeoLite2-City.mmdb supports zh-CN, but does not support chi and zho in sourcemod

The Chinese translation cannot be returned. 图片

This is a fix when GetLanguageInfo got chi or zho, convert to zh-CN to match GeoLite2-City.mmdb

Lxeon avatar Jun 06 '25 16:06 Lxeon

Good catch! Can we be sure that the mmdb always contains the zh-CN locale? It might be safer to check that and fallback properly if not.

peace-maker avatar Jun 12 '25 13:06 peace-maker

i already check the GeoLite2-City.mmdb in sourcemod, date 2019/12/17. 图片

and the first comment pictrue is this version.(download from sourcemod latest release)

I also checked maxmind webside, although it is a csv version, I think it apply to mmdb too. 图片

or maybe for code robustness, change the code like this:

if (translator->GetLanguageInfo(langid, &code, NULL))
{
	for (size_t i = 0; i < mmdb.metadata.languages.count; i++)
	{
		if (strcmp(code, mmdb.metadata.languages.names[i]) == 0)
		{
			return code;
		}
		// Not sure whether mmdb will include chi or zho in the future, so multiple judgments
		if ((strcmp(code, "chi") == 0 || strcmp(code, "zho") == 0) && strcmp(mmdb.metadata.languages.names[i], "zh-CN"))
		{
			return "zh-CN";
		}
	}
}

Lxeon avatar Jun 12 '25 14:06 Lxeon

If it is confirmed that the simplified Chinese language code of mmdb is "zh-CN", would it be better to modify the variable code directly?

Even if mmdb does not contain the "zh-CN" language, we can return "en" as a fallback.

if (translator->GetLanguageInfo(langid, &code, NULL))
{
	if (strcmp(code, "chi") == 0)
	{
		code = "zh-CN";
	}
	else if (strcmp(code, "zho") == 0)
	{
		code = "zh-TW";
	}

	for (size_t i = 0; i < mmdb.metadata.languages.count; i++)
	{
		if (strcmp(code, mmdb.metadata.languages.names[i]) == 0)
		{
			return code;
		}
	}
}

F1F88 avatar Jun 13 '25 13:06 F1F88

If it is confirmed that the simplified Chinese language code of mmdb is "zh-CN", would it be better to modify the variable code directly?

Even if mmdb does not contain the "zh-CN" language, we can return "en" as a fallback.

if (translator->GetLanguageInfo(langid, &code, NULL))
{
	if (strcmp(code, "chi") == 0)
	{
		code = "zh-CN";
	}
	else if (strcmp(code, "zho") == 0)
	{
		code = "zh-TW";
	}

	for (size_t i = 0; i < mmdb.metadata.languages.count; i++)
	{
		if (strcmp(code, mmdb.metadata.languages.names[i]) == 0)
		{
			return code;
		}
	}
}

sourcemod default mmdb version didnt contain zh-TW, so it will cause error.

i not sure if mmdb is customizable.

Lxeon avatar Jun 13 '25 15:06 Lxeon

sourcemod default mmdb version didnt contain zh-TW, so it will cause error.

i not sure if mmdb is customizable.

This does not cause an error, but returns the default language "en".

See line 196:

https://github.com/alliedmodders/sourcemod/blob/20642a8518904ccfa0d36905471a313fcca2db7a/extensions/geoip/geoip_util.cpp#L186-L197

I checked GeoLite2-City_20250610 and it doesn't include zh-TW either.

~~When I asked chatgpt what languages ​​geoip returns, gpt's answer included zh-TW. Sorry, I should have verified this answer myself first.~~

F1F88 avatar Jun 13 '25 16:06 F1F88

sourcemod default mmdb version didnt contain zh-TW, so it will cause error. i not sure if mmdb is customizable.

This does not cause an error, but returns the default language "en".

See line 196:

https://github.com/alliedmodders/sourcemod/blob/20642a8518904ccfa0d36905471a313fcca2db7a/extensions/geoip/geoip_util.cpp#L186-L197

I checked GeoLite2-City_20250610 and it doesn't include zh-TW either.

~When I asked chatgpt what languages ​​geoip returns, gpt's answer included zh-TW. Sorry, I should have verified this answer myself first.~

ye i miss the final return. and i think mmdb must include zh-cn.

Lxeon avatar Jun 14 '25 17:06 Lxeon

I think normalizing the language code before looking it up in the available languages in the mmdb is the way to go as @F1F88 suggested is the way to go. Falling back to English is what we do everywhere else. Can you please adjust your code accordingly?

peace-maker avatar Aug 15 '25 18:08 peace-maker

I think normalizing the language code before looking it up in the available languages in the mmdb is the way to go as @F1F88 suggested is the way to go. Falling back to English is what we do everywhere else. Can you please adjust your code accordingly?

code optimized. please check

Lxeon avatar Aug 16 '25 09:08 Lxeon