Phonegap local storage
I'm developing a multi-language PhoneGap app and would like to store the user's language preference. On first launch of the app a splash screen would come up with 4 flags, user picks, and then is directed to a landing page in 开发者_JAVA技巧that language. From then on the app load skips the splash screen and goes straight to the landing page in the preferred language. Easy to do? Suggestions? I'm familiar with cookies/local storage but if anyone has done this before I would appreciate specific directions. Thanks.
What you describe should be very easy to do with localStorage. I researched it before using it on Android, and found this webpage to have very helpful code examples and explanations. Very short and sweet.
To save to local storage use:
function saveLanuageID(languageID)
window.localStorage.setItem('lang', languageID);
Read from local storage
function readLanguageID(){
if (typeof window.localStorage.getItem('lang')!== 'undefined' && window.localStorage.getItem('lang')!=null) {
return window.localStorage.getItem('lang');
}
else return false
}
You would have to test for saved language using the code above
if(!readLanguageID())
//redirect to splash
I would prefer you to use local storage as using cookies gave some problem in IOS.
I had the same problem and I took a different approach.
The language files are in separated directories, e.g.
/assets/www/de/index.html
/assets/www/en/index.html
The user can select the language in every web application via a button in the toolbar which opens a dialog.
In the start activity/screen I want to select the chosen language when I start the application, so I have to know natively (Android, iOS) which language was selected. But the language is selected in the web app.
So I created a PhoneGap plugin (currently only Android, but iOS will follow) which allows me to set a key/value pair. In the web app I set the language. Natively (in the activity) I read the language (get the key/value pair) and select the proper index.html.
I know that there are solutions available which allows me to store date within the web application. But I wanted a solution which allows me to access the values from both sides (natively and web).
精彩评论