CLR - GetCultures

Started by stanl, February 20, 2019, 04:21:32 AM

Previous topic - Next topic

stanl


The script below illustrates obtaining the Days of the week between the current Windows culture and an alternate [in this case German]. At the end of the script are lookup values for both culture codes and additional properties for the DateTimeFormat.


What I have been unable to do is obtain the cultures with the GetCultures() method. The MFST docs describe the culture 'types' here:


https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.getcultures?view=netframework-4.7.2


Not sure how WB's CLR handles the bitwise 'type' required and if additional CLR Options are needed. Thought I'd just ask now and not waste the board space with a series of failed attempts. Otherwise, not critical, just part of a learning path.


Code (WINBATCH) Select


;Winbatch 2018B - Using CLR to obtain properties from selected culture
;=======================================================================
IntControl(73,1,0,0,0)
Gosub udfs


ObjectClrOption("useany","System.Globalization")
oCulture = ObjectClrNew("System.Globalization.CultureInfo","es-US")
new = oCulture.CreateSpecificCulture("de-DE")
oCulture = oCulture.CurrentUICulture


r = GetDOW(oCulture,oCulture.ToString())
Message("US",r)


r = GetDOW(new,new.ToString())
Message("German",r)
oCulture=0
new=0


Exit


:WBERRORHANDLER
Terminate(@TRUE,"Error Running Script","Exiting")


;==================================================================
:udfs
#DefineSubRoutine GetDOW(culture,Name)
Dt=culture.DateTimeFormat
cal=Dt.NativeCalendarName
results=Name:"{":cal:")":@LF:"Days of Week":@LF
Dn=Dt.DayNames


ForEach d in Dn
   results=results:d:@LF
Next


Return(results)
#EndSubRoutine


Return


