Android Tutorial [B4X] Localizator - Localize your B4X applications

Localizator is a cross platform solution for strings localization.

The strings are defined in an Excel workbook.

SS-2016-07-07_17.00.16.png


They are then converted to a SQLite database file with a B4J program:

SS-2016-07-07_17.21.20.png


The database file should be added to the Files tab of your application.

Localizator is a class. You need to add it to your B4A, B4i or B4J program. It loads the set of strings based on the device locale or based on the forced locale (Localizator.ForceLocale).
Each string is identified by a key. The key matching is case insensitive. If there is no match then the key itself is returned (the value passed to Localize method).

There are some helper methods to automate the tasks. The main one is LocalizeLayout. It will go over all the views and will replace the matched keys.

Values can include parameters. For example "Hello {1}!". LocalizeParams deals with such values:
B4X:
lblHello.Text = loc.LocalizeParams("Hello {1}!", Array(edtName.Text))
{1} will be replaced with the first parameter from the array.

Notes

- In B4A it is recommended to declare and initialize the Localizator from the starter service.
- The three examples attached are a bit more complicated as they allow the user to change the language.
In its simplest form you just need to initialize the localizator and call loc.LocalizeLayout.
- Languages two letters codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

upload_2016-7-7_17-13-53.png
upload_2016-7-7_17-14-9.png
upload_2016-7-7_17-28-25.png


The compiled executable jar of the B4J converter can be downloaded from: www.b4x.com/b4j/files/B4XLocalizator.jar
The source code is attached (depends on XLUtils).

B4A, B4i and B4J examples are also attached.

Localizator v1.01 is attached.

Automating the localizator: https://www.b4x.com/android/forum/t...our-b4x-applications.68751/page-3#post-548169

B4XPages example: https://www.b4x.com/android/forum/threads/localization-example-in-b4xpages-how.148649/post-942204
 

Attachments

  • B4J_LocalizatorExample.zip
    5.7 KB · Views: 1,535
  • ExcelWorkbook.zip
    7.1 KB · Views: 1,974
  • B4i_LocalizatorExample.zip
    6.1 KB · Views: 1,224
  • Localizator.bas
    4.4 KB · Views: 1,946
  • B4A_LocalizatorExample.zip
    11.6 KB · Views: 1,500
  • B4J_LocalizatorConverterSource.zip
    10 KB · Views: 725
Last edited:

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Another silly question...

Can the xls file have empty rows? in long files it would help create separation between "topics"

[edit] tested with success!!!

You can use this code line from @Erel to add comment lines in the worksheet.

B4X:
If row.GetCell(0).ValueString.StartsWith("'") Then Continue

see above.
 

trueboss323

Active Member
Licensed User
Longtime User
When using the LocalizeLayout how do I know which keys are assigned to which? In the example, how does it determine Label1, Label2, Label3 equals one two three? Do the text of the labels have to be the same as the keys?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can see it in the code:
B4X:
Public Sub LocalizeLayout(PanelOrActivity As Panel)
   For Each v As View In PanelOrActivity.GetAllViewsRecursive
     If v Is Label Then 'this will catch all of Label subclasses which includes EditText, Button and others
       Dim lbl As Label = v
       lbl.Text = Localize(lbl.Text)
     End If
     If v Is EditText Then
       Dim et As EditText = v
       et.Hint = Localize(et.Hint)
     End If
   Next
End Sub

The text should match the keys.
 

trueboss323

Active Member
Licensed User
Longtime User
I'm having a strange problem Erel. My text isn't getting localize even when I put Starter.loc.LocalizeLayout. The strange thing is that when I put a breakpoint on that line , and then continue running, the text is localized. This doesn't happen when I don't put a breakpoint, like it ignores that line. Why does this happen?
 

trueboss323

Active Member
Licensed User
Longtime User
Here you go Erel. I tried testing this with only the French language. Only the 'days','hours', 'minutes' labels should be in French. And i put the LoadLayout line right at the end of the ActivityCreate sub.
 

Attachments

  • SmokingCalculator.zip
    136.4 KB · Views: 662

trueboss323

Active Member
Licensed User
Longtime User
That seemed to fix the problem, thank you Erel! How could I also do the same with Tabstrip? I don't see a method to get panels from it.
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Finally I took the time to test this translation method and I like it very much.

Some notes and tipps:
  • The precompild B4XLocalizator.jar did not work for me. It opens a complete white window. I compiled it myself and then it works well.
  • The heading in the first column MUST be "key" otherwise wrong translations are generated.
  • I use Google Docs SpreadSheets for translation. There is a really nice plugin called "Easy Translations". It uses the exact same format for the sheet and can automatically generate missing translations from Google Translator. This is really great.
    Additionally it is possible to share the document and restrict editing access to single language columns to other users. With this feature it is possible to give other translators access to just one language. So this can be used as a simple community translation service.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The precompild B4XLocalizator.jar did not work for me. It opens a complete white window. I compiled it myself and then it works well.
It is related to the UNIFIED theme that is not working properly in Windows 10 (it was removed in the last version of B4J). I've uploaded a new jar.
 

DavideV

Active Member
Licensed User
Longtime User
Hello,
i need to retrieve a list or array of iso codes present in the created database to let the user choose his preferred language from the app settings.
Unfortunately i'm not familiar with SQL so i don't know how to query them.

A new sub in the Localizator class that returns that values (isocodes) will be great :)

Thanks in advance
DavideV
 

DavideV

Active Member
Licensed User
Longtime User
1. You should create a new thread for your question as it has nothing to do with this thread.
2. Read the beginners guide and users guide. There are Database sections in it too

DonManfred,
1) my question is related to the Localizator class in this thread, why do i need to create a new thread?
2) will do, thanks for help
 

LucaMs

Expert
Licensed User
Longtime User
Hello,
i need to retrieve a list or array of iso codes present in the created database to let the user choose his preferred language from the app settings.
Unfortunately i'm not familiar with SQL so i don't know how to query them.

A new sub in the Localizator class that returns that values (isocodes) will be great :)

Thanks in advance
DavideV
It seems to me that the class does not contains such data and the db is created from an excel file that you should create.

You can get that data (and many other) using the AHLocale library.
 

DavideV

Active Member
Licensed User
Longtime User
It seems to me that the class does not contains such data and the db is created from an excel file that you should create.

You can get that data (and many other) using the AHLocale library.

Hi Luca,
maybe i'm not clear, i don't need the device locale but the isocodes present in the db created by the b4J utility by Erel (first post).
I think it should contains these info and maybe obtained by a query
 

LucaMs

Expert
Licensed User
Longtime User
Hi Luca,
maybe i'm not clear, i don't need the device locale but the isocodes present in the db created by the b4J utility by Erel (first post).
I think it should contains these info and maybe obtained by a query
I have never used this tool* but if I understand it it will convert your excel file into a SQLite db, so it will only contain the languages already included in the Excel file.
Maybe you just want this; in this case you could add this routine to the Localizator class:
B4X:
Public Sub GetLanguages As List
    Dim lstLanguages As List
    lstLanguages.Initialize
    Dim rs As ResultSet = sql.ExecQuery("SELECT lang FROM data GROUP BY lang")
    Do While rs.NextRow
        lstLanguages.Add(rs.GetString("lang"))
    Loop
    rs.Close
    Return lstLanguages
End Sub


* Probably because I like this one:
https://www.b4x.com/android/forum/threads/lmtranslationdbmanager.58660/
:)
 
Last edited:

DavideV

Active Member
Licensed User
Longtime User
I have never used this tool* but if I understand it it will convert your excel file into a SQLite db, so it will only contain the languages already included in the Excel file.
Maybe you just want this; in this case you could add this routine to the Localizator class:
B4X:
Public Sub GetLanguages As List
    Dim lstLanguages As List
    lstLanguages.Initialize
    Dim rs As ResultSet = sql.ExecQuery("SELECT lang FROM data GROUP BY lang")
    Do While rs.NextRow
        lstLanguages.Add(rs.GetString("lang"))
    Loop
    rs.Close
    Return lstLanguages
End Sub


* Probably because I like this one:
https://www.b4x.com/android/forum/threads/lmtranslationdbmanager.58660/
:)

Hi Luca,
it is exactly what i need, works perfect, useful answer :)

It is a pity i can't put more than one like ;)

Thanks
 
Top