Android Question [Solved] localization example in B4XPages, how ?

AnandGupta

Expert
Licensed User
Longtime User
I have taken below example for localization,

It has below code to change the language on the fly,
B4X:
Sub lstLanguages_ItemClick (Position As Int, Value As Object)
    Starter.loc.ForceLocale(Value)
    Activity.Finish
    StartActivity(Me)
End Sub

I need similar in B4XPage project, I tried to emulate it as
B4X:
Private Sub lstLanguages_ItemClick (Position As Int, Value As Object)
    Log(Position & " " & Value)
    loc.ForceLocale(Value)

    B4XPages.GetNativeParent(Me).Finish
    StartActivity(Me)

End Sub

But the app just close down. How to achieve the same in B4XPages ?
 
Solution
A B4XPages example (B4A only implemented) is attached. It is much simpler with B4XPages.

Complete code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private edtName As B4XView
    Private lblHello As B4XView
    Private lstLanguages As ListView
    Public loc As Localizator
    Private name As String
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    loc.Initialize(File.DirAssets, "strings.db")
    ResetLayout
End Sub

Private Sub ResetLayout
    Root.RemoveAllViews
    Root.LoadLayout("1")
    If loc.Language = "he" Then FlipLayout
    loc.LocalizeLayout(Root)
    lstLanguages.SingleLineLayout.Label.Gravity = Gravity.CENTER...

Erel

B4X founder
Staff member
Licensed User
Longtime User
A B4XPages example (B4A only implemented) is attached. It is much simpler with B4XPages.

Complete code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private edtName As B4XView
    Private lblHello As B4XView
    Private lstLanguages As ListView
    Public loc As Localizator
    Private name As String
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    loc.Initialize(File.DirAssets, "strings.db")
    ResetLayout
End Sub

Private Sub ResetLayout
    Root.RemoveAllViews
    Root.LoadLayout("1")
    If loc.Language = "he" Then FlipLayout
    loc.LocalizeLayout(Root)
    lstLanguages.SingleLineLayout.Label.Gravity = Gravity.CENTER
    lstLanguages.SingleLineLayout.Label.TextColor = Colors.Gray
    Dim availableLangs() As String = Array As String ("en", "fr", "he", "de")
    For i = 0 To availableLangs.Length - 1
        lstLanguages.AddSingleLine2(loc.Localize(availableLangs(i)), availableLangs(i))
    Next
    edtName.Text = name
    UpdateNameLabel
End Sub

Sub FlipLayout
    For Each v As B4XView In Root.GetAllViewsRecursive
        v.Left = v.Parent.Width - v.Left - v.Width
    Next
End Sub

Sub lstLanguages_ItemClick (Position As Int, Value As Object)
    loc.ForceLocale(Value)
    ResetLayout
End Sub

Sub edtName_TextChanged (Old As String, New As String)
    If New <> "" Then
        name = New
        UpdateNameLabel
    End If
End Sub

Sub UpdateNameLabel
    lblHello.Text = loc.LocalizeParams("Hello {1}!", Array(name))
End Sub
 

Attachments

  • Project.zip
    18.2 KB · Views: 142
Upvote 0
Solution

AnandGupta

Expert
Licensed User
Longtime User
Thanks @aeric , I did search "localize b4xpage" but did not get your thread.
Screenshot 2023-06-23 at 21-53-45 Search Results.png
 
Upvote 0
Top