460 views
in GUI Development by
Hello,

is it possible to get a localized string from the "Strings" unit at runtime by its name?

In my case I have a bunch of localized error messages. The name of all these strings is "Error_0", "Error_1", ... , "Error_xx". In my code I'm getting an error id and I have to find the corresponding error message. How can I access the "Strings" unit and search a string by name?

Thank you and best regards
Christian Roessler

1 Answer

+1 vote
by
 
Best answer

Hi Christian,

the concrete string ids only exist in the Embedded Wizard project.

If you are using the excel string export, you could extend the vba script that creates the string-unit. Besides the strings in the string.ewu, you could create also a string.h that contains a corresponding enum to a string. In addition, add a getStringByEnum-method to the string.ewu.
At the end, if I want to send a string from the middleware to the GUI, you just need to use the enum from the included string.h and convert it in the GUI via the mentioned method to a real string.
In my opinion, this is an elegant way to avoid creating and transfering strings.

How the vba could looks like:

The last lines of the existing ExportStrings()-method:

   ' build string map
   ExportStringsMap
    
   ' Close the open file
   Close #1
   End With
   
   ExportStringsHeader
   
End Sub

 

The ExportStringMap()-method that creates a class and the GetStringByEnum-function to the strings unit:

Sub ExportStringsMap()

    Print #1, ""
    Print #1, "$rect <620, 10, 820, 50>"
    Print #1, "$output false"
    Print #1, "class MessageMapClass"
    Print #1, "{"
    Print #1, "  $rect < 0, 0, 400, 40 >"
    Print #1, "  method string GetStringById( arg int32 aId )"
    Print #1, "  {"
    Print #1, "    switch( aId )"
    Print #1, "    {"
       
    ' Now define the table row for the headers
    Dim noOfIds As Integer
    Dim table As Excel.Worksheet
    Set table = Excel.Worksheets("StringTable")
    Set generator = Excel.Worksheets("Generator")
    
    ' Define location of the used cells within the Excel sheet
    noOfIds = generator.Cells(6, 3)
    
    Dim I As Integer
    I = 0
    
    For strID = 2 To noOfIds + 1
        If table.Cells(strID, 1) <> "" Then
        
            ' Print the lines
            Print #1, "      case "; I; ": return Strings::"; table.Cells(strID, 1); ";"
            
            I = I + 1
        
        End If
    Next strID
    
    Print #1, "      default: return """";"
    Print #1, "    }"
    Print #1, "  }"
    Print #1, "}"
    Print #1, ""
    Print #1, "$rect <620,50,820,90>"
    Print #1, "$output false"
    Print #1, "autoobject Strings::MessageMapClass MessageMap;"

End Sub

 

The ExportStringsHeader()-method that creates the ansi c header file:

Sub ExportStringsHeader()

    ' Stores the filename provide by the user
    Dim strFileName As String
    
    ' Now define the table row for the headers
    Dim headerRow As Integer
    Dim noOfLangugaes As Integer
    Dim noOfIds As Integer
    Dim table As Excel.Worksheet
    Dim name As String
   
    ' Working on the active sheet.
    With Excel.ActiveSheet
   
    ' Change to the Excel files folder for the save dialog
    ChDir Application.ActiveWorkbook.Path
    
    ' Get the filename, remove the XLS extention and add EWU extention
    name = Application.ActiveWorkbook.name
    name = Left(name, Len(name) - 4)
    name = name + "Map.h"
    
    'Display the file selection dialog
    strFileName = Application.GetSaveAsFilename(name, _
        fileFilter:="Embedded Wizard Unit (*.h), *.h")

    'Check if the user has pressed Cancel (Inputbox returns a zero length string)
    b = VarType(strFileName)
    If strFileName = "" Or strFileName = "False" Or strFileName = "Falsch" Then
        Exit Sub
    End If
    
    ' Do some good housekeeping and check for the existence of the file.
    ' Ask the user for further directions in case it does. : )

    If Dir(strFileName) <> "" Then
        If MsgBox(strFileName & " already exists. Overwrite it?", _
                    vbQuestion + vbYesNo, "Warning") = vbNo Then
            Exit Sub
        End If
    End If


    Open strFileName For Output As #1
    
    Print #1, "// This file is generated automatically."
    Print #1, "// Source:    " & Excel.ActiveWindow.Caption
    Print #1, "// Generated: " & Date & " " & Time
    Print #1, "// Excel:     Version " & Application.Version & " on " & Application.OperatingSystem & "."
    Print #1, ""
    Print #1, ""
    Print #1, "#ifndef _EMWI_STRINGS_H_"
    Print #1, "#define _EMWI_STRINGS_H_"
    Print #1, ""
    Print #1, "enum ENUM_EMWI_STRING_ID"
    Print #1, "{"

    
    Set table = Excel.Worksheets("StringTable")
    Set generator = Excel.Worksheets("Generator")
    
    ' Define location of the used cells within the Excel sheet
    headerRow = generator.Cells(4, 3)
    noOfLanguages = generator.Cells(5, 3)
    noOfIds = generator.Cells(6, 3)
    
    Dim I As Integer
    Dim x() As Byte
    Dim text As String
    Dim textSub As String
    Dim textLength As Integer
    Dim textPos As Integer
    Dim charVal As Integer
    
    I = 0
    
    For strID = 2 To noOfIds + 1
        If table.Cells(strID, 1) <> "" Then
        
            ' Print the lines
            If strID < noOfIds Then
                Print #1, "  "; table.Cells(strID, 1); " = " & I; ","
            Else
                Print #1, "  "; table.Cells(strID, 1); " = " & I
            End If
            
            I = I + 1
        
        End If
    Next strID
    
    Print #1, "}"
    
   ' Close the open file
   Close #1
   End With
End Sub

Perhaps you need some adjustments (e.g. in ExportStringMap I hardcoded "Strings::"). 
This code snippets should work, but you should also clean the code a little bit. I made this quickly, without optimizing it. 

Hope this helps!

Best regards,
Chris

by
Hi Christoph,

thank you very much. This is exactly what I was looking for.

 

Best regards,

Christian

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...