Detect users language settings

Share your advanced PureBasic knowledge/code with the community.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Detect users language settings

Post by utopiomania »

I needed to detect users language settings, and use this the first time the program runs, and came up with this.

Check it out and let me know if it doesn't work for you. There's a language code list at the end of the listing.

Code: Select all

;-program notes
;-initialize
;-
enumeration
  #WIN
  #WEB
endEnumeration
#DQ = #DQUOTE$

global winW = 480, winH = 660, counter = -1
global appName.s = "directory"

declare openMainWindow()
declare navigationCallback(id, url.s)
declare.s userInterfacePage1()
declare.s saveHtml(fileName.s, html.s)
declare deleteHtml(filename.s)
declare exitProgram()

;-program entry
openMainWindow()

;-program event handler
repeat
  event = waitWindowEvent()
  select event
    case #PB_EVENT_SIZEWINDOW
       winW = windowWidth(#WIN)
       winH = windowHeight(#WIN)
       resizeGadget(#WEB, 0, 0, winW, winH)
      ;load the html
      setGadgetText(#WEB, userInterfacePage1())
    case #PB_EVENT_CLOSEWINDOW
      exitProgram()
  endSelect
forever

;-program exit
procedure exitProgram()
  deleteHtml(appName + ".html")
;   writePreferences(appName, appName)
  end
endProcedure


procedure openMainWindow()
  if openWindow(#WIN, 0, 0, winW, winH, "program template", $CF0001)
    stickyWindow(#WIN, 1)
    webGadget(#WEB, 0, 0, winW, winH, "")
    ;load the html
    setGadgetText(#WEB, userInterfacePage1())
    ;callback to monitor navigation
    setGadgetAttribute(#WEB, #PB_WEB_NAVIGATIONCALLBACK, @navigationCallback())
  endIf
endProcedure

procedure navigationCallback(id, url.s)
  url = lcase(url)
  if findString(url, "locale", 1)
    ;handle event...
    url = mid(url, len(getTemporaryDirectory() + "locale") + 1)
    messageRequester ("Userlanguage", url)
    ;block drag-drop
    procedureReturn 0
  endIf
  procedureReturn 1
endProcedure

procedure.s userInterfacePage1()
  ;html
  html.s + "<html><script language = 'vbscript'>" + #CRLF$
  html + "sub window_onLoad()" + #CRLF$
  ;navigator.userLanguage or navigator.systemLanguage
  html + "  window.location = " + #DQ + "locale" + #DQ + " & navigator.userLanguage" + #CRLF$
  html + "end sub" + #CRLF$
  html + "</script></html>"
  procedureReturn saveHtml(appName + ".html", html)
endProcedure

procedure.s saveHtml(fileName.s, html.s)
  ;saves file to temp directory
  path.s = getTemporaryDirectory()
  path + fileName
  if createFile(0, path)
    writeString(0, html, #PB_UTF8)
    closeFile(0)
  else
    procedureReturn ""
  endIf
  procedureReturn "file://" + path
endProcedure

procedure deleteHtml(fileName.s)
  ;deletes file in temp directory
  path.s = getTemporaryDirectory()
  path + fileName
  deleteFile(path)
endProcedure

; 
; Language Codes
; The following table lists all the possible language codes used to 
; specify various system settings.
; 
; af Afrikaans  
; sq Albanian  
; ar-sa Arabic (Saudi Arabia)  
; ar-iq Arabic (Iraq)  
; ar-eg Arabic (Egypt)  
; ar-ly Arabic (Libya)  
; ar-dz Arabic (Algeria)  
; ar-ma Arabic (Morocco)  
; ar-tn Arabic (Tunisia)  
; ar-om Arabic (Oman)  
; ar-ye Arabic (Yemen)  
; ar-sy Arabic (Syria)  
; ar-jo Arabic (Jordan)  
; ar-lb Arabic (Lebanon)  
; ar-kw Arabic (Kuwait)  
; ar-ae Arabic (U.A.E.)  
; ar-bh Arabic (Bahrain)  
; ar-qa Arabic (Qatar)  
; eu Basque  
; bg Bulgarian  
; be Belarusian  
; ca Catalan  
; zh-tw Chinese (Taiwan)  
; zh-cn Chinese (PRC)  
; zh-hk Chinese (Hong Kong SAR)  
; zh-sg Chinese (Singapore)  
; hr Croatian  
; cs Czech  
; da Danish  
; nl Dutch (Standard)  
; nl-be Dutch (Belgium)  
; en English  
; en-us English (United States)  
; en-gb English (United Kingdom)  
; en-au English (Australia)  
; en-ca English (Canada)  
; en-nz English (New Zealand)  
; en-ie English (Ireland)  
; 
; en-za English (South Africa)  
; en-jm English (Jamaica)  
; en English (Caribbean)  
; en-bz English (Belize)  
; en-tt English (Trinidad)  
; et Estonian  
; fo Faeroese  
; fa Farsi  
; fi Finnish  
; fr French (Standard)  
; fr-be French (Belgium)  
; fr-ca French (Canada)  
; fr-ch French (Switzerland)  
; fr-lu French (Luxembourg)  
; gd Gaelic (Scotland)  
; gd-ie Gaelic (Ireland)  
; de German (Standard)  
; de-ch German (Switzerland)  
; de-at German (Austria)  
; de-lu German (Luxembourg)  
; de-li German (Liechtenstein)  
; el Greek  
; he Hebrew  
; hi Hindi  
; hu Hungarian  
; is Icelandic  
; id Indonesian  
; it Italian (Standard)  
; it-ch Italian (Switzerland)  
; ja Japanese  
; ko Korean  
; ko Korean (Johab)  
; lv Latvian  
; lt Lithuanian  
; mk Macedonian (FYROM) 
; ms Malaysian  
; mt Maltese  
; no Norwegian (Bokmal)  
; no Norwegian (Nynorsk)  
; pl Polish  
; pt-br Portuguese (Brazil)  
; pt Portuguese (Portugal)  
; rm Rhaeto-Romanic  
; ro Romanian  
; ro-mo Romanian (Republic of Moldova)  
; ru Russian  
; ru-mo Russian (Republic of Moldova)  
; sz Sami (Lappish)  
; sr Serbian (Cyrillic)  
; sr Serbian (Latin)  
; sk Slovak  
; sl Slovenian  
; sb Sorbian  
; es Spanish (Spain)  
; es-mx Spanish (Mexico)  
; es-gt Spanish (Guatemala)  
; es-cr Spanish (Costa Rica)  
; es-pa Spanish (Panama)  
; es-do Spanish (Dominican Republic)  
; es-ve Spanish (Venezuela)  
; es-co Spanish (Colombia)  
; es-pe Spanish (Peru)  
; es-ar Spanish (Argentina)  
; es-ec Spanish (Ecuador)  
; es-cl Spanish (Chile)  
; es-uy Spanish (Uruguay)  
; es-py Spanish (Paraguay)  
; es-bo Spanish (Bolivia)  
; es-sv Spanish (El Salvador)  
; es-hn Spanish (Honduras)  
; es-ni Spanish (Nicaragua)  
; es-pr Spanish (Puerto Rico)  
; sx Sutu  
; sv Swedish  
; sv-fi Swedish (Finland)  
; th Thai  
; ts Tsonga  
; tn Tswana  
; tr Turkish  
; uk Ukrainian  
; ur Urdu  
; ve Venda  
; vi Vietnamese  
; xh Xhosa  
; ji Yiddish  
; zu Zulu  
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Detect users language settings

Post by PB »

If you just want the description, use this:

Code: Select all

buffer$=Space(999)
GetLocaleInfo_(#LOCALE_USER_DEFAULT,#LOCALE_SLANGUAGE,buffer$,Len(buffer$))
Debug buffer$
For me, the above returns "English (Australia)" instead of "en-au".
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

The description is just fine. All I want is to use the result in a lookup table and return an index into the languages my program can use, so
both description and language code will do just fine.

Only your contribution is much better than mine. Thanks! :)
Post Reply