Changing a Global Variable through menu options

Hello,

I recently started using Corona and I am not an experienced programmer so first of all sorry for my question.

After two weeks of intense “try and error” and not finding anything that helps me online I decided to post my question here, hopping for a solution :slight_smile:

My book-app is a multi language one. Inside the few options in the menu there is one that allows to choose between three languages.

My plan was to create a Global Variable that ruled the language of the entire app, from the menu to the texts of the book.

Having all the three different language texts, this Global Variable would point at which language was chosen by the user. The image of three flags showed the options. Obviously one language must be offered by default ( stored in the global variable).

Anything I tried can’t change the default selection I provided and it makes impossible to choose any other language.

And here is the last code I tried, simplified:

[lua]

– language: Global Variable 

       _L = “es”

          

– Language Options

 

local textos = {}

  textos.es = {“Espanol”, “Ingles”, “Ruso”}

  textos.en = {“Spanish”, “English”, “Russian”}

  textos.ru = {“Rus1”, “Rus2”, “Rus3”}

           

     local es = display.newText( textos[_L][1], _W * 0.5, 80, native.systemFont, 36 )

        es:setTextColor (255, 255, 255)

     local en = display.newText( textos[_L][2], _W * 0.5, 120, native.systemFont, 36 )

        en:setTextColor (255, 255, 255)

     local ru = display.newText( textos[_L][3], _W * 0.5, 160, native.systemFont, 36 )

        ru:setTextColor (255, 255, 255)

 

     function en:tap(event)

      _L = “en”

      return _L

      end

      

     function es:tap(event)

      _L = “es”

      return _L

      end

      

     function ru:tap(event)

      _L = “ru”

      return _L

      end          

          

    es:addEventListener (“tap”, es)

    en:addEventListener (“tap”, en)

    ru:addEventListener (“tap”, ru)

[/lua]

I understand it can be done but I can’t find how.

Thank you very much in advance.

Sergio

You have to change the text values of the display.newText()'s when you change your languages:

     function en:tap(event)           \_L = "en"            en.text = textos[\_L][2]  -- since the 2nd entry in the "en" array is the value for the english button.           return true       end

 

Rob

Thank you very much for your reply!

I will work with your solution.

Hello again,

I am finally able to change the menu language through the flag icons but the previous menu is not going away and everytime I “tap” in the flag the new image menu appears on the previous.

I tryed to remove the objects but I can’t find the right place:

[lua]

_W = display.contentWidth

_H = display.contentHeight

  – Function to switch the language

  

   local function switchLanguage(event)

      local click = event.target.lang

        

      – Menu (Display object)

      local yoLeo = display.newImageRect( “yoLeo”…click…".png", 204, 57)

        yoLeo.x = _W * 0.5

        yoLeo.y = _H * 0.6

   end

  

  – Buttons to switch the language (Flag Icons) –

  

   local esButton = display.newImageRect( “Icon.png”, 57, 57)

      esButton.x = _W * 0.3

      esButton.y = _H * 0.2

      esButton.lang = “es”

      esButton:addEventListener(“tap”, switchLanguage)

     

   local enButton = display.newImageRect( “IconEN.png”, 57, 57)

      enButton.x = _W * 0.5

      enButton.y = _H * 0.2

      enButton.lang = “en”

      enButton:addEventListener(“tap”, switchLanguage)

      

   local ruButton = display.newImageRect( “IconRU.png”, 57, 57)

      ruButton.x = _W * 0.7

      ruButton.y = _H * 0.2

      ruButton.lang = “ru”

      ruButton:addEventListener(“tap”, switchLanguage)

[/lua]

How can I remove the previous menu when the new menu will appear?

You have to change the text values of the display.newText()'s when you change your languages:

     function en:tap(event)           \_L = "en"            en.text = textos[\_L][2]  -- since the 2nd entry in the "en" array is the value for the english button.           return true       end

 

Rob

Thank you very much for your reply!

I will work with your solution.

Hello again,

I am finally able to change the menu language through the flag icons but the previous menu is not going away and everytime I “tap” in the flag the new image menu appears on the previous.

I tryed to remove the objects but I can’t find the right place:

[lua]

_W = display.contentWidth

_H = display.contentHeight

  – Function to switch the language

  

   local function switchLanguage(event)

      local click = event.target.lang

        

      – Menu (Display object)

      local yoLeo = display.newImageRect( “yoLeo”…click…".png", 204, 57)

        yoLeo.x = _W * 0.5

        yoLeo.y = _H * 0.6

   end

  

  – Buttons to switch the language (Flag Icons) –

  

   local esButton = display.newImageRect( “Icon.png”, 57, 57)

      esButton.x = _W * 0.3

      esButton.y = _H * 0.2

      esButton.lang = “es”

      esButton:addEventListener(“tap”, switchLanguage)

     

   local enButton = display.newImageRect( “IconEN.png”, 57, 57)

      enButton.x = _W * 0.5

      enButton.y = _H * 0.2

      enButton.lang = “en”

      enButton:addEventListener(“tap”, switchLanguage)

      

   local ruButton = display.newImageRect( “IconRU.png”, 57, 57)

      ruButton.x = _W * 0.7

      ruButton.y = _H * 0.2

      ruButton.lang = “ru”

      ruButton:addEventListener(“tap”, switchLanguage)

[/lua]

How can I remove the previous menu when the new menu will appear?