viewpoint-particle

Author Topic: System.Globalization.CultureInfo  (Read 50 times)

stanl

  • Pundit
  • *****
  • Posts: 863
System.Globalization.CultureInfo
« on: February 21, 2019, 09:54:03 am »
Basic script is done and tested. Thanks Tony for your response/help. Might make a good addition to the Help DB for people interested in investigating .NET classes through WB.


Code: Winbatch

;Winbatch 2018B - Using CLR to obtain properties from selected culture
;Stan Littlefield / Tony Deets 2/20/2019
;
;for MFST class overview
;https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=netframework-4.7.2
;====================================================================================================
IntControl(73,1,0,0,0)
Gosub udfs


ObjectClrOption("useany","System.Globalization")
;open the CultureInfo class
oCulture = ObjectClrNew("System.Globalization.CultureInfo","es-US")


;this would obtain the locale of your pc/laptop
;oCulture = oCulture.CurrentUICulture


;thanks to Tony for help on this secion
;cast the culture type prior to calling the GetCultures method
enumCulTypes = ObjectClrNew("System.Globalization.CultureTypes")


;uncomment if you want to see the complete list
;AllCultures = ObjectClrType('System.Globalization.CultureTypes', enumCulTypes.AllCultures)
;allCultures = oCulture.GetCultures(AllCultures)


;list is grouped by language
NeutralCultures = ObjectClrType('System.Globalization.CultureTypes', enumCulTypes.NeutralCultures)
allCultures = oCulture.GetCultures(NeutralCultures)


;display tab-delimited list of countries/codes and allow single selection
;========================================================================
results=""
foreach ci in allCultures
   results=results:ci.EnglishName:",":ci.Name:@TAB
next


code= AskItemList("Select Windows Culture",results,@TAB,@SORTED,@SINGLE)
country=ItemExtract(1,code,",")
code=ItemExtract(2,code,",")
;========================================================================


new = oCulture.CreateSpecificCulture(code)
r = GetTimeformat(new,code)  ;gather data in UDF




Message(country,r)
oCulture=0
new=0


Exit


:WBERRORHANDLER
ErrorProcessing(0,1,0,0)
Exit


:CANCEL
oCulture=0
new=0
Terminate(@TRUE,"Script Aborted","No Code chosen")
Exit


;==================================================================
:udfs
#DefineSubRoutine GetTimeformat(culture,code)
IntControl(73,1,0,0,0)
Dt=culture.DateTimeFormat
cal=Dt.NativeCalendarName
results=code:" {":cal:") TimeFormats":@LF
Dn=Dt.DayNames
tmp=""
ForEach d in Dn
   tmp=tmp:d:","
Next
tmp=StrSub(tmp,1,strlen(tmp)-1)


Mn=Dt.MonthNames
tmp1=""
ForEach m in Mn
   tmp1=tmp1:m:","
Next
tmp1=StrSub(tmp1,1,strlen(tmp1)-1)




results=results:"Days of Week: ":tmp:@LF
results=results:"Months: ":tmp1:@LF
wrule="FirstDay,FirstFourDayWeek,FirstFullWeek"
results=results:"CalendarWeekRule: ":ItemExtract(Dt.CalendarWeekRule,wrule,","):@LF


results=results:"FirstDayOfWeek: ":ItemExtract(Dt.FirstDayOfWeek,tmp,","):@LF
results=results:"FullDateTimePattern: ":Dt.FullDateTimePattern:@LF
results=results:"RFC1123Pattern: ":Dt.RFC1123Pattern:@LF
Return(results)


:WBERRORHANDLER
ErrorProcessing(0,1,0,0)
Exit


#EndSubRoutine


#DefineSubroutine ErrorProcessing(deleteIni,showerr,logfile,Err_Array)
If Vartype(Err_Array) ==256
   WbError = Err_Array[0]
   wberrorhandlerline = Err_Array[1]  
   wberrorhandleroffset = Err_Array[2]  
   wberrorhandlerassignment = Err_Array[3]  
   wberrorhandlerfile = Err_Array[4]
   wberrortextstring = Err_Array[5]  
   wberroradditionalinfo = Err_Array[6]  
   wberrorinsegment = Err_Array[7]  
Else
   WbError = LastError()
Endif
WbTextcode = WbError
If WbError==1668||WbError==2669||WbError==3670
   WbError = ItemExtract(1,IntControl(34,-1,0,0,0),":")
   WbTextcode = -1
EndIf
WbErrorString = IntControl(34,WbTextcode,0,0,0)
WbErrorDateTime = TimeYmdHms()
If deleteIni
   WbErrorFile = StrCat(ShortCutDir( 'AppData', 0, 0 ),'\WinBatch\Settings\')
   If ! DirExist(WbErrorFile) Then WbErrorFile = DirWindows(0)
   WbErrorFile = StrCat(WbErrorFile,"WWWBATCH.INI")
   FileDelete(WbErrorFile)
   ;IniWritePvt(WbErrorDateTime,"CurrentScript",WbErrorHandlerFile      ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ErrorValue"   ,WbError                 ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ErrorString"  ,WbErrorString           ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ScriptLine"   ,WbErrorHandlerLine      ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ScriptOffset" ,WbErrorHandlerOffset    ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"VarAssignment",WbErrorHandlerAssignment,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"VarInSegment" ,WbErrorInSegment,WbErrorFile)
   IniWritePvt("","","",WbErrorFile)
Endif  
WbErrorMsgText = StrCat(WbErrorDateTime,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Current Script: ",WbErrorHandlerFile,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Error# [",WbError,"]",@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Error Text: ",wberrortextstring,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"[Extended Information] ",wberroradditionalinfo,@CRLF,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"On Line:",@CRLF,WbErrorHandlerLine,@CRLF)
;WbErrorMsgText = StrCat(WbErrorMsgText,"Offset: ",WbErrorHandlerOffset,@CRLF)
If (WbErrorHandlerAssignment>"") Then %WbErrorHandlerAssignment% = "UNKNOWN"
WbErrorMsgText = StrCat(WbErrorMsgText,"Assignment/Variable: ",WbErrorHandlerAssignment,@CRLF)
If (WbErrorInSegment>"") Then WbErrorMsgText = StrCat(WbErrorMsgText,"In UDF/UDS: ",WbErrorInSegment,@CRLF)
If logfile
   cSep = StrCat(StrFill("=",50),@CRLF)
   cLogFile = StrCat(dirscript(),"log.err")
   If ! FileExist(cLogFile) Then FilePut(cLogFile,StrCat("Error Log",@CRLF,cSep))
   FilePut(cLogFile,StrCat(FileGet(cLogFile),WbErrorMsgText,cSep))
   display(2,"An Error Occured",StrCat("written to ",cLogFile))
Else
   If showerr
      WbErrorMsgText = StrCat(WbErrorMsgText,"[THIS ERROR NOT WRITTEN TO LOG FILE]",@CRLF)
      Message("An Error Was Encountered",WbErrorMsgText)
   Endif
Endif
Return(1)
#EndSubroutine




Return