Is the standard Android-Localisation - Feature possible?

RiverRaid

Active Member
Licensed User
Longtime User
Hi!

For the time, i use the ATHtranslator - Lib, which works fine, but its not possible (at least i don't know how) to use the generated files with GeoLocalization .. so I'd like tu use the standard-android translationfeature
(as Described here) which is, in my opinion, quite nice ;)
Is there any possibility to do this with B4A?

Thanks ;)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes. You can use this code to get string resources (requires Reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetResourceString("app_name"))
End Sub

Sub GetResourceString(Key As String) As String
    Dim r As Reflector
    Dim package As String
    Dim id As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    id = r.GetStaticField(package & ".R$string", Key)
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    Return r.RunMethod2("getText", id, "java.lang.int")
End Sub

1. Make sure to set the xml files to be read-only. Otherwise they will be deleted during compilation.
2. Click on Tools - Clean project, after you add or remove xml files.
 
Upvote 0

RiverRaid

Active Member
Licensed User
Longtime User
Are there Plans to make that easyer?
F.i. If you type in the Text - Property of an Label in the Designer something like "@string/thisismystring" that the right string it will be automaticly used? As far as I know (may be wrong!) it should work in an xml - file that way:
B4X:
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/thisismystring" />
Or am I completely wrong?
Regards,
Andi
 
Upvote 0

RiverRaid

Active Member
Licensed User
Longtime User
Ok..

Thanks for your answer :)
Then I'm writing it in a Module. If I'm done, I will share a small sample project with the module :)

Regards
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Please, a bit more prompt how to use Sub GetResourceString(Key As String) As String.
Where .XML files should be ?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
And sub-folders like res\values\strings-ru.xml will work also for localization?
 
Upvote 0
Top