1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Globalization ;
3
4
using System . Linq ;
4
5
using System . Windows . Input ;
5
6
using Xamarin . Forms ;
@@ -46,6 +47,7 @@ public class PlaygroundViewModel : BaseViewModel
46
47
public bool DayAutoSetStyleBasedOnDayState { get ; set ; } = true ;
47
48
public int ForwardsNavigationAmount { get ; set ; } = 1 ;
48
49
public int BackwardsNavigationAmount { get ; set ; } = - 1 ;
50
+ public string TargetCultureCode { get ; set ; } = CultureInfo . CurrentCulture ? . Name ?? CultureInfo . DefaultThreadCurrentCulture ? . Name ?? CultureInfo . CurrentUICulture ? . Name ?? CultureInfo . DefaultThreadCurrentUICulture ? . Name ?? "en" ;
49
51
public Color CalendarBackgroundColor { get ; set ; } = ( Color ) Application . Current . Resources [ "CalendarBackgroundColor" ] ;
50
52
public Color NavigationBackgroundColor { get ; set ; } = ( Color ) Application . Current . Resources [ "CalendarPrimaryColor" ] ;
51
53
public Color NavigationTextColor { get ; set ; } = ( Color ) Application . Current . Resources [ "CalendarPrimaryTextColor" ] ;
@@ -89,6 +91,7 @@ public class PlaygroundViewModel : BaseViewModel
89
91
public ICommand NavigateCalendarCommand { get ; set ; }
90
92
public ICommand ChangeDateSelectionCommand { get ; set ; }
91
93
public ICommand ChangeCalendarVisibilityCommand { get ; set ; }
94
+ public ICommand UpdateCurrentCultureCommand { get ; set ; }
92
95
#endregion
93
96
94
97
#region Constructors
@@ -119,6 +122,8 @@ public PlaygroundViewModel()
119
122
NavigateCalendarCommand = new Command < int > ( NavigateCalendar ) ;
120
123
ChangeDateSelectionCommand = new Command < DateTime > ( ChangeDateSelection ) ;
121
124
ChangeCalendarVisibilityCommand = new Command < bool > ( ChangeCalendarVisibility ) ;
125
+ UpdateCurrentCultureCommand = new Command ( UpdateCurrentCulture ) ;
126
+ UpdateCurrentCulture ( ) ;
122
127
}
123
128
#endregion
124
129
@@ -187,6 +192,29 @@ public void ChangeCalendarVisibility(bool isVisible)
187
192
{
188
193
CalendarIsVisible = isVisible ;
189
194
}
195
+ public async void UpdateCurrentCulture ( )
196
+ {
197
+ try
198
+ {
199
+ //Set DefaultThreadCurrentCulture because CurrentCulture gets automatically reset when changed.
200
+ CultureInfo . DefaultThreadCurrentCulture = new CultureInfo ( TargetCultureCode ) ;
201
+ CultureInfo . DefaultThreadCurrentUICulture = new CultureInfo ( TargetCultureCode ) ;
202
+
203
+ //This causes the binding converters (which use the current culture) to update.
204
+ //Day Names
205
+ var oldDayNamesOlder = Calendar . DayNamesOrder . ToList ( ) ;
206
+ Calendar . DayNamesOrder . ReplaceRange ( new List < DayOfWeek > ( ) { DayOfWeek . Monday } ) ;
207
+ Calendar . DayNamesOrder . ReplaceRange ( oldDayNamesOlder ) ;
208
+
209
+ //NavigationView Title
210
+ NavigateCalendar ( 1 ) ;
211
+ NavigateCalendar ( - 1 ) ;
212
+ }
213
+ catch
214
+ {
215
+ await Shell . Current . DisplayAlert ( "Invalid Culture Code" , "The specified culture code was invalid." , "OK" ) ;
216
+ }
217
+ }
190
218
public async void ShowCustomDayNamesOrderDialog ( )
191
219
{
192
220
IEnumerable < DayOfWeek > newCustomDayNamesOrder = await PopupHelper . ShowConstructListDialogAsync ( Calendar . CustomDayNamesOrder ?? new ObservableRangeCollection < DayOfWeek > ( ) , Calendar . StartOfWeek . GetWeekAsFirst ( ) ) ;
@@ -291,6 +319,7 @@ public async void ShowDayInvalidTextColorDialog()
291
319
{
292
320
DayInvalidTextColor = await PopupHelper . ShowColorDialogAsync ( DayInvalidTextColor ) ;
293
321
}
322
+
294
323
#endregion
295
324
}
296
325
}
0 commit comments