PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: CWindow and multiple languages (Localization)  (Read 1365 times)

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8641
  • Windows 10
    • PlanetSquires Software
CWindow and multiple languages (Localization)
« on: June 03, 2016, 11:20:48 AM »

Hi Jose,

Just throwing this out there for consideration.

As I am developing my application using CWindow it is great to know that the application will be fully Unicode aware. However during development it would be nice to have a generic way to to be able to handle multiple languages for the application. For example, the user would be able to load French, Spanish, German, English, etc files and the application (or at least the language used for GUI text elements) would then be localized to that language.

With Firefly, I attempted to implement such a thing in version 3.77. If CWindow had some helper classes built into it to allow a hash table that gets populated with various languages then it would be useful to other developers for localizing languages. I know that DLL's could also be used.
http://www.freebasic.net/forum/viewtopic.php?f=8&t=15796&start=345#p211559

Something to think about.  :-)
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

Josť Roca

  • Moderator
  • Master Member
  • *****
  • Posts: 3111
    • Jos
Re: CWindow and multiple languages (Localization)
« Reply #1 on: June 03, 2016, 01:25:37 PM »

The Dictionary object could be used for that purpose. It implements an associative array with key/value pairs. Both the key and the value can be any type, since they are variants, and, of course, the strings are unicode BSTRs. The .lang files will have to be saved as unicode, if you want to support languages that don't use the Latin alphabet.
« Last Edit: June 03, 2016, 01:31:01 PM by Jose Roca »
Logged

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8641
  • Windows 10
    • PlanetSquires Software
Re: CWindow and multiple languages (Localization)
« Reply #2 on: June 26, 2016, 02:49:49 PM »

For the WinFBE editor I have decided to take an extremely simple approach to localization.

I have defined a global dynamic wstring array that gets resized and filled on program startup or when the user selects a localization file from "Environment Options". I also define a simple macro to access the array.

Code: [Select]
' Create a dynamic array that will hold all localization words/phrases. This
' array is resized and loaded using the LoadLocalizationFile function.
ReDim Shared LL(Any) As WString * MAX_PATH

' Define a macro that allows the user to specify the LL array subscript and
' also a descriptive label (that is ignored), and return the LL array value.
#Define L(e,s)  LL(e)

The format of a localization file is UTF16 unicode and looks like the following:
Code: [Select]
' WinFBE localization file for ENGLISH language
'
' The first element to exist must be called "MAXIMUM" and needs to be set to
' the value of the last key in this file.
'
' This file should be created and saved using UTF-16 encoding (unicode).
'
' Each line represents a key/value pair describing the position to store the
' localized word/phrase into the localization array used by winFBE. Simple.

MAXIMUM:13

00000:&OK
00001:&Cancel
00002:&File
00003:&New
00004:&Open...
00005:&Close
00006:C&lose All
00007:&Save
00008:Save &As...
00009:Sa&ve All
00010:&Recent Files
00011:(Empty)
00012:Co&mmand Prompt...
00013:E&xit

When creating controls or needing to use a localized word or phrase you would do like the following:
Code: [Select]
   pWindow->AddControl("BUTTON", , IDC_FRMOPTIONS_CMDOK, L(0,"&OK"), 372, 367, 74, 28, _
        WS_CHILD Or WS_VISIBLE Or WS_TABSTOP Or BS_TEXT Or BS_DEFPUSHBUTTON Or BS_NOTIFY Or BS_CENTER Or BS_VCENTER, _
        WS_EX_LEFT Or WS_EX_LTRREADING)
   
   pWindow->AddControl("BUTTON", , IDC_FRMOPTIONS_CMDCANCEL, L(1,"&Cancel"), 460, 367, 74, 28, _
        WS_CHILD Or WS_VISIBLE Or WS_TABSTOP Or BS_TEXT Or BS_PUSHBUTTON Or BS_NOTIFY Or BS_CENTER Or BS_VCENTER, _
        WS_EX_LEFT Or WS_EX_LTRREADING)

   hMenu = CreateMenu
   hPopUpMenu = CreatePopupMenu
      AppendMenuW hMenu, MF_POPUP Or MF_ENABLED, Cast(UINT_PTR, hPopUpMenu), L(2,"&File")
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILENEW, L(3,"&New") & Chr(9) & "Ctrl+N"
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILEOPEN, L(4,"&Open...") & Chr(9) & "Ctrl+O"
         AppendMenuW hPopUpMenu, MF_SEPARATOR, 0, ""
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILECLOSE, L(5,"&Close") & Chr(9) & "Ctrl+Q"
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILECLOSEALL, L(6,"C&lose All")
         AppendMenuW hPopUpMenu, MF_SEPARATOR, 0, ""
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILESAVE, L(7,"&Save") & Chr(9) & "Ctrl+S"
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILESAVEAS, L(8,"Save &As") & Chr(9) & "F12"
         AppendMenuW hPopUpMenu, MF_ENABLED, IDM_FILESAVEALL, L(9,"Sa&ve All") & Chr(9) & "Ctrl+Shift+S"


The programmer just needs to ensure that the L() macro accesses a valid within bounds element in the LL() array. This means that care should be taken when creating the localization files (*.lang).

Simple and easy.

Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8641
  • Windows 10
    • PlanetSquires Software
Re: CWindow and multiple languages (Localization)
« Reply #3 on: June 26, 2016, 03:03:54 PM »

I can use the following macro to prevent out of bounds errors:

#Define L(e,s)  Iif( e >= LBound(LL) AndAlso e <= Ubound(LL), LL(e), WStr(""))

Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor