B4A Class Language selector

Hello all;

I'm not a professional, I am beginner. This is first Class.
This for multi lanugage App. I hope you enjoy.

note: all flags are in attachment.

use on main

B4X:
'******************MAIN ACTIVITY***********************
' you can min 1 or 8 language type,
'lang_select.Initialize("pt","dil",Activity,Me,True)
'lang_select.Initialize("en-it-pt-es-de","dil",Activity,Me,True)
lang_select.Initialize("en-tr-fr-ru-it-pt-es-de","dil",Activity,Me,True)
' it is return call sub "Language_Loaded"

Sub Language_Loaded(langbook As Map,language As String)
    Log("language loaded:"&langbook.Get("key"))
    Log("language::"&language)
End Sub




B4X:
'Class module

Sub Class_Globals
    Private pnl As Panel
    Dim language As String
    Type pozitions (obj_top As Int,obj_left As Int,obj_with As Int,obj_heigth As Int)
End Sub
'https://www.b4x.com/android/forum/threads/localize-your-app-using-androidresources.19165/
'Lib >> reflection
'Initializes the object. You can add parameters to this method if needed.
'Ülke bayrakları buradan yuklenir / Flag resuerce web site
'https://tr.wikipedia.org/wiki/%C3%9Clke_bayraklar%C4%B1_listesi
'https://en.wikipedia.org/wiki/ISO_639-1
'************************************
'English     >> en      >>    flag_en.png
'German     >> de      >>    flag_de.png
'French     >> fr     >>    flag_fr.png
'Italian     >> it    >>    flag_it.png
'Portuguese >> pt    >>    flag_pt.png
'Spanish     >> es    >>    flag_es.png
'Turkish     >> tr    >>    flag_tr.png
'Russian     >> ru    >>    flag_ru.png
'************************************
' you must load flag pictures in app files
' ***.intalize("fr-tr-ru") '<for only options for language French, turkish, russian>
'Initialize("fr-tr-ru","Dil",Activity,Me,True) 'Initialize the Class.
'--------------for main activities results -------------------------------------
'Sub Language_Loaded(langbook As Map,language As String)
'Log("language loaded")
'End Sub
Public Sub Initialize(lang As String,EventName As String,Parent As Panel,Module As Object,ActivityTouch As Boolean) 'As String 
    'Dim language As String
    'Log ("DİL="&language)
    File.Delete(File.DirInternal,"lang.dat")
 
 
    If File.Exists(File.DirInternal,"lang.dat") Then
        language=File.ReadString(File.DirInternal,"lang.dat")
        Log("selected lang:"&language)
        Dim langbook As Map
        langbook.Initialize
        langbook.Put("key","sample")
        langbook.Put("key2","valuse") ' this is example, you add this words...
        '...
        '....
        '.....
        '......
        CallSubDelayed3(Main,"Language_Loaded",langbook,language)
    Else
        ' OPEN LANGUAGE SLECTION MENU
        Dim r As Reflector
        r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
        language = r.RunMethod("getLanguage")
        Log("new selection lang:"&language) 
        pnl.Initialize("pnl")
        Parent.AddView(pnl,0,0,100%x,100%y)
        pnl.Color=Colors.Green
        Dim i As Int=0
        Dim picture(8) As ImageView     
        Dim lbl(8) As Label
        Dim pos(8) As pozitions
        Dim lbl_height=20dip
        '---------------------------------------------
        If 100%x<100%y Then
        For i=0 To 7
            pos(i).Initialize
            pos(i).obj_with=30%x
            pos(i).obj_heigth=30%x*(145/220)'flag diemsions rate
            If Ceil(i/2)*2=i Then
                pos(i).obj_left=12%x
                Else
                pos(i).obj_left=58%x
                End If
         
            Dim ss As Double=(100%y-(pos(i).obj_heigth)*4 - 4*lbl_height)/5
            Select i
            Case 0,1
                pos(i).obj_top=ss
            Case 2,3
                pos(i).obj_top=1*(ss+pos(0).obj_heigth+lbl_height)+ss
            Case 4,5
                pos(i).obj_top=2*(ss+pos(0).obj_heigth+lbl_height)+ss
            Case 6,7
                pos(i).obj_top=3*(ss+pos(0).obj_heigth+lbl_height)+ss
            End Select
        Next
     
        Else '------------------------------------------------------------------
            For i=0 To 7
            pos(i).Initialize
            pos(i).obj_with=20%x
            pos(i).obj_heigth=20%x*(145/220)'flag diemsions rate
         
            Select i
                Case 0,4
                    pos(i).obj_left=20%x/5
                Case 1,5
                    pos(i).obj_left=(20%x/5+20%x)*1 +20%x/5
                Case 2,6
                    pos(i).obj_left=(20%x/5+20%x)*2+20%x/5
                Case 3,7
                    pos(i).obj_left=(20%x/5+20%x)*3+20%x/5
            End Select
         
         
         
            Dim ss As Double=(100%y-(pos(i).obj_heigth)*2 - 2*lbl_height)/3
            Select i
            Case 0,1,2,3
                pos(i).obj_top=ss
            Case 4,5,6,7
                pos(i).obj_top=ss+pos(0).obj_heigth+lbl_height+ss
            End Select
        Next     
     
     
        End If
     
        '--------------------------------------------------------------
     
        i=0
        If lang.IndexOf("tr")>=0 Then
            picture(i).Initialize("picture")'("picture_"&i)
            picture(i).tag="tr"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_tr.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="tr"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Türkçe"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
        If lang.IndexOf("en")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="en"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_en.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="en"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="English"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
     
        If lang.IndexOf("de")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="de"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_de.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="de"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Deutsch"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
        If lang.IndexOf("fr")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="fr"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_fr.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="fr"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Français"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
        If lang.IndexOf("it")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="it"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_it.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="it"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Italiano"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If     
        If lang.IndexOf("es")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="es"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_es.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="es"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Español"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If
            If lang.IndexOf("pt")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="pt"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_pt.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="pt"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="Português"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
        End If
            If lang.IndexOf("ru")>=0 Then
            picture(i).Initialize("picture")
            picture(i).tag="ru"
            picture(i).Bitmap=LoadBitmapSample(File.DirAssets,"flag_ru.png",220dip,145dip)
            picture(i).Gravity=Gravity.FILL
            lbl(i).Initialize("lbl_"&i)
            lbl(i).tag="ru"
            lbl(i).Color=Colors.Blue
            lbl(i).Text="русский"
            lbl(i).Gravity = Gravity.CENTER
            pnl.AddView(picture(i),pos(i).obj_left,pos(i).obj_top,pos(i).obj_with,pos(i).obj_heigth)
            pnl.AddView(lbl(i),pos(i).obj_left,pos(i).obj_top+pos(i).obj_heigth,pos(i).obj_with,lbl_height)
            i=i+1
        End If 
 
    'Return language
