[Scummvm-cvs-logs] SF.net SVN: scummvm:[54479] scummvm/trunk

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Thu Nov 25 19:40:56 CET 2010


Revision: 54479
          http://scummvm.svn.sourceforge.net/scummvm/?rev=54479&view=rev
Author:   lordhoto
Date:     2010-11-25 18:40:56 +0000 (Thu, 25 Nov 2010)

Log Message:
-----------
OSYSTEM: Add API to query the system locale.

I also adapted the SDL backend to implement the API.

Modified Paths:
--------------
    scummvm/trunk/backends/platform/sdl/sdl.cpp
    scummvm/trunk/backends/platform/sdl/sdl.h
    scummvm/trunk/common/system.cpp
    scummvm/trunk/common/system.h
    scummvm/trunk/common/util.cpp
    scummvm/trunk/common/util.h

Modified: scummvm/trunk/backends/platform/sdl/sdl.cpp
===================================================================
--- scummvm/trunk/backends/platform/sdl/sdl.cpp	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/backends/platform/sdl/sdl.cpp	2010-11-25 18:40:56 UTC (rev 54479)
@@ -53,6 +53,12 @@
 
 #include <time.h>	// for getTimeAndDate()
 
+#ifdef USE_DETECTLANG
+#ifndef WIN32
+#include <locale.h>
+#endif // !WIN32
+#endif
+
 //#define SAMPLES_PER_SEC 11025
 #define SAMPLES_PER_SEC 22050
 //#define SAMPLES_PER_SEC 44100
@@ -586,6 +592,67 @@
 #endif
 }
 
+Common::Language OSystem_SDL::getSystemLanguage() const {
+#ifdef USE_DETECTLANG
+#ifdef WIN32
+	// We can not use "setlocale" (at least not for MSVC builds), since it
+	// will return locales like: "English_USA.1252", thus we need a special
+	// way to determine the locale string for Win32.
+	char langName[9];
+	char ctryName[9];
+
+	const LCID languageIdentifier = GetThreadLocale();
+
+	// GetLocalInfo is only supported starting from Windows 2000, according to this:
+	// http://msdn.microsoft.com/en-us/library/dd318101%28VS.85%29.aspx
+	// On the other hand the locale constants used, seem to exist on Windows 98 too,
+	// check this for that: http://msdn.microsoft.com/en-us/library/dd464799%28v=VS.85%29.aspx
+	//
+	// I am not exactly sure what is the truth now, it might be very well that this breaks
+	// support for systems older than Windows 2000....
+	//
+	// TODO: Check whether this (or ScummVM at all ;-) works on a system with Windows 98 for
+	// example and if it does not and we still want Windows 9x support, we should definitly
+	// think of another solution.
+	if (GetLocaleInfo(languageIdentifier, LOCALE_SISO639LANGNAME, langName, sizeof(langName)) != 0 &&
+		GetLocaleInfo(languageIdentifier, LOCALE_SISO3166CTRYNAME, ctryName, sizeof(ctryName)) != 0) {
+		Common::String localeName = langName;
+		localeName += "_";
+		localeName += ctryName;
+
+		return Common::parseLanguageFromLocale(localeName.c_str());
+	} else {
+		return Common::UNK_LANG;
+	}
+#else // WIN32
+	// Activating current locale settings
+	const char *locale = "de_DE.utf8";//setlocale(LC_ALL, "");
+
+	// Detect the language from the locale
+	if (!locale) {
+		return Common::UNK_LANG;
+	} else {
+		int length = 0;
+
+		// Strip out additional information, like
+		// ".UTF-8" or the like. We do this, since
+		// our translation languages are usually
+		// specified without any charset information.
+		for (int i = 0; locale[i]; ++i, ++length) {
+			// TODO: Check whether "@" should really be checked
+			// here.
+			if (locale[i] == '.' || locale[i] == ' ' || locale[i] == '@')
+				break;
+		}
+
+		return Common::parseLanguageFromLocale(Common::String(locale, length).c_str());
+	}
+#endif // WIN32
+#else // USE_DETECTLANG
+	return Common::UNK_LANG;
+#endif // USE_DETECTLANG
+}
+
 void OSystem_SDL::setupIcon() {
 	int x, y, w, h, ncols, nbytes, i;
 	unsigned int rgba[256];

Modified: scummvm/trunk/backends/platform/sdl/sdl.h
===================================================================
--- scummvm/trunk/backends/platform/sdl/sdl.h	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/backends/platform/sdl/sdl.h	2010-11-25 18:40:56 UTC (rev 54479)
@@ -194,6 +194,8 @@
 	// Logging
 	virtual void logMessage(LogMessageType::Type type, const char *message);
 
+	virtual Common::Language getSystemLanguage() const;
+
 	void deinit();
 
 	virtual void getTimeAndDate(TimeDate &t) const;

Modified: scummvm/trunk/common/system.cpp
===================================================================
--- scummvm/trunk/common/system.cpp	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/common/system.cpp	2010-11-25 18:40:56 UTC (rev 54479)
@@ -90,3 +90,7 @@
 	fflush(output);
 }
 
