B4J Question Best way to localize B4J applications

xulihang

Active Member
Licensed User
Hi there,

I am trying to translate my app's UI from Chinese to English and I am having trouble doing this with B4XLocalizator.

First, I don't know if there is an automation tool which exports text to be translated from layout files and the code.

I wrote a helper class to exports UI text from layout files:

B4X:
Sub ExportLayoutText
    Dim map1 As Map
    map1.Initialize
    Dim p As Pane
    p.Initialize("")
    Dim assets As String
    assets=File.Combine(File.GetFileParent(File.DirApp),"Files")
    If File.Exists(assets,"")=False Then
        Return
    End If
    For Each filename As String In File.ListFiles(assets)
        If filename.ToLowerCase.EndsWith(".bjl") Then
            p.RemoveAllNodes
            p.LoadLayout(Utils.GetFilenameWithoutExtension(filename))
            For Each node As Object In p.GetAllViewsRecursive
                Dim transUnit As Map
                transUnit.Initialize
                transUnit.Put("type",GetType(node))
                transUnit.Put("layout",filename)
                Dim n As Node=node
                If n Is TextArea Then
                    Dim ta As TextArea = n
                    transUnit.Put("text",ta.Text)
                Else If n Is TextField Then
                    Dim tf As TextField = n
                    transUnit.Put("text",tf.Text)
                Else if n Is Button Then
                    Dim btn As Button = n
                    transUnit.Put("text",btn.Text)
                Else if n Is CheckBox Then
                    Dim cbx As CheckBox = n
                    transUnit.Put("text",cbx.Text)
                Else If n Is RadioButton Then
                    Dim rbtn As RadioButton = n
                    transUnit.Put("text",rbtn.Text)
                Else If n Is ToggleButton Then
                    Dim tbtn As ToggleButton = n
                    transUnit.Put("text",tbtn.Text)
                Else If n Is Label Then
                    Dim lbl As Label = n
                    transUnit.Put("text",lbl.Text)
                End If
                If transUnit.ContainsKey("text") Then
                    Dim text As String=transUnit.Get("text")
                    map1.Put(text.ToLowerCase,transUnit)
                End If
            Next
        End If
    Next
    ExportMaptoXLSX(map1)
End Sub
As for text in code, I think I may need to handle them manually.

But for some methods, like fx.msgbox, the UI text is sure, which I think the source text can be filtered out with an automation tool.

Second, I found the default localizator does not localize text in menubar.

Third, java has its Localization class called java.util.Locale. Is it better to use this to do the localization job?
 

xulihang

Active Member
Licensed User
java.util.ResourceBundle seems the right class.

The usage is like Localizator: System.out.println(bundle.getString(hello"));
 
Top