' Gambas module file

Public AllowLog As Boolean

Private Const DEFAULT_LANG_DICT As String = ""
  ":aa=ER:af=ZA:afr=PE:ak=GH:am=ET:an=ES:anp=IN:ar=EG:as=IN:ast=ES:ayc=PE:az=AZ"
  ":be=BY:bem=ZM:ber=MA:bg=BG:bhb=IN:bho=IN:bi=VU:bd=BD:bo=CN:br=FR:brx=IN:bs=BA"
  ":byn=ER:ca=ES:chr=US:ckb=IQ:cmn=TW:crh=UA:csb=PL:cs=CZ:cv=RU:cy=GB:da=DK:de=DE"
  ":doi=IN:dsb=DE:dv=MV:dz=BT:el=GR:en=GB:eo=US:es=ES:et=EE:eu=ES:fa=IR:ff=SN:fi=FI"
  ":fil=PH:fo=FO:fr=FR:fur=IT:fy=NL:ga=IE:gd=GB:gez=ET:gl=ES:gu=IN:gv=GB:hak=TW"
  ":ha=NG:he=IL:hif=FJ:hi=IN:hne=IN:hr=HR:hsb=DE:ht=HT:hu=HU:hy=AM:ia=FR:id=ID"
  ":ig=NG:ik=CA:is=IS:it=IT:iu=CA:ja=JP:kab=DZ:ka=GE:kk=KZ:kl=GL:km=KH:kn=IN:kok=IN"
  ":ko=KR:ks=IN:ku=TR:kw=GB:ky=KG:lb=LU:lg=UG:li=NL:lij=IT:ln=CD:lo=LA:lt=LT:lv=LV"
  ":lzh=TW:mag=IN:mai=IN:mfe=MU:mg=MG:mhr=RU:mi=NZ:miq=NI:mjw=IN:mk=MK:ml=IN:mni=IN"
  ":mn=MN:mr=IN:ms=MY:mt=MT:my=MM:nan=TW:nb=NO:nds=DE:ne=NP:nhn=MX:niu=NZ:nl=NL"
  ":nn=NO:nr=ZA:nso=ZA:oc=FR:om=ET:or=IN:os=RU:pa=PK:pap=CW:pl=PL:ps=AF:pt=PT:quz=PE"
  ":raj=IN:ro=RO:ru=RU:rw=RW:sah=RU:sa=IN:sat=IN:sc=IT:sd=PK:se=NO:sgs=LT:shn=MM"
  ":shs=CA:sid=ET:si=LK:sk=SK:sl=SI:sm=WS:so=SO:sq=AL:sr=RS:ss=ZA:st=ZA:sv=SE:sw=KE"
  ":szl=PL:ta=LK:tcy=IN:te=IN:tg=TJ:the=NP:th=TH:ti=ER:tig=ER:tk=TM:tl=PH:tn=ZA"
  ":to=TO:tpi=PG:tr=TR:ts=ZA:tt=RU:ug=CN:uk=UA:unm=US:ur=PK:uz=UZ:ve=ZA:vi=VN:wa=BE"
  ":wae=CH:wal=ET:wo=SN:xh=ZA:yi=US:yo=NG:yue=HK:yuw=PG:zh=CN:zu=ZA:"


Public Sub Log(sMsg As String, Optional bForce As Boolean)
  
  Dim hFile As File
  
  If Not AllowLog And If Not bForce Then Return
  
  hFile = Open "/tmp/session.log" For Append 
  Print #hFile, sMsg
  Close #hFile
  
End

Public Sub DecodeURL(sUrl As String, aField As String[], cVal As Collection, bDoNotDecodePlus As Boolean)
  
  Dim sElt As String
  Dim aElt As String[]
  Dim sKey As String
  Dim sVal As String

  sUrl = Trim(sUrl)
  
  For Each sElt In Split(sUrl, Request.Delimiter)
  
    If Not sElt Then Continue
  
    aElt = Split(sElt, "=")
    If aElt.Count = 2 Then
      sKey = URL.Unquote(aElt[0], bDoNotDecodePlus)
      If Not sKey Then Continue
      sVal = URL.Unquote(aElt[1], bDoNotDecodePlus)
    Else 
      sKey = URL.Unquote(aElt[0], bDoNotDecodePlus)
      If Not sKey Then Continue
      sVal = ""
    Endif
    
    If Not aField.Exist(sKey) Then
      aField.Add(sKey)
      cVal[sKey] = sVal
    Else If sVal Then 
      'IF TypeOf(cVal[sKey]) = gb.String THEN 
      '  cVal[sKey] = [CStr(cVal[sKey]), sVal]
      'ELSE  
      '  cVal[sKey].Add(sVal)
      'ENDIF
      cVal[sKey] &= "\n" & sVal
    Endif
  
  Next  
  