End Sub
Sub picture_Click
    Dim Pic_Sender As ImageView
    Pic_Sender = Sender
     language=Pic_Sender.Tag
    Dim subj,aprv,cancl,titl As String
 
     Select Pic_Sender.Tag
         Case "tr"
            subj="Uygulamızın dili Türkçe"
            aprv="Kabul et"
            cancl="İptal et"
            titl="Emin misiniz?"
        Case "en"
            subj="Application language is English"
            aprv="Accept"
            cancl="Cancel"
            titl="Aru you sure?"
        Case "fr"
            subj=("Langue de l'application est le Français")
            aprv="Accepter"
            cancl="Annuler"
            titl="Es-tu sûr?"
        Case "ru"
            subj=("Язык приложения является русский")
            aprv="принимать"
            cancl="отменить"
            titl="Вы уверены?"
        Case "es"
            subj=("Idioma de la aplicación es el Español")
            aprv="Aceptar"
            cancl="Cancelar"
            titl="Estas seguro?"
        Case "pt"
            subj=("Linguagem de aplicação é o Português")
            aprv="Aceitar"
            cancl="Cancelar"
            titl="Você tem certeza?"
        Case "it"
            subj=("Lingua L'applicazione è italiano")
            aprv="Accettare"
            cancl="Cancellare"
            titl="Sei sicuro?"
        Case "de"
            subj=("Anwendungssprache ist Deutsch")
            aprv="Akzeptieren"
            cancl="Stornieren"
            titl="Sind Sie sicher?"
    End Select
 
    Dim reslt As Int
    Dim pict As Bitmap
    If File.Exists(File.DirAssets,"flag_"&language&".png") Then
            Log("resim var")
            pict=LoadBitmapSample(File.DirAssets,"flag_"&language&".png",(20dip)/(145/220),20dip)
        Else
            If File.Exists(File.DirAssets,"logo.png") Then
                pict=LoadBitmapSample(File.DirAssets,"logo.png",20dip,20dip)
                Else
                pict.InitializeMutable(20dip,20dip)
                End If
        End If
 
    If DialogResponse.POSITIVE=Msgbox2(subj,titl,aprv,"",cancl,pict) Then
        Log("kayıt edildi" & language)
        File.WriteString(File.DirInternal,"lang.dat",language)
        DoEvents
        pnl.RemoveView
        Dim langbook As Map
        langbook.Initialize
        langbook.Put("key","valuse") ' this is example, you add this words...
        '...
        '....
        '.....
        '......
        CallSubDelayed3(Main,"Language_Loaded",langbook,language)
    End If
 
End Sub
Screenshot_2015-08-13-17-56-25.png Screenshot_2015-08-13-17-56-33.png Screenshot_2015-08-13-17-56-40.png Screenshot_2015-08-13-17-56-53.png
 

Attachments

  • Flags.zip
    19.6 KB · Views: 414
  • Language Selector.zip
    416.6 KB · Views: 469
Last edited:
Top