Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Embed Translation Strings as a Resource

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embed Translation Strings as a Resource

    Following on from the discussion here: https://forum.powerbasic.com/forum/u...ion#post822964
    (Please put any discussion there, not here in the Source Code forum)

    One method of "internationalising" your application with a language file embedded as RCData.

    '
    Code:
    'Uses a translations file.
    'File is UTF-8 encoded, $CRLF delimited (you can even create it with Notepad)
    'Each line contains three Tab delmited values:
    'IDString, English wording, Translation
    
    #COMPILE EXE
    #DIM ALL
    #RESOURCE RCDATA,transdata,"Russian.txt"
    #INCLUDE ONCE "WIN32API.INC"
    ENUM ctrls SINGULAR
        IDC_btnAction= 1001
    END ENUM
    
    FUNCTION Translate(wsValue AS WSTRING) AS WSTRING
        LOCAL lngTrans,lngSearchLen AS LONG
        LOCAL wsTranslations AS WSTRING
        DIM trans() AS STATIC WSTRING
        IF NOT ARRAYATTR(trans(), 0) THEN
            wsTranslations = UTF8TOCHR$(RESOURCE$(RCDATA,"transdata"))
            lngTrans = PARSECOUNT(wsTranslations,$CRLF)
            REDIM trans(1 TO lngTrans)
            PARSE wsTranslations, trans(),$CRLF
       END IF
       lngSearchLen = LEN(wsValue)+1
       ARRAY SCAN trans() ,FROM 1 TO lngSearchLen, COLLATE UCASE, = wsValue & $TAB , TO lngTrans
       IF lngTrans > 0 THEN
           FUNCTION = PARSE$(trans(lngTrans),$TAB,3)
       END IF
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
       LOCAL lRslt AS LONG
       LOCAL hDlg  AS DWORD
        DIALOG NEW 0, Translate("CAPTION"), , , 200, 180, %WS_SYSMENU, TO hDlg
        CONTROL ADD BUTTON , hDlg,%IDC_btnAction,Translate("ACTION"),60,130,80,20
        DIALOG SHOW MODAL hDlg, CALL MainDlgCB TO lRslt
    END FUNCTION
    
    CALLBACK FUNCTION MainDlgCB()
         SELECT CASE AS LONG CB.MSG
             CASE %WM_COMMAND
                SELECT CASE AS LONG CB.CTL
                    CASE %IDC_btnAction
                        IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                            ? Translate("MSG1"),,Translate("MSGTITLE")
                        END IF
                END SELECT
        END SELECT
    END FUNCTION
    '


    Click image for larger version

Name:	Russian4.jpg
Views:	132
Size:	19.8 KB
ID:	822978 Click image for larger version

