Hello everyone, I’m developing in Ionic/angular and i was trying to understand the AppPreferences plugin
It kinda worked with a boolean value, but then i tried to store the app language for localization, so with a string.
I’d like to store the device navigator.language as a default value, then use it if the app is newly installed (no settings setted), but i can’t make it work.
I looked into the API and the library. Maybe I’m wrong, but I can’t understand why the store() function says the dict is optional while is the key which is declared as optional, i.e.
/**
* Get a preference value
*
* @param {string} dict Dictionary for key (OPTIONAL)
* @param {string} key Key
* @return {Promise} Returns a promise
*/
fetch(dict: string, key?: string): Promise;
Same thing in the store() function:
/**
* Set a preference value
*
* @param {string} dict Dictionary for key (OPTIONAL)
* @param {string} key Key
* @param {any} value Value
* @return {Promise} Returns a promise
*/
store(dict: string, key: string, value?: any): Promise;
Am I wrong or is kinda senseless tostoresomething without avalue?
Back to my problem, here’s a sample code for what I was trying to do:
initializeApp(): void {
this.platform.ready().then(async () => {
await this.appPreferences.fetch("settings", "defaultLanguage")
.then(async (inDefLang) => {
console.log("APP defLang read:", { inDefLang },JSON.stringify(inDefLang));
if (inDefLang === "") {
console.log( "defLang is not set => ", { inDefLang },JSON.stringify(inDefLang));
const toDefLang = navigator.language.slice(0, 2);
await this.appPreferences.store("preferences", "defaultLanguage", toDefLang)
.then(async (initDefLang) => {
await this.appPreferences.store("preferences", "defaultLanguage", toDefLang );
console.log("Init defLang to",{ toDefLang }, "=>", initDefLang );
await this.appPreferences .fetch("settings", "defaultLanguage")
.then((defLang) => {
console.log("APP defLang end:",{ defLang },"=>",JSON.stringify(defLang));
});
});
} else {
console.log("defLang is set => ", inDefLang);
}
});
});
}
The rest of the component is working and the imports/modules declarations are working
The output is:
APP defLang read: “”
defLang is not set => “”
Init defLang to {toDefLang: “en”} => OK
APP defLang end: => “”
I tried filling it with async calls but nothing changed
I’m still learning so feel free to criticize if you can
Thanks
1 post - 1 participant