Android Code Snippet ResourceManager - load multiple string values named in an array

I wanted to ensure that I had an easy way to localise my app, which means storing all user-visible text in a resource file and looking up the text from the file when the app is run. That way, the external file can be translated and stored as a localized resource file.

I came across the work that warwound did a while ago (https://www.b4x.com/android/forum/t...ing-androidresources.19165/page-2#post-246538) and wanted to give that work the thumbs up! Makes it very easy to get hold of the resource strings - and other resources. And it makes it possible to get hold of localized strings for "quantity" items (zero ducks, one duck, two ducks etc.) as string arrays

I also wanted to share the following addition that I'm using, which makes it simple to get multiple resource values at once, with "Not Found: resource-name" returned when the resource name isn't found.
B4X:
'    returns the application string resources identified in the TagValues array
'    if no string resource is found then the DefaultValue is returned
Sub GetApplicationStrings(TagValues() As String, DefaultValue As String, bAppendTagValue As Boolean) As Map
    Dim StringValues As Map: StringValues.Initialize
    For Each TagValue As String In TagValues
        Dim ResourceValue As String = DefaultValue
        If bAppendTagValue Then ResourceValue = ResourceValue & TagValue
        ResourceValue = GetApplicationString(TagValue, ResourceValue)
        StringValues.Put(TagValue, ResourceValue)
    Next
    Return StringValues
End Sub

In my app I can now define consts for the resource names and then make a simple call to warwound's RescourceManager module.
B4X:
    Dim const sRES_NAME As String = "Sample_BeepTest_Name"
    Dim const sRES_DESC As String = "Sample_BeepTest_Desc"
    Dim oResStrings As Map = ResourceManager.GetApplicationStrings(Array As String(sRES_NAME, sRES_DESC), "Not found: ", True)

Enjoy :)
 
Top