B4J Code Snippet [B4X] [B4XPages] Localizator

I can't find an example of using Localizator in B4XPages so I tried to make it work in B4XPages with a simple example.

1662539661661.png
1662539695377.png
1662540263983.png


Main module:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public Loc As Localizator
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Loc.Initialize(File.DirAssets, "strings.db")
    MainForm = Form1
    MainForm.Show
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(MainForm)
End Sub

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private B4XPage2 As Page2
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    LocalizePage ' apply Localization
   
    B4XPage2.Initialize
    B4XPages.AddPage("Page2", B4XPage2)
End Sub

Sub LocalizePage
    #If B4A
    Starter.Loc.ForceLocale("zh")
    Log("Localizing MainPage...")
    Starter.Loc.LocalizeLayout(Root)
    #Else
    Main.Loc.ForceLocale("zh")
    Log("Localizing MainPage...")
    Main.Loc.LocalizeLayout(Root)
    #End If
End Sub

Private Sub Button1_Click
    B4XPages.ShowPage("Page2")
End Sub

Page2 class:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private Label1 As Label
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    LoadLayouts ' use a sub to load layout
End Sub

Sub LoadLayouts
    Root.LoadLayout("Page2")
    Log("Localizing Page2...")
    #If B4A
    Starter.Loc.LocalizeLayout(Root)
    Label1.Text = Starter.Loc.LocalizeParams(Label1.Text, Array(2))
    #Else
    Main.Loc.LocalizeLayout(Root)
    Label1.Text = Main.Loc.LocalizeParams(Label1.Text, Array(2))
    #End If
    B4XPages.SetTitle(Me, Label1.Text)
End Sub
 

Attachments

  • B4XPagesLocalizator.zip
    20.2 KB · Views: 288
Top