' Catch
'   
'   Error.Raise("Malformed URL:" & Error.Where & ": " & Error.Text)
'  
End

Public Sub GetAbsoluteURL(sPath As String) As String

  Dim sReq As String
  Dim sPort As String
    
  sReq = Application.Protocol & "://" & Application.Host
  
  sPort = Application.Port
  If sPort And If sPort <> "80" Then sReq &= ":" & sPort
  
  Return sReq &/ sPath
  
End


Public Sub Main()

  'CGI["HTTP_ACCEPT_LANGUAGE"] = "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
  'Print Request.Language
  MakeLanguage

End



Private Sub MakeLanguage()

  Dim cLang As New Collection
  Dim sVal As String
  Dim sStr As String
  
  cLang["aa"] = "ER"
  cLang["af"] = "ZA"
  cLang["afr"] = "PE"
  cLang["ak"] = "GH"
  cLang["am"] = "ET"
  cLang["an"] = "ES"
  cLang["anp"] = "IN"
  cLang["ar"] = "EG"
  cLang["as"] = "IN"
  cLang["ast"] = "ES"
  cLang["ayc"] = "PE"
  cLang["az"] = "AZ"
  cLang["be"] = "BY"
  cLang["bem"] = "ZM"
  cLang["ber"] = "MA"
  cLang["bg"] = "BG"
  cLang["bhb"] = "IN"
  cLang["bho"] = "IN"
  cLang["bi"] = "VU"
  cLang["bd"] = "BD"
  cLang["bo"] = "CN"
  cLang["br"] = "FR"
  cLang["brx"] = "IN"
  cLang["bs"] = "BA"
  cLang["byn"] = "ER"
  cLang["ca"] = "ES"
  cLang["chr"] = "US"
  cLang["ckb"] = "IQ"
  cLang["cmn"] = "TW"
  cLang["crh"] = "UA"
  cLang["csb"] = "PL"
  cLang["cs"] = "CZ"
  cLang["cv"] = "RU"
  cLang["cy"] = "GB"
  cLang["da"] = "DK"
  cLang["de"] = "DE"
  cLang["doi"] = "IN"
  cLang["dsb"] = "DE"
  cLang["dv"] = "MV"
  cLang["dz"] = "BT"
  cLang["el"] = "GR"
  cLang["en"] = "GB"
  cLang["eo"] = "US"
  cLang["es"] = "ES"
  cLang["et"] = "EE"
  cLang["eu"] = "ES"
  cLang["fa"] = "IR"
  cLang["ff"] = "SN"
  cLang["fi"] = "FI"
  cLang["fil"] = "PH"
  cLang["fo"] = "FO"
  cLang["fr"] = "FR"
  cLang["fur"] = "IT"
  cLang["fy"] = "NL"
  cLang["ga"] = "IE"
  cLang["gd"] = "GB"
  cLang["gez"] = "ET"
  cLang["gl"] = "ES"
  cLang["gu"] = "IN"
  cLang["gv"] = "GB"
  cLang["hak"] = "TW"
  cLang["ha"] = "NG"
  cLang["he"] = "IL"
  cLang["hif"] = "FJ"
  cLang["hi"] = "IN"
  cLang["hne"] = "IN"
  cLang["hr"] = "HR"
  cLang["hsb"] = "DE"
  cLang["ht"] = "HT"
  cLang["hu"] = "HU"
  cLang["hy"] = "AM"
  cLang["ia"] = "FR"
  cLang["id"] = "ID"
  cLang["ig"] = "NG"
  cLang["ik"] = "CA"
  cLang["is"] = "IS"
  cLang["it"] = "IT"
  cLang["iu"] = "CA"
  cLang["ja"] = "JP"
  cLang["kab"] = "DZ"
  cLang["ka"] = "GE"
  cLang["kk"] = "KZ"
  cLang["kl"] = "GL"
  cLang["km"] = "KH"
  cLang["kn"] = "IN"
  cLang["kok"] = "IN"
  cLang["ko"] = "KR"
  cLang["ks"] = "IN"
  cLang["ku"] = "TR"
  cLang["kw"] = "GB"
  cLang["ky"] = "KG"
  cLang["lb"] = "LU"
  cLang["lg"] = "UG"
  cLang["li"] = "BE"
  cLang["lij"] = "IT"
  cLang["li"] = "NL"
  cLang["ln"] = "CD"
  cLang["lo"] = "LA"
  cLang["lt"] = "LT"
  cLang["lv"] = "LV"
  cLang["lzh"] = "TW"
  cLang["mag"] = "IN"
  cLang["mai"] = "IN"
  cLang["mfe"] = "MU"
  cLang["mg"] = "MG"
  cLang["mhr"] = "RU"
  cLang["mi"] = "NZ"
  cLang["miq"] = "NI"
  cLang["mjw"] = "IN"
  cLang["mk"] = "MK"
  cLang["ml"] = "IN"
  cLang["mni"] = "IN"
  cLang["mn"] = "MN"
  cLang["mr"] = "IN"
  cLang["ms"] = "MY"
  cLang["mt"] = "MT"
  cLang["my"] = "MM"
  cLang["nan"] = "TW"
  cLang["nb"] = "NO"
  cLang["nds"] = "DE"
  cLang["ne"] = "NP"
  cLang["nhn"] = "MX"
  cLang["niu"] = "NZ"
  cLang["nl"] = "NL"
  cLang["nn"] = "NO"
  cLang["nr"] = "ZA"
  cLang["nso"] = "ZA"
  cLang["oc"] = "FR"
  cLang["om"] = "ET"
  cLang["or"] = "IN"
  cLang["os"] = "RU"
  cLang["pa"] = "IN"
  cLang["pap"] = "CW"
  cLang["pa"] = "PK"
  cLang["pl"] = "PL"
  cLang["ps"] = "AF"
  cLang["pt"] = "PT"
  cLang["quz"] = "PE"
  cLang["raj"] = "IN"
  cLang["ro"] = "RO"
  cLang["ru"] = "RU"
  cLang["rw"] = "RW"
  cLang["sah"] = "RU"
  cLang["sa"] = "IN"
  cLang["sat"] = "IN"
  cLang["sc"] = "IT"
  cLang["sd"] = "PK"
  cLang["se"] = "NO"
  cLang["sgs"] = "LT"
  cLang["shn"] = "MM"
  cLang["shs"] = "CA"
  cLang["sid"] = "ET"
  cLang["si"] = "LK"
  cLang["sk"] = "SK"
  cLang["sl"] = "SI"
  cLang["sm"] = "WS"
  cLang["so"] = "SO"
  cLang["sq"] = "AL"
  cLang["sr"] = "RS"
  cLang["ss"] = "ZA"
  cLang["st"] = "ZA"
  cLang["sv"] = "SE"
  cLang["sw"] = "KE"
  cLang["szl"] = "PL"
  cLang["ta"] = "LK"
  cLang["tcy"] = "IN"
  cLang["te"] = "IN"
  cLang["tg"] = "TJ"
  cLang["the"] = "NP"
  cLang["th"] = "TH"
  cLang["ti"] = "ER"
  cLang["tig"] = "ER"
  cLang["tk"] = "TM"
  cLang["tl"] = "PH"
  cLang["tn"] = "ZA"
  cLang["to"] = "TO"
  cLang["tpi"] = "PG"
  cLang["tr"] = "TR"
  cLang["ts"] = "ZA"
  cLang["tt"] = "RU"
  cLang["ug"] = "CN"
  cLang["uk"] = "UA"
  cLang["unm"] = "US"
  cLang["ur"] = "PK"
  cLang["uz"] = "UZ"
  cLang["ve"] = "ZA"
  cLang["vi"] = "VN"
  cLang["wa"] = "BE"
  cLang["wae"] = "CH"
  cLang["wal"] = "ET"
  cLang["wo"] = "SN"
  cLang["xh"] = "ZA"
  cLang["yi"] = "US"
  cLang["yo"] = "NG"
  cLang["yue"] = "HK"
  cLang["yuw"] = "PG"
  cLang["zh"] = "CN"
  cLang["zu"] = "ZA"

  For Each sVal In cLang
    sStr &= ":" & cLang.Key & "=" & sVal
    If Len(sStr) > 74 Then
      Print Quote(sStr)
      sStr = ""
    Endif
  Next
  
  Print Quote(sStr & ":")

End

Public Sub ToLanguage(sLang As String) As String
  
  Dim sCountry As String
  Dim iPos As Integer
  Dim sFind As String
  
  iPos = RInStr(sLang, ".")
  If iPos Then sLang = Left(sLang, iPos - 1)
  
  If InStr(sLang, "-") = 0 Then
    
    sFind = ":" & LCase(sLang) & "="
    iPos = InStr(DEFAULT_LANG_DICT, sFind)
    If iPos = 0 Then Return System.Language
    
    sCountry = Mid$(DEFAULT_LANG_DICT, iPos + Len(sFind), 2)
    If Not sCountry Then Return System.Language
    
    Return sLang & "_" & sCountry & ".UTF-8"
  
  Else
    
    Return Replace(sLang, "-", "_") & ".UTF-8"
    
  Endif
  
End
