Detect OS language?

BlitzMax Forums/BlitzMax Programming/Detect OS language?

SLotman(Posted 2010) [#1]
I know this works on windows to get the LID (Language ID) of the OS:

Extern "win32"
  Function GetUserDefaultLangID%()
End Extern

Local lang% = GetUserDefaultLangID()


Question is: how can this be achieved on Linux and Mac? (I can test it on Linux, but not on Mac unfortunately)

Have anyone tryed this yet? Searching the forum I only see one mention to wxmax - but I would like to avoid any unecessary overhead for this if possible...


SLotman(Posted 2010) [#2]
This should work on Linux:

        Global lang:String
	Local tempLang:String
	
	tempLang = getenv_("LANGUAGE")
	If tempLang="" Then tempLang = getenv_("LC_ALL")
	If tempLang="" Then tempLang = getenv_("LC_MESSAGES")
	If tempLang="" Then tempLang = getenv_("LANG")
	If tempLang="" Then tempLang = "en"
	
	tempLang=Lower(tempLang)
	If Instr(tempLang, "en") Then lang="en" ' english
	If Instr(tempLang, "ru") Then lang="ru" ' russian
	If Instr(tempLang, "zh") Then lang="zh" ' chinese
	If Instr(tempLang, "de") Then lang="de" ' german
	If Instr(tempLang, "fr") Then lang="fr" ' french
	If Instr(tempLang, "it") Then lang="it" ' italian
	If Instr(tempLang, "ja") Then lang="ja" ' japanese
	If Instr(tempLang, "pt") Then lang="pt" ' portuguese
	If Instr(tempLang, "sp") Then lang="sp" ' spanish      
	If Instr(tempLang, "es") Then lang="sp" ' spanish
	If Instr(tempLang, "nl") Then lang="nl" ' dutch
	
	' nothing found, defaults to english
	If lang="" Then lang="en"

        print "Detected language: " + lang


At least on Ubuntu it worked. Should detect english, russian, chinese, german, french, italian, japanese, portuguese and spanish - or if none detected, default to english.

Can anyone test it on a Mac and see if it works there too?
Would be nice if anyone with Linux - other than Ubuntu 8, in English - could test this to see if it's actually recognizing the languages correctly.

Last edited 2010

Last edited 2010

Last edited 2010


Galaxy613(Posted 2010) [#3]
It works on Snow Leopard.


ziggy(Posted 2010) [#4]
spanish sometimes is identified as "es" instead of "sp". Just in case...


SLotman(Posted 2010) [#5]
ah, maybe thats why on ubuntu 10.04 live cd, it didnt detected spanish! have to recompile to es and try it out...

btw: it did work for english and portuguese!
update2: now it works also for spanish (thanks ziggy!) and successfully tested french on Ubuntu 10.04.01
@Galaxy613: thanks for testing on Mac! Glad to know it works :)

Last edited 2010


degac(Posted 2010) [#6]
Thanks!
The only thing is that Windows report a numeric ID (ie: 1039) while Linux report in plain text...
A 'mod' with this 'extra-OS' command (like default user, documents/system folder etc) will be interesting.

ps: I will use a Map to determine the language, it *should* be faster and better manageable I think.


orgos(Posted 2010) [#7]
Good, so when I get the id language on Window how I can know what language it is?

Last edited 2010


SLotman(Posted 2010) [#8]
Google for GetUserDefaultLangID. On the Microsoft page about it, there's a link to all possible values.

I just don't post a direct link, because it's MS site... links always change, and a month from now, it will be a 404 :P~


DrDeath(Posted 2010) [#9]
It works on Snow Leopard.

Nope. On Mac OS X, the system language is stored differently and checking the LANGUAGE environment variable would generally turn up empty.

I'm not sure if there is an easier way to get the current system language, but it appears that you might have to implement some Cocoa calls.


orgos(Posted 2010) [#10]
thanks slotman

I look for it. if any one make the proper code to detect the os language on mac os and can put it I will be very grateful.

thanks.


SLotman(Posted 2010) [#11]
if any one make the proper code to detect the os language on mac os and can put it I will be very grateful.

Yeah, me too. Altough I don't have a Mac to test it, would be nice to have the code 'ready' for this :/