Issue
I have a requirement to change language of App on selection of radio buttons. App already has localization implemented using Android Resources. I have tried below solutions in different combinations from Activity as well as normal class, but nothing worked so far:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(lang); // lang => en-US or nl-BE
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
var locale = new Java.Util.Locale(lang);
Java.Util.Locale.Default = locale;
Android.Content.Res.Configuration config = new Android.Content.Res.Configuration();
config.Locale = locale;
var context = Application.Context;
context.Resources.Configuration.Locale = locale;
BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);
context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
string title = Application.Context.GetString(Resource.String.title_settings);
Above 'title' never shows string in language other than that of the device. Any idea what could be missing?
Thanks in advance.
Solution
I found the solution after a bit of hit and trial - Found that I was using a wrong constructor of Locale, and simply changing it started reflecting the updated language. Below is the working code:
var locale = new Java.Util.Locale(lang, region); // lang => nl; region => BE
Java.Util.Locale.Default = locale;
var context = Application.Context;
context.Resources.Configuration.Locale = locale;
context.Resources.UpdateConfiguration(context.Resources.Configuration, context.Resources.DisplayMetrics);
string title = Application.Context.GetString(Resource.String.title_settings); // Reflects text in updated language
Answered By - SnapADragon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.