iOS Question DateUtils - GetMonthName set device localization language

Alexander Stolte

Expert
Licensed User
Longtime User
if i call DateUtils.GetMonthName then it returns the month name in english on a german language device, how can i set the device language to get the month name in the right language?
 
Solution
Hi Alexander

B4X:
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
   
    'Get Language Device and Set Language <----------------
    SetDateTimeLocale(GetPreferredLanguage)
    Log(DateUtils.GetMonthName(DateTime.Now))
   
End Sub

Sub SetDateTimeLocale (locale As String)
    Dim loc As NativeObject
    loc = loc.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc)
    no.GetField("timeFormat").SetField("locale", loc)
End Sub

Sub GetPreferredLanguage As String
    Dim no As NativeObject...

MarcoRome

Expert
Licensed User
Longtime User
Hi Alexander

B4X:
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
   
    'Get Language Device and Set Language <----------------
    SetDateTimeLocale(GetPreferredLanguage)
    Log(DateUtils.GetMonthName(DateTime.Now))
   
End Sub

Sub SetDateTimeLocale (locale As String)
    Dim loc As NativeObject
    loc = loc.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc)
    no.GetField("timeFormat").SetField("locale", loc)
End Sub

Sub GetPreferredLanguage As String
    Dim no As NativeObject
    Return no.Initialize("NSLocale").RunMethod("preferredLanguages", Null).RunMethod("objectAtIndex:", Array(0)).AsString
End Sub
 
Upvote 0
Solution
Top