;culture lookup
Country,Name Language-Country code,
Arabic (Saudi Arabia),ar-SA,
Bulgarian (Bulgaria),bg-BG,
Catalan (Catalan),ca-ES,
Chinese (Taiwan),zh-TW,
Czech (Czech Republic),cs-CZ,
Danish (Denmark),da-DK,
German (Germany),de-DE,
Greek (Greece),el-GR,
English (United States),en-US,
Finnish (Finland),fi-FI,
rench (France),fr-FR,
Hebrew (Israel),he-IL,
Hungarian (Hungary),hu-HU,
Icelandic (Iceland),is-IS,
Italian (Italy),it-IT,J
apanese (Japan),ja-JP,
Korean (Korea),ko-KR,
Dutch (Netherlands),nl-NL,
Norwegian, Bokmål (Norway),nb-NO,
Polish (Poland),pl-PL,
Portuguese (Brazil),pt-BR,
Romanian (Romania),ro-RO,
Russian (Russia),ru-RU,
Croatian (Croatia),hr-HR,
Slovak (Slovakia),sk-SK,
Albanian (Albania),sq-AL,
Swedish (Sweden),sv-SE,
Thai (Thailand),th-TH,
Turkish (Turkey),tr-TR,
Urdu (Islamic Republic of Pakistan),ur-PK,
Indonesian (Indonesia),id-ID,
Ukrainian (Ukraine),uk-UA,
Belarusian (Belarus),be-BY,S
lovenian (Slovenia),sl-SI,
Estonian (Estonia),et-EE,
Latvian (Latvia),lv-LV,
Lithuanian (Lithuania),lt-LT,
Persian (Iran),fa-IR,
Vietnamese (Vietnam),vi-VN,
Armenian (Armenia),hy-AM,
Azeri (Latin, Azerbaijan),az-Latn-AZ,
Basque (Basque),eu-ES,
Macedonian (Former Yugoslav Republic of Macedonia),mk-MK,
Afrikaans (South Africa),af-ZA,
Georgian (Georgia),ka-GE,
Faroese (Faroe Islands),fo-FO,
Hindi (India),hi-IN,
Malay (Malaysia),ms-MY,
Kazakh (Kazakhstan),kk-KZ,
Kyrgyz (Kyrgyzstan),ky-KG,
Kiswahili (Kenya),sw-KE,
Uzbek (Latin, Uzbekistan),uz-Latn-UZ,
Tatar (Russia),tt-RU,
Punjabi (India),pa-IN,
Gujarati (India),gu-IN,
Tamil (India),ta-IN,
Telugu (India),te-IN,
Kannada (India),kn-IN,
Marathi (India),mr-IN,
Sanskrit (India),sa-IN,
Mongolian (Cyrillic, Mongolia),mn-MN,
Galician (Galician),gl-ES,
Konkani (India),kok-IN,
Syriac (Syria),syr-SY,
Divehi (Maldives),dv-MV,
Arabic (Iraq),ar-IQ,
Chinese (People's Republic of China),zh-CN,
German (Switzerland),de-CH,
English (United Kingdom),en-GB,
Spanish (Mexico),es-MX,
French (Belgium),fr-BE,
Italian (Switzerland),it-CH,
Dutch (Belgium),nl-BE,
Norwegian, Nynorsk (Norway),nn-NO,
Portuguese (Portugal),pt-PT,
Serbian (Latin, Serbia),sr-Latn-CS,
Swedish (Finland),sv-FI,
Azeri (Cyrillic, Azerbaijan),az-Cyrl-AZ,
Malay (Brunei Darussalam),ms-BN,
Uzbek (Cyrillic, Uzbekistan),uz-Cyrl-UZ,
Arabic (Egypt),ar-EG,
Chinese (Hong Kong S.A.R.),zh-HK,
German (Austria),de-AT,
English (Australia),en-AU,
Spanish (Spain),es-ES,
French (Canada),fr-CA,
Serbian (Cyrillic, Serbia),sr-Cyrl-CS,
Arabic (Libya),ar-LY,
Chinese (Singapore),zh-SG,
German (Luxembourg),de-LU,
English (Canada),en-CA,
Spanish (Guatemala),es-GT,
French (Switzerland),fr-CH,
Arabic (Algeria),ar-DZ,
Chinese (Macao S.A.R.),zh-MO,
German (Liechtenstein),de-LI,
English (New Zealand),en-NZ,
Spanish (Costa Rica),es-CR,
French (Luxembourg),fr-LU,
Arabic (Morocco),ar-MA,
English (Ireland),en-IE,
Spanish (Panama),es-PA,
French (Principality of Monaco),fr-MC,
Arabic (Tunisia),ar-TN,
English (South Africa),en-ZA,
Spanish (Dominican Republic),es-DO,
Arabic (Oman),ar-OM,
English (Jamaica),en-JM,
Spanish (Venezuela),es-VE,
Arabic (Yemen),ar-YE,
English (Caribbean),en-029,
Spanish (Colombia),es-CO,
Arabic (Syria),ar-SY,
English (Belize),en-BZ,
Spanish (Peru),es-PE,
Arabic (Jordan),ar-JO,
English (Trinidad and Tobago),en-TT,
Spanish (Argentina),es-AR,
Arabic (Lebanon),ar-LB,
English (Zimbabwe),en-ZW,
Spanish (Ecuador),es-EC,
Arabic (Kuwait),ar-KW,
English (Republic of the Philippines),en-PH,
Spanish (Chile),es-CL,
Arabic (U.A.E.),ar-AE,
Spanish (Uruguay),es-UY,
Arabic (Bahrain),ar-BH,
Spanish (Paraguay),es-PY,
Arabic (Qatar),ar-QA,
Spanish (Bolivia),es-BO,
Spanish (El Salvador),es-SV,
Spanish (Honduras),es-HN,
Spanish (Nicaragua),es-NI,
Spanish (Puerto Rico),es-PR,
Sami, Southern (Norway),sma-NO,
Serbian (Cyrillic, Bosnia and Herzegovina),sr-Cyrl-BA,
Zulu,zu-ZA,Xhosa,xh-ZA,
Frisian (Netherlands),fy-NL,
Setswana (South Africa),tn-ZA,
Sami, Northern (Sweden),se-SE,
Sami, Southern (Sweden),sma-SE,
Filipino (Philippines),fil-PH,
Sami, Inari (Finland),smn-FI,
Quechua (Peru),quz-PE,
Sami, Northern (Finland),se-FI,
Sami, Skolt (Finland),sms-FI,
Welsh,cy-GB,Croatian (Bosnia and Herzegovina),hr-BA,
Inuktitut (Latin, Canada),iu-Latn-CA,
Bosnian (Cyrillic, Bosnia and Herzegovina),bs-Cyrl-BA,
Mohawk (Mohawk),moh-CA,
Sami, Lule (Norway),smj-NO,
Mapudungun (Chile),arn-CL,
Maori,mi-NZ,
Quechua (Ecuador),quz-EC,
Irish (Ireland),ga-IE,
Romansh (Switzerland),rm-CH,
Serbian (Latin, Bosnia and Herzegovina),sr-Latn-BA,
Sami, Lule (Sweden),smj-SE,
Luxembourgish (Luxembourg),lb-LU,
Sesotho sa Leboa (South Africa),ns-ZA,
Quechua (Bolivia),quz-BO,
Sami, Northern (Norway),se-NO,
Maltese,mt-MT,
Bosnian (Latin, Bosnia and Herzegovina),bs-Latn-BA






;DateTimeFormat properties
AMDesignator                     :
Calendar                         : System.Globalization.GregorianCalendar
DateSeparator                    : .
FirstDayOfWeek                   : Monday
CalendarWeekRule                 : FirstFourDayWeek
FullDateTimePattern              : dddd, d. MMMM yyyy HH:mm:ss
LongDatePattern                  : dddd, d. MMMM yyyy
LongTimePattern                  : HH:mm:ss
MonthDayPattern                  : dd MMMM
PMDesignator                     :
RFC1123Pattern                   : ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
ShortDatePattern                 : dd.MM.yyyy
ShortTimePattern                 : HH:mm
SortableDateTimePattern          : yyyy'-'MM'-'dd'T'HH':'mm':'ss
TimeSeparator                    : :
UniversalSortableDateTimePattern : yyyy'-'MM'-'dd HH':'mm':'ss'Z'
YearMonthPattern                 : MMMM yyyy
AbbreviatedDayNames              : {So, Mo, Di, Mi...}
ShortestDayNames                 : {So, Mo, Di, Mi...}
DayNames                         : {Sonntag, Montag, Dienstag, Mittwoch...}
AbbreviatedMonthNames            : {Jan, Feb, Mrz, Apr...}
MonthNames                       : {Januar, Februar, März, April...}
IsReadOnly                       : False
NativeCalendarName               : Gregorianischer Kalender
AbbreviatedMonthGenitiveNames    : {Jan, Feb, Mrz, Apr...}
MonthGenitiveNames               : {Januar, Februar, März, April...}



td

CultureTypes is an enum so it is used like any other FCL enum.

https://docs.microsoft.com/en-us/dotnet/api/system.globalization.culturetypes?view=netframework-4.7.2

You handle it like any other FCL enum:

Code (winbatch) Select
ObjectClrOption("useany","System.Globalization")
CultureInfo = ObjectClrNew("System.Globalization.CultureInfo","es-ES")

; Cast the value of the enum to the enum's type so the CLR can
; disambigulate the method name.  The cast is not always needed.
; It depends on how any given method is attributed and whether or
; not it is overloaded.  The safe play is to just use a cast.
enumCulTypes = ObjectClrNew("System.Globalization.CultureTypes")
AllCultures = ObjectClrType('System.Globalization.CultureTypes', enumCulTypes.AllCultures)

allCultures = CultureInfo.GetCultures(AllCultures)
foreach ci in allCultures
   Message('Culture', ci.EnglishName:' (':ci.Name:')')
next


Note that you cannot directly access the "CultureTypes" property of a "CultureInfo" object using WIL CLR hosting.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl