error when trying to change APP language in some device

Gerson Jose Rodriguez Quintero 51 Reputation points
2022-06-28T18:07:27.44+00:00

good afternoon I'm trying to force my application to run in es-MX language through the following code, in mainActivity

protected override void OnResume()  
    {  
        base.OnResume();  
  
        try  
        {  
            var userSelectedCulture = new CultureInfo("es-MX");  
  
            Thread.CurrentThread.CurrentCulture = userSelectedCulture;  
        }  
        catch (Exception exception)  
        {  
  
            Console.WriteLine(exception.Message);  
        }  
    }  

oncreate

try  
        {  
            string cultureName = "es-MX";  
            var locale = new Java.Util.Locale(cultureName);  
            Java.Util.Locale.Default = locale;  
  
            var configg = new Android.Content.Res.Configuration { Locale = locale };  
            BaseContext.Resources.UpdateConfiguration(configg, BaseContext.Resources.DisplayMetrics);  
        }  
        catch (Exception e)  
        {  
            Console.WriteLine(e);  
  
        }  

the code works on some devices but on others, it generates this error

System.Reflection.TargetInvocationException:* 'Exception has been thrown by the target of an invocation.'

Can you help me with a solution?

thxs

Developer technologies .NET Xamarin
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.