Hello,
What is the recommented way to built Multi Language Support to my app ?
I am thinking to make text files for each language eg
English.txt
Greek.txt
Italian.txt
and inside each file the contents would be something like this :
OK=OK
Cancel=Cancel
Retry=Retry
Are there any objects or library that I should use to do it,
or is there a more efficient way ?
If you want to allow the user to select another language that is not the same of the local device, you can create a SQLite DB with a table of literals (fields Name Resource Language), with the records in several languages. When you start the activity or change the language setting, you must to call a Sub to assign these literals to the buttons, labels, etc. ..
B4X:
Sub Literals
lblBanner.Text =GetString("Words","Words")
End sub
Sub GetString(vName As String, vDefault As String ) As String
Dim mstrQuery As String
Try
Dim mstrResource As String
mstrQuery="Select Resource from Strings where Languaje ='" & strLanguaje & "' and Name='" & vName & "'"
mstrResource = SQL.ExecQuerySingleResult( mstrQuery)
If mstrResource = Null Then
mstrResource= vDefault
End If
Return mstrResource
Catch
Return vDefault
Log("GetString " & mstrQuery & " vDefault="& vDefault & " Error: " & LastException.Message )
End Try
End Sub
I used a value by default, because the first time attempt to put the verbatim with the language of the phone
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Base")
strLanguaje =GetDefaultLanguage
Literals
end sub
Sub GetDefaultLanguage As String
Dim r As Reflector
r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
DefaultLanguaje=r.RunMethod("getLanguage")
Return r.RunMethod("getLanguage")
End Sub
Sub GetDefaultLanguage As String
Dim r As Reflector
r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
DefaultLanguaje=r.RunMethod("getLanguage")
Return r.RunMethod("getLanguage")
End Sub
Hi quimacama,
Excuse me please, In sub GetDefaultLanguage As String,why don't you write only these below
B4X:
Sub GetDefaultLanguage As String
Dim r As Reflector
Return r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
End Sub
P.S. I'm studying about this,please help me understand this(refer to this).
:sign0013:
You're right, in the method there are too many lines. This is the correct code:
B4X:
Sub GetDefaultLanguage As String
Dim r As Reflector
r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
Return r.RunMethod("getLanguage")
End Sub
I'm using this approach. You can store the name of the chosen file somewhere, for e.g. in a database or another file. Then, what you have to do is retrieve the text file, split its rows according to a special string (I'm using the map's one '->') and create a map. Then get the text of a specific key, by using map.get(key).