I'm trying to create an app that supports multilabel languages, in which I have a settings page that allows the user to chose a language, then the View Model would publish a message using the MessagingCenter, then finally, MainActivity would respond to this message
and that's the code sample for the above mentioned
private async void HandleSelectedLanguageChanged()
{
if (SelectedLanguage == null || !this.isInitialized)
return;
MessagingCenter.Send(this, Constants.LanguageChanged, SelectedLanguage);
ShowLoading();
await Task.Delay(1000);
await NavigationService.NavigateAsync($"/{Routes.Landing}/{CallbackPage ?? Routes.Login}");
HideLoading();
}
MainAcitivty
CultureInfo.CurrentCulture = selectedCulture;
CultureInfo.CurrentUICulture = selectedCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = selectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = selectedCulture;
var selectedCulture = Common.Settings.Settings.CurrentCultureInfo.ToDescriptionString().Split('-');
if (selectedCulture.Length == 2)
{
var selectedLang = selectedCulture[0];
var selectedCountry = selectedCulture[1];
var locale = new Java.Util.Locale(selectedLang, selectedCountry);
Java.Util.Locale.Default = locale;
var config = new Android.Content.Res.Configuration { Locale = locale };
Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);
}
the code in the MainActivity was simplified for the purpose of this question there are some device specific cultural configurations that are not relevant to this question, also I made sure that selectedCulture contains the intended culture, the code of creating this object is irrelevant to this question.
The weird thing is, the culture is only changed for the first page I navigate to, and reverts back for every other page in the app. here's the code for the translation MarkupExtension that I use for this
[ContentProperty("Text")] public class BaseTranslateExtension : IMarkupExtension { private string _resourceId; private Type _assemblyType;
public BaseTranslateExtension(string resourceId, Type assemblyType)
{
_resourceId= resourceId;
_assemblyType = assemblyType;
}
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return null;
ResourceManager resourceManager = new ResourceManager(_resourceId, _assemblyType.GetTypeInfo().Assembly);
return resourceManager.GetString(Text, CultureInfo.CurrentCulture);
}
}
the CurrentCulture seems to only be set to the new culture for the first page only, for every other page it's just the same old value, which is weird since it's a static property that I'm sure is not being set anywhere else in the code