facebook

Using LocalStorage variables: SOLVED

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #339071 Reply

    lemorlenny
    Member

    Hi all,
    I’m building an multilanguage app and I have a problem to save e recover the language selected.
    I have a splash screen with 3 image buttons with a national flag each.
    In the OnClick of that buttons event I inserted this code:

    localStorage.setItem("nation","FR"); // 'FR' change in 'UK' and 'SP' in the other buttons

    Then inside the same page I wrote:

    
    phoneui.documentReadyHandler = function() {
    
      idNation = localStorage.getItem("nation");
      
      if(idNation =='FR')   { .....}
      else if(idNation =='UK')   { ..... }
      else if(idNation =='SP')   { ..... }
    }
    
    

    but the getItem return null always until I reload the page, after this the value is correct.

    How can I correct this behaviour?

    Many thanks in advance

    Lenny

    #339072 Reply

    Stu Wade
    Member

    Is this not simply a matter of timing?

    Surely the document handler is called before you have had a chance to save the value.
    On re-entry the value is ready for you – the null return from the handler simply indicates that this is the very first time in, or am I missing something?

    #339074 Reply

    lemorlenny
    Member

    @Stu Wade wrote:

    Is this not simply a matter of timing?

    Surely the document handler is called before you have had a chance to save the value.
    On re-entry the value is ready for you – the null return from the handler simply indicates that this is the very first time in, or am I missing something?

    So you suggest to place a delay after the “localStorage.setItem()” instruction?

    #339078 Reply

    Stu Wade
    Member

    No, the document ready handler executes once only literally as soon as the project is ready to display – this will always be before any on click event can possibly occur.
    I would suggest catching the null storage and going directly to a choose language (for the first time) page.

    #339079 Reply

    lemorlenny
    Member

    I found the solution,
    its necessary insert the language variables reading in the prePageTransition event and here place the translation operations too.

    
    // main page
    
    phoneui.prePageTransition = function(currentScreenId,targetScreenId) {
    
      idNation = localStorage.getItem("Nation");
    
      Translate();
      
      return true;
    }
    
    function Translate() {
      if(idNation =='IT')   {
        $('#m1-YourPage-txtHeader').text("Trova");
     }
      else if(idNation =='UK')   {
        $('#m1-YourPage-txtHeader').text("Find");
      }
      else if(idNation =='FR')   {
         $('#m1-YourPage-txtHeader').text("Trouvez");
       }
    
    }
    

    Best regards

    Lenny

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Using LocalStorage variables: SOLVED

You must be logged in to post in the forum log in