Game Development Community

Bug (and fix) in LangTable::setDefaultLanguage and LangTable::setCurrentLanguage

by Giuseppe Bertone · in Torque Game Builder · 02/25/2010 (1:25 pm) · 0 replies

Hi guys,
I'm working with many translation files and I use the LangTable object to accomplish language switches. I noticed some bugs inside setDefaultLanguage and setCurrentLanguage functions: essentially they are really error prone. You can breaks the toy setting the same language twice or even playing with the current and default language (i.e. setting a default language can deactivate the current language and vice versa)

I rewritten these two functions entirely, adding some comments and console messages so it should be easier to understand what's going on.

void LangTable::setDefaultLanguage(S32 langID)
{
	//langID must be a valid value
	if(langID < 0 || langID > mLangTable.size())
	{
		Con::errorf("Language must be between 0 and %i.", mLangTable.size());
		return;
	}

	//Check if it's a real change
	if(langID == mDefaultLang) return;

	//Try to avoid useless activation/deactivation process
	if(mCurrentLang >= 0 && langID == mCurrentLang)
	{
		//Language is already activated and it's equal to current language
		mDefaultLang = mCurrentLang;
		Con::printf("Language %s [%s] is now the default language.", mLangTable[langID]->getLangName(), mLangTable[langID]->getLangFile());
		//Nothing to activate or deactivate
		return;
	}

	//Deactivate default language only if it is not the current language
	if(mDefaultLang >= 0 && mDefaultLang != mCurrentLang)
	{
		mLangTable[mDefaultLang]->deactivateLanguage();
		Con::printf("Language %s [%s] deactivated.", mLangTable[mDefaultLang]->getLangName(), mLangTable[mDefaultLang]->getLangFile());
	}

	//Activate requested language and set it as default one
	if(mLangTable[langID]->activateLanguage())
	{
		mDefaultLang = langID;
		Con::printf("Language %s [%s] is now the default language.", mLangTable[langID]->getLangName(), mLangTable[langID]->getLangFile());
	}
	else
		Con::errorf("Error while setting default language to %s.", langID);
}

void LangTable::setCurrentLanguage(S32 langID)
{
	//langID must be a valid value
	if(langID < 0 || langID > mLangTable.size())
	{
		Con::errorf("Language must be between 0 and %i.", mLangTable.size());
		return;
	}

	//Check if it's a real change
	if(langID == mCurrentLang) return;

	//Deactivate current language only if it's not the default language
	if(mCurrentLang >= 0 && mCurrentLang != mDefaultLang)
	{
		mLangTable[mCurrentLang]->deactivateLanguage();
		Con::printf("Language %s [%s] deactivated.", mLangTable[mCurrentLang]->getLangName(), mLangTable[mCurrentLang]->getLangFile());
	}

	//Try to avoid useless activation process
	if(langID == mDefaultLang)
	{
		//Language is already activated and it's equal to default language
		mCurrentLang = mDefaultLang;
		//Nothing to activate
		return;
	}

	//Activate the requested language
	if(mLangTable[langID]->activateLanguage())
	{
		mCurrentLang = langID;
		Con::printf("Language %s [%s] activated.", mLangTable[langID]->getLangName(), mLangTable[langID]->getLangFile());
	}
	else
		Con::errorf("Error while setting current language to %s.", langID);
}

Perhaps it was not when TGB was designed, but currently multilanguage support is a property people consider obvious to find in a game (or even in a game engine ^_^). I think it will be great to have with T2D an easier and more powerful I18N framework.

Happy internationalization to all.