I want to write a settings page where, when you choose a language from a picker, the language changes for the whole page. I use Resx files and EventHandler (PropertyChanged...) for this.
I call the EventHandler when the value of the selected element (CurrentCulture) of the picker changes.
In XAML:
<!--SETTINGSPAGE_LABEL_LANGUAGE returns (get;) text from resx as string with the name SETTINGSPAGE_LABEL_LANGUAGE -->
<Label Text="{Binding SETTINGSPAGE_LABEL_LANGUAGE}"
Grid.Row="0" Grid.Column="0"/>
<!--LanguageList is a string[] with all Languagenames in their specific Language (For example: Deutsch, English)
CurrentCulture saves the name of the culture (set triggers onpropertychanged) -->
<Picker ItemsSource = "{Binding LanguageList}"
SelectedItem="{Binding CurrentCulture}"/>
Some code from the ViewModel:
public string CurrentCulture
{
get
{
return _currentCulture;
}
set
{
MainThread.BeginInvokeOnMainThread(() =>
{
_currentCulture = value;
//Im using the MVVM with Controllers. Thats why i named it ViewModelController
_settingsViewModelController.SetCurrentAppCulture(_currentCulture);
});
}
}
and
public static void SetCurrentAppCulture(string cultureCode, BaseViewModel baseViewModel = null)
{
CultureInfo cultureInfo = new CultureInfo(cultureCode);
//I just tried all methods to set the culture in any way, but nothing helped.
//Like so:
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
//The if-Statement can be deleted. I fixed the error, when the baseViewModel were null.
if (baseViewModel != null)
baseViewModel.OnEveryPropertyChanged();
}
Now the problem is that
does not reload every label when PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(String.Empty)); is called.
But now comes the strange thing.
I currently have 2 languages. When I change the language for the first time, sometimes nothing happens or everything is translated (as it should be).
When I change the language again, not all labels etc. are translated.
If I change the language again, the languages of the texts are inverted (language 1 becomes language 2 and vice versa).
And when I invert the lyrics, it stays inverted even if I change the language again and change it ...
Label1 had language1 -> Label1 now has language 2
Label2 had language2 -> Label2 now has language 1
Label3 had language1 -> Label3 now has language 2
But if I set a breakpoint now, this will not happen.
I get the feeling that it doesn't load fast enough, which I find quite funny, since I do everything on the main thread.
I ran also in the problem, that i can only change the language of the current and future views.
My mainController has a list of all views which I have opened before and can call up again with the Navigator (PopAsync). I have tried to change the language of the old views using this list, but unfortunately this does not work.
Does anyone have an idea how I can solve these two problems?
I've been searching for days and I'm getting desperate.