NativeScript / plugins

@nativescript plugins to help with your developments.
https://docs.nativescript.org/plugins/index.html
Apache License 2.0
189 stars 107 forks source link

Android and iOS Application's language does not match #533

Open lano-vargas opened 10 months ago

lano-vargas commented 10 months ago

I have implemented the localize plugin, and Im' facing issues:

"get the default language on user's phone via the language property of the Device class."

I have Android which phone language setting is set United Kingdom (en) this picks up correctly. On iOS I also have the setting language to English as preferred language but this isn't printing correctly. I the following code.

    console.log('DEVICE LANGUAGE', device.language);
    console.log('From Cores, Device.language);
    console.log('from __app__language__', ApplicationSettings.getString('__app__language__'))
    const localeOverriddenSuccessfully = overrideLocale(Device.language); 

On Android is correct:

Screenshot 2023-09-29 at 11 45 25

On iOS is incorrect:

Screenshot 2023-09-29 at 11 45 10 Screenshot 2023-09-29 at 11 43 00

ns --version 8.5.3 ✔ Up to date.

Angular: ng version

 _                      _                 ____ _     ___
/ \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|

/ △ \ | ' \ / | | | | |/ _ | '| | | | | | | / _ | | | | (| | || | | (| | | | || |_ | | // __| ||_, |_,||_,|| __|__|| |/

Angular CLI: 15.2.8 Node: 20.4.0 (Unsupported) Package Manager: npm 9.7.2 OS: darwin x64

Angular: 15.1.5 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.1501.6 @angular-devkit/build-angular 15.1.6 @angular-devkit/core 15.1.6 @angular-devkit/schematics 15.2.8 @angular/cli 15.2.8 @ngtools/webpack 15.1.6 @schematics/angular 15.2.8 rxjs 7.6.0 typescript 4.8.4

============================================= It seems to me that on iOS some how it cached into the the phone's default language variables, earlier on I was manually updating the language like so: const localeOverriddenSuccessfully = overrideLocale('nl'); could this have been cached for iOS on this variables prior?

device.language Device.language __app__language__

Thanks

uderline commented 9 months ago

Depending on your code, if you followed the README, you may have added:

Application.on(Application.launchEvent, (args) => {
    if (args.android) {
        androidLaunchEventLocalizationHandler();
    }
});

that resets the language (i.e. remove __app__language setting).

This doesn't exist for iOS so __app__language still exist and therefore is still used to localise your app.

earlier on I was manually updating the language like so

I guess you removed the overrideLocale('nl') so iOS is still using __app__language and android's one has been reset.

Re-installing the app will remove the app settings and use the correct language. If you want to "reproduce" the same behaviour as android, I would simply add:

Application.on(Application.launchEvent, (args) => {
    if (args.android) {
        androidLaunchEventLocalizationHandler();
    }
       if (args.ios) {
        ApplicationSettings.remove('__app__language__');
    }
});

On a personal note, I have never used this androidLaunchEventLocalizationHandler() function and it seems to work :)