Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using from the Android settings dialog #32

Open
sthewissen opened this issue Sep 23, 2024 · 3 comments
Open

Using from the Android settings dialog #32

sthewissen opened this issue Sep 23, 2024 · 3 comments

Comments

@sthewissen
Copy link

I wanted to use the NuGet to implement translations, so far so good. However, I didn't want to roll my own select language dialog so for Android I added a xml/locales_config.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
	<locale android:name="en-US"/>
	<locale android:name="nl"/>
</locale-config>

Then I linked that in my manifest:

<application android:allowBackup="true" 
	 android:icon="@mipmap/appicon" 
	 android:roundIcon="@mipmap/appicon_round" 
	 android:supportsRtl="true" 
	 android:localeConfig="@xml/locales_config">	

I have a button that pops open the settings dialog with AppInfo.ShowSettingsUI(). When selecting a different language there and going back into the app it stays on the old language. A full app reboot DOES change the language. Is there something I'm missing or something built into the NuGet that would be able to handle this scenario?

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 23, 2024

Yes, in all scenarios the ILocalizationResourceManager.CurrentCulture need to be set to trigger application language refresh. In this case, it needs to be set after the Locale has been set in Settings.

After some quick testing, I came up with this solution that works, regardless where in the app this is triggered:

using System.Globalization;

namespace LocalizationResourceManager.Maui.Sample
{
    public partial class App : Application
    {
        private readonly ILocalizationResourceManager resourceManager;

        public App(ILocalizationResourceManager resourceManager)
        {
            InitializeComponent();

            this.resourceManager = resourceManager;

            MainPage = new AppShell();
        }

        protected override Window CreateWindow(IActivationState? activationState)
        {
#if ANDROID
            resourceManager.CurrentCulture = CultureInfo.GetCultureInfo(Java.Util.Locale.Default.Language);
#endif
            return base.CreateWindow(activationState);
        }
    }
}

Maybe a more elegant solution would be to have a Locale change listener that trigger the CurrentCulture change and something similar for other platforms. 😄

Note: That the RestoreLatestCulture should not be activated in this case, since that will conflict with setting the language outside the application.

If you find any better solution for this or anything that could be part of the library, I would love your feedback!

Finally I wanted to thank you for trying out my library and it was nice seeing you at the .NET MAUI Day in Cologne in February! 👍

@sthewissen
Copy link
Author

I'll play around with this some more, thanks for that! I'm happy to try it, it's a great library 👍 Nice seeing you indeed! 👌

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 24, 2024

Thanks! ❤️ If you haven't already, you can look at the latest prerelease version on NuGet that includes some really neat features, if I say so myself. 😄 After this, I will now include the option to change Locale in app settings for Android in the sample application. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants