iOS Question Localization

jo1234

Member
Licensed User
The AHLocale library is ideal for localization.
Unfortunately, it is not available for B4i.

Maybe someone finds the time to port it?
 

mrossen

Active Member
Licensed User
Hi Again,

I have tried to make this to Work

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim Sprog As Map
   
End Sub

Sub File_init(Lf As String)
    Sprog.Initialize
    If File.Exists(File.DirAssets, Lf) Then
        Sprog = File.ReadMap(File.DirAssets,Lf)
    Else
        Log("Language file : " & Lf & " missing.")
    End If
End Sub

'
'    If there isnt any Translation the original is keept
'
Sub ObjTo(v As View)
       
    If v Is Label Then
            Dim l As Label
            l = v
            If Sprog.ContainsKey(l.Tag) Then l.Text = Sprog.Get(l.Tag)       
    End If

End Sub

Sub ThisPanel(v As View)
    Dim p As Panel
    p = v
    If p.NumberOfViews > 0 Then
        For i = 0 To p.NumberOfViews-1
            If p.GetView(i) Is Panel Then
            Log(p.GetView(i))
                ThisPanel(p.GetView(i))
            Else
                ObjTo(p.GetView(i))
            End If
        Next
    End If
End Sub

Sub ThisActivity(P As Page)
    If P.RootPanel.NumberOfViews > 0 Then
        For i = 0 To P.RootPanel.NumberOfViews-1
            If P.RootPanel.GetView(i) Is Panel Then
                ThisPanel(P.RootPanel.GetView(i))
            Else
                ObjTo(P.RootPanel.GetView(i))
            End If
        Next
    End If
End Sub
But it seems like if the view i other than "Panel" it still runs "ThisPanel" sub

Anyone there can see the problem,

Mogens
 

Filippo

Expert
Licensed User
Hi mrossen,

I use this module, maybe you can also do it.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim mpLang As Map
    Dim Language As String
End Sub

Sub InitLanguage
    Dim Lang As String
    Dim lst As List
    Dim str() As String
    Dim tmp As String
   
    mpLang.Initialize
   
    Log("Sprache=" & GetPreferredLanguage)
    Select Case GetPreferredLanguage
        Case "de"
            Language = "de_"
            Lang="lang_de.txt"
        Case "it"
            Language = "it_"
            Lang="lang_it.txt"
        Case Else
            Language = "en_"
            Lang="lang_en.txt"
    End Select

    lst=File.ReadList(File.DirAssets, Lang)
    For i = 0 To lst.Size - 1
        tmp = lst.Get(i)
        str = Regex.Split("=", tmp)
        If str.Length = 2 Then
            mpLang.Put(str(0),str(1))
        Else If str.Length > 2 Then
            mpLang.Put(str(0), tmp.SubString(tmp.IndexOf("=") + 1))
        End If
    Next
End Sub

Sub getStr(Key As String) As String
    Dim ret As StringBuilder
    ret.Initialize
    Dim str() As String=Regex.Split("\\n\ ", mpLang.Get(Key))
    For i = 0 To str.Length - 1
        ret.Append(str(i))
        If i < str.Length - 1 Then ret.Append(CRLF)
    Next
'    Log("Key= " & Key & " ;" & ret.ToString)
    Return ret.ToString
End Sub

'NSString * myString = [[NSLocale preferredlanguage]objectAtIndex:0];
Sub GetPreferredLanguage As String
    Dim no As NativeObject
    Return no.Initialize("NSLocale") _
        .RunMethod("preferredLanguages", Null).RunMethod("objectAtIndex:", Array(0)).AsString
End Sub
lang_de.txt
[de]
strJa=Ja
strNein=Nein
lang_en.txt
[en]
strJa=Yes
strNein=No
lang_it.txt
[it]
strJa=Sì
strNein=No
 

mrossen

Active Member
Licensed User
Hi Fillipo,

How are you dealing with non UTF char.

Here in Denmark we have ÆØÅ

The code module break Down trying to load them

Mogens
 

Filippo

Expert
Licensed User
Hi Fillipo,

How are you dealing with non UTF char.

Here in Denmark we have ÆØÅ

The code module break Down trying to load them

Mogens
Can you upload a test file?
 

mrossen

Active Member
Licensed User
Hi Fillipo,

Thank you for your help

I think I need to tell "file.readlist" that the file is not UTF but Latin, but I don't know how.

Here is a testfile with the 6 Danish extra chars

Mogens
 

Attachments

Filippo

Expert
Licensed User
Hi mrossen,

you must save the text file with UTF8 code, then it works.
 

Attachments

mrossen

Active Member
Licensed User
Hi Fillipo,

Thank you for your help.

But it still not solve my problem with changing the label's text.

You code only reads the different language into a list, right?

How do you go througth all the view's and change the text?

I am still trying to get the code above til Works but it breaks Down when try to change Label's text


Mogens
 

Filippo

Expert
Licensed User
Have you tried my example?
 

mrossen

Active Member
Licensed User
Sorry about that,

I just used the txt file in my own project not your test example.

But I need to make a singe line for every textfield for the translate, and I have many line to translate.

Therefore I would prefere to have a id in the textfield tag an use that to match the translation in the language file.

So If any know know to loop though all views and match the tag field I swould be happy,

Thanks for you help.

I post if I get a solution

Mogens
 
Top