+Common::Language OSystem::getSystemLanguage() const {
+	return Common::UNK_LANG;
+}
+

Modified: scummvm/trunk/common/system.h
===================================================================
--- scummvm/trunk/common/system.h	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/common/system.h	2010-11-25 18:40:56 UTC (rev 54479)
@@ -30,6 +30,7 @@
 #include "common/noncopyable.h"
 #include "common/rect.h"
 #include "common/list.h" // For OSystem::getSupportedFormats()
+#include "common/util.h" // For Common::Language
 
 #include "graphics/pixelformat.h"
 
@@ -1050,6 +1051,25 @@
 	 */
 	virtual void logMessage(LogMessageType::Type type, const char *message);
 
+	/**
+	 * Returns the locale of the system.
+	 *
+	 * This returns the currently set up locale of the system, on which
+	 * ScummVM is run.
+	 *
+	 * In case the locale can not be prepresented by Common::Language the
+	 * backend should return Common::UNK_LANG.
+	 *
+	 * @see Common::Language
+	 * @see Common::UNK_LANG
+	 *
+	 * The default implementation returns Common::UNK_LANG.
+	 *
+	 *
+	 * @return locale of the system
+	 */
+	virtual Common::Language getSystemLanguage() const;
+
 	//@}
 };
 

Modified: scummvm/trunk/common/util.cpp
===================================================================
--- scummvm/trunk/common/util.cpp	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/common/util.cpp	2010-11-25 18:40:56 UTC (rev 54479)
@@ -107,29 +107,29 @@
 
 
 const LanguageDescription g_languages[] = {
-	{ "zh-cn", "Chinese (China)", ZH_CNA },
-	{ "zh", "Chinese (Taiwan)", ZH_TWN },
-	{ "cz", "Czech", CZ_CZE },
-	{ "nl", "Dutch", NL_NLD },
-	{ "en", "English", EN_ANY }, // Generic English (when only one game version exist)
-	{ "gb", "English (GB)", EN_GRB },
-	{ "us", "English (US)", EN_USA },
-	{ "fr", "French", FR_FRA },
-	{ "de", "German", DE_DEU },
-	{ "gr", "Greek", GR_GRE },
-	{ "he", "Hebrew", HE_ISR },
-	{ "hb", "Hebrew", HE_ISR }, // Deprecated
-	{ "hu", "Hungarian", HU_HUN },
-	{ "it", "Italian", IT_ITA },
-	{ "jp", "Japanese", JA_JPN },
-	{ "kr", "Korean", KO_KOR },
-	{ "nb", "Norwegian Bokm\xE5l", NB_NOR },
-	{ "pl", "Polish", PL_POL },
-	{ "br", "Portuguese", PT_BRA },
-	{ "ru", "Russian", RU_RUS },
-	{ "es", "Spanish", ES_ESP },
-	{ "se", "Swedish", SE_SWE },
-	{ 0, 0, UNK_LANG }
+	{ "zh-cn", "zh_CN", "Chinese (China)", ZH_CNA },
+	{    "zh", "zh_TW", "Chinese (Taiwan)", ZH_TWN },
+	{    "cz", "cs_CZ", "Czech", CZ_CZE },
+	{    "nl", "nl_NL", "Dutch", NL_NLD },
+	{    "en",    "en", "English", EN_ANY }, // Generic English (when only one game version exist)
+	{    "gb", "en_GB", "English (GB)", EN_GRB },
+	{    "us", "en_US", "English (US)", EN_USA },
+	{    "fr", "fr_FR", "French", FR_FRA },
+	{    "de", "de_DE", "German", DE_DEU },
+	{    "gr", "el_GR", "Greek", GR_GRE },
+	{    "he", "he_IL", "Hebrew", HE_ISR },
+	{    "hb", "he_IL", "Hebrew", HE_ISR }, // Deprecated
+	{    "hu", "hu_HU", "Hungarian", HU_HUN },
+	{    "it", "it_IT", "Italian", IT_ITA },
+	{    "jp", "ja_JP", "Japanese", JA_JPN },
+	{    "kr", "ko_KR", "Korean", KO_KOR },
+	{    "nb", "nb_NO", "Norwegian Bokm\xE5l", NB_NOR }, // TODO Someone should verify the unix locale
+	{    "pl", "pl_PL", "Polish", PL_POL },
+	{    "br", "pt_BR", "Portuguese", PT_BRA },
+	{    "ru", "ru_RU", "Russian", RU_RUS },
+	{    "es", "es_ES", "Spanish", ES_ESP },
+	{    "se", "sv_SE", "Swedish", SE_SWE },
+	{       0,       0, 0, UNK_LANG }
 };
 
 Language parseLanguage(const String &str) {
@@ -145,6 +145,19 @@
 	return UNK_LANG;
 }
 
+Language parseLanguageFromLocale(const char *locale) {
+	if (!locale || !*locale)
+		return UNK_LANG;
+
+	const LanguageDescription *l = g_languages;
+	for (; l->code; ++l) {
+		if (!strcmp(l->unixLocale, locale))
+			return l->id;
+	}
+
+	return UNK_LANG;
+}
+
 const char *getLanguageCode(Language id) {
 	const LanguageDescription *l = g_languages;
 	for (; l->code; ++l) {
@@ -154,6 +167,15 @@
 	return 0;
 }
 
+const char *getLanguageLocale(Language id) {
+	const LanguageDescription *l = g_languages;
+	for (; l->code; ++l) {
+		if (l->id == id)
+			return l->unixLocale;
+	}
+	return 0;
+}
+
 const char *getLanguageDescription(Language id) {
 	const LanguageDescription *l = g_languages;
 	for (; l->code; ++l) {

Modified: scummvm/trunk/common/util.h
===================================================================
--- scummvm/trunk/common/util.h	2010-11-25 16:09:45 UTC (rev 54478)
+++ scummvm/trunk/common/util.h	2010-11-25 18:40:56 UTC (rev 54479)
@@ -133,6 +133,7 @@
 
 struct LanguageDescription {
 	const char *code;
+	const char *unixLocale;
 	const char *description;
 	Common::Language id;
 };
@@ -142,7 +143,9 @@
 
 /** Convert a string containing a language name into a Language enum value. */
 extern Language parseLanguage(const String &str);
+extern Language parseLanguageFromLocale(const char *locale);
 extern const char *getLanguageCode(Language id);
+extern const char *getLanguageLocale(Language id);
 extern const char *getLanguageDescription(Language id);
 
 /**


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list