Page 1 of 1

Multilanguage application - how to refresh form object ?

Posted: Mon Feb 01, 2016 7:58 pm
by agb2008
I am creating application that suppose to be available in multiple languages. I've got translation of all text labels,
combobox gadgets, link gadgets e.t.c. I've also would like to change image button picture when I change application language,
so that it display proper language flag image.
My question is - what's the best way to handle such task in PB ? Right now I am closing and recreating form window when I am
changing languages... But something tells me that there should be another way of doing so. I was expecting some kind of "refresh"
option that I could apply to my form so that I would update all items within form or am I asking about something that does not exist
in PB ?

Re: Multilanguage application - how to refresh form object ?

Posted: Mon Feb 01, 2016 8:01 pm
by Keya
have a look at SetGadgetAttribute with #PB_Button_Image flag

Re: Multilanguage application - how to refresh form object ?

Posted: Mon Feb 01, 2016 10:06 pm
by Andre
If you're using Dialogs (see: http://purebasic.com/documentation/dialog/index.html) take a look to the new command RefreshDialog(). This one you can use after changing the gadget contents to recalc the Window/Gadget dimensions (if you've previously created your GUI using the dialog feature).

Re: Multilanguage application - how to refresh form object ?

Posted: Tue Feb 02, 2016 5:06 am
by TI-994A
agb2008 wrote:I am creating application that suppose to be available in multiple languages ... change image button picture when I change application language ... what's the best way to handle such task in PB?
Here's a simple example to illustrate your requirements. It reads some text from the data section, and loads some sample images from my DropBox folder:

* note: PureBasic v5.40 minimum required for the ReceiveHTTPFile() function to work properly with DropBox:

Code: Select all

;requires PureBasic v5.40 minimum for ReceiveHTTPFile() to work properly with DropBox 

Enumeration
  #MainWindow
  #Label1
  #Label2
  #Label3
  #Language
EndEnumeration

Enumeration regions
  #USA
  #Italy
  #France
EndEnumeration

InitNetwork()

ReceiveHTTPFile("https://www.dropbox.com/s/axjsl3w7kgnf3sp/USA.bmp?dl=1",
                GetTemporaryDirectory() + "USA.bmp")
ReceiveHTTPFile("https://www.dropbox.com/s/uqmvo5pcftnqdem/Italy.bmp?dl=1",
                GetTemporaryDirectory() + "Italy.bmp")
ReceiveHTTPFile("https://www.dropbox.com/s/1ydvr4vmc4g8vtx/France.bmp?dl=1",
                GetTemporaryDirectory() + "France.bmp")

LoadImage(#USA, GetTemporaryDirectory() + "USA.bmp")
LoadImage(#Italy, GetTemporaryDirectory() + "Italy.bmp")
LoadImage(#France, GetTemporaryDirectory() + "France.bmp")

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 300, 120, "Multi-Language System", wFlags)
TextGadget(#Label1, 20, 20, 200, 25, "Current language: English")
TextGadget(#Label2, 20, 45, 200, 25, "Welcome to PureBasic!")
TextGadget(#Label3, 20, 70, 280, 30, "Click the flag to change language...")
ButtonImageGadget(#Language, 210, 15, 70, 43, ImageID(#USA))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Language
          language + 1
          If language > #France
            language = #USA
          EndIf
          SetGadgetAttribute(#Language, #PB_Button_Image, ImageID(language))
          Select language:
            Case #USA
              Restore USA
            Case #Italy
              Restore Italy
            Case #France
              Restore France
          EndSelect
          
          Read.s text$
          SetGadgetText(#Label1, text$)
          Read.s text$
          SetGadgetText(#Label2, text$)
          Read.s text$
          SetGadgetText(#Label3, text$)
          
      EndSelect
  EndSelect
Until appQuit = 1

DataSection:
  USA:
    Data.s "Current language: English", "Welcome to PureBasic!", 
           "Click the flag to change language..."
  Italy:
    Data.s "Lingua corrente: Italiano", "Benvenuti a PureBasic!",
           "Cliccando sulla bandiera cambiare la lingua..."
  France:
    Data.s "Langage courant : Français", "Bienvenue à PureBasic!", 
           "Cliquez sur le drapeau pour changer de langue..."
EndDataSection
If you do not wish to load those images, simply substitute them with some of your own.

Hope it helps. :wink:
EDITS wrote:18th February 2019: updated download links

Re: Multilanguage application - how to refresh form object ?

Posted: Tue Feb 02, 2016 7:14 am
by agb2008
Keya:

Thanks ! I am using that function.

Andre:

I do not use Dialogs (was not aware of this option, would have a look, thanks !)


TI-994A:

Thank you for providing this great example ! Looks like main issue that I am facing is an attempt
to bring (use) slightly different approach in programming brought from other flavor of Basic implementation...
But looks like there are slightly different was of implementing same tasks in PB that could improve
all process ! :D