Name:	Russian5.jpg
Views:	120
Size:	112.1 KB
ID:	822979

  • #2
    An add on: a message box replacement that translated the button text.

    '
    Code:
    'Message box replacement than translates button text
    'Uses a translations file.
    'File is UTF-8 encoded, $CRLF delimited (you can even create it with Notepad)
    'Each line contains three Tab delmited values:
    'IDString, English wording, Translation
    
    #COMPILE EXE
    #DIM ALL
    #RESOURCE RCDATA,transdata,"Russian2.txt"
    FUNCTION PBMAIN() AS LONG
        LOCAL lRet AS LONG
        lret = TransMsgBox("MSG1",%MB_YESNO,"TITLE1")
        ? "Return value:" & STR$(lret)
            lret = TransMsgBox("MSG1",%MB_ABORTRETRYIGNORE,"TITLE1")
        ? "Return value:" & STR$(lret)
    END FUNCTION
    
    FUNCTION TransMsgBox(sText AS WSTRING,lButtons AS LONG,sTitle AS WSTRING) AS LONG
        LOCAL txtB1,txtB2,txtB3 AS WSTRING
        LOCAL lngButtons AS LONG
        LOCAL hDlg AS DWORD
        DIM Retval(1 TO 3) AS LONG
        '%MB_OK, %MB_OKCANCEL, %MB_ABORTRETRYIGNORE, %MB_YESNOCANCEL, %MB_YESNO, %MB_RETRYCANCEL, %MB_CANCELTRYCONTINUE
        '%IDOK, %IDCANCEL, %IDABORT, %IDRETRY, %IDIGNORE, %IDYES, %IDNO, %IDCLOSE, %IDHELP, %IDTRYAGAIN, %IDCONTINUE,
        SELECT CASE AS LONG lButtons
            CASE %MB_OK
                lngButtons = 1
                txtB1 = Translate("OK")
                retval(1) = %IDOK
            CASE %MB_OKCANCEL
                lngButtons = 2
                txtB1 = Translate("CANCEL")
                txtB2 = Translate("OK")
                retval(1) = %IDCANCEL
                retval(2) = %IDOK
    
            CASE %MB_ABORTRETRYIGNORE
                lngButtons = 3
                txtB1 = Translate("IGNORE")
                txtB2 = Translate("RETRY")
                txtB3 = Translate("ABORT")
                retval(1) = %IDIGNORE
                retval(2) = %IDRETRY
                retval(3) = %IDABORT
    
            CASE %MB_YESNOCANCEL
                lngButtons = 3
                txtB1 = Translate("CANCEL")
                txtB2 = Translate("NO")
                txtB3 = Translate("YES")
                retval(1) = %IDCANCEL
                retval(2) = %IDNO
                retval(3) = %IDYES
    
            CASE %MB_YESNO
                lngButtons = 2
                txtB1 = Translate("NO")
                txtB2 = Translate("YES")
                retval(1) = %IDNO
                retval(2) = %IDYES
    
            CASE %MB_RETRYCANCEL
                lngButtons = 2
                txtB1 = Translate("CANCEL")
                txtB2 = Translate("RETRY")
                retval(1) = %IDCANCEL
                retval(2) = %IDRETRY
    
            CASE %MB_CANCELTRYCONTINUE
                lngButtons = 3
                txtB1 = Translate("CONTINUE")
                txtB2 = Translate("RETRY")
                txtB3 = Translate("CANCEL")
                retval(1) = %IDCONTINUE
                retval(2) = %IDRETRY
                retval(3) = %IDCANCEL
    
         END SELECT
         LOCAL dlgX AS LONG, dlgY, lRet AS LONG
         dlgX = 240: dlgY = 100
         DIALOG DEFAULT FONT "Arial", 9
         DIALOG NEW 0,translate(sTitle),,,dlgX,dlgY TO hDlg
             CONTROL ADD BUTTON ,hDlg,1001,txtB1,dlgX - 70,dlgY - 30,64,16
             IF lngButtons > 1 THEN
                 CONTROL ADD BUTTON ,hDlg,1002,txtB2,dlgX - 140,dlgY - 30,64,16
             END IF
             IF lngButtons =3 THEN
                 CONTROL ADD BUTTON, hDlg,1003,txtB3,dlgX - 210,dlgY - 30,64,16
             END IF
             CONTROL ADD LABEL ,hdlg,1004, translate(sText),5,5,dlgX -20,dlgY - 50
          DIALOG SHOW MODAL hDlg, CALL TransMsgCB TO lRet
          FUNCTION = retval(lRet)
    END FUNCTION
    
    CALLBACK FUNCTION TransMsgCB()
          SELECT CASE CB.MSG
              CASE %WM_COMMAND
                   SELECT CASE CB.CTL
                       CASE 1001
                           DIALOG END CB.HNDL ,1
                       CASE 1002
                           DIALOG END CB.HNDL ,2
                       CASE 1003
                           DIALOG END CB.HNDL ,3
                  END SELECT
          END SELECT
    END FUNCTION
    
    FUNCTION Translate(wsValue AS WSTRING) AS WSTRING
        LOCAL lngTrans,lngSearchLen AS LONG
        LOCAL wsTranslations AS WSTRING
        DIM trans() AS STATIC WSTRING
        IF NOT ARRAYATTR(trans(), 0) THEN
            wsTranslations = UTF8TOCHR$(RESOURCE$(RCDATA,"transdata"))
            lngTrans = PARSECOUNT(wsTranslations,$CRLF)
            REDIM trans(1 TO lngTrans)
            PARSE wsTranslations, trans(),$CRLF
       END IF
       lngSearchLen = LEN(wsValue)+1
       ARRAY SCAN trans() ,FROM 1 TO lngSearchLen, COLLATE UCASE, = wsValue & $TAB , TO lngTrans
       IF lngTrans > 0 THEN
           FUNCTION = PARSE$(trans(lngTrans),$TAB,3)
       END IF
    END FUNCTION
    '


    Click image for larger version

Name:	RussianMB.jpg
Views:	98
Size:	41.6 KB
ID:	823006

    The new translation file is attached.
    Attached Files

    Comment

    Working...
    X