Localization of the system menu on Mac - how?

Just starting out? Need help? Post your questions and find answers here.
Giansauna
New User
New User
Posts: 4
Joined: Thu Jan 29, 2015 7:59 am

Localization of the system menu on Mac - how?

Post by Giansauna »

Hi,
it seems that PB does not offer complete support of localized system dialogs other than english out of the box, is that right? At least, I wasn't able to find a way to have MessageRequester() and the OS X system menu translated into german. And even the PB GUI itself (5.31, 64-bit) has a system menu in english on non-english Macs, while all other menu options are properly translated.
MessageRequester() ist not a major problem; it can be replaced with an own built requester window, but I did not manage to have translated the Mac system menu properly (the one just right of the apple) in my own programs. It always stays in english, even if I change the 'CFBundleDevelopmentRegion' key in the Info.plist file to 'German', which at least does the localization of some of the system dialogs (printer, save, load).
Could it possibly be achieved with some Cocoa API calls? And if yes: how?
Best regards, Gian-Reto.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Localization of the system menu on Mac - how?

Post by Shardik »

Giansauna wrote:Hi,
it seems that PB does not offer complete support of localized system dialogs other than english out of the box, is that right? At least, I wasn't able to find a way to have MessageRequester() and the OS X system menu translated into german.
Unfortunately that's right.

For code examples demonstrating how to display localized message requesters you may take a look into this thread with one example using API functions and another one using only native PB commands:
http://www.purebasic.fr/english/viewtop ... 24&t=48186

In order to display requesters with custom icons you may take a look into this code example:
http://www.purebasic.fr/english/viewtop ... &start=101

The following code example demonstrates how to display a German app menu. It removes all separators and all titles from the app menu with the exception of "About", "Preferences" and "Quit" and translates them into German. It also enables "About" and "Preferences" which are disabled by default. I have tested the code example successfully on MacOS X 10.6.8 (Snow Leopard) and MacOS X 10.9.5 (Mavericks) with PB 5.31 x86 and x64 in both ASCII and Unicode mode.

Code: Select all

EnableExplicit

Procedure ChangeAppMenuTitlesToGerman()
  Protected AppMenu.I
  Protected AppSubmenu.I
  Protected i.I
  Protected MainMenu.I
  Protected NewSubmenuTitle.S
  Protected NumItems.I
  Protected SharedApplication.I
  Protected SubmenuItem.I
  Protected SubmenuTitle.S
  
  SharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")
  MainMenu = CocoaMessage(0, SharedApplication, "mainMenu")
  AppMenu = CocoaMessage(0, MainMenu, "itemAtIndex:", 0)
  AppSubmenu = CocoaMessage(0, AppMenu, "submenu")
  NumItems = CocoaMessage(0, AppSubmenu, "numberOfItems")
  
  For i = NumItems - 1 To 0 Step -1
    SubmenuItem = CocoaMessage(0, AppSubmenu, "itemAtIndex:", i)
    
    If CocoaMessage(0, SubmenuItem, "isSeparatorItem")
      ; ----- Remove every separator from app menu
      CocoaMessage(0, AppSubmenu, "removeItem:", SubmenuItem)
    Else
      SubmenuTitle = PeekS(CocoaMessage(0, CocoaMessage(0, SubmenuItem, "title"),
        "UTF8String"), -1, #PB_UTF8)

      ; ----- Translate "About", "Preferences" and "Quit" into German
      If Left(SubmenuTitle, 5) = "About"
        NewSubmenuTitle = "Über " + Mid(SubmenuTitle, 7)
        CocoaMessage(0, SubmenuItem, "setTitle:$", @NewSubmenuTitle)
      ElseIf Left(SubmenuTitle, 11) = "Preferences"
        NewSubmenuTitle = "Voreinstellungen..."
        CocoaMessage(0, SubmenuItem, "setTitle:$", @NewSubmenuTitle)
      ElseIf Left(SubmenuTitle, 4) = "Quit"
        NewSubmenuTitle = "Beende " + Mid(SubmenuTitle, 6)
        CocoaMessage(0, SubmenuItem, "setTitle:$", @NewSubmenuTitle)
      Else
        ; ----- Remove all other items from app menu
        CocoaMessage(0, AppSubmenu, "removeItem:", SubmenuItem)
      EndIf
    EndIf
  Next i
EndProcedure

OpenWindow(0, 200, 200, 300, 70, "With localized German app menu")
CreateMenu(0, WindowID(0))

; ----- Enable app menu titles 'About' and 'Preferences'
;       (Unfortunately overwriting the english titles doesn't work!)
MenuItem(#PB_Menu_About, "")
MenuItem(#PB_Menu_Preferences, "")

ChangeAppMenuTitlesToGerman()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_About
          MessageRequester("About", "Demo with localized German app menu")
        Case #PB_Menu_Preferences
          MessageRequester("Preferences",
            "Here you should display your preferences window!")
        Case #PB_Menu_Quit
          If MessageRequester("Exit confirmation",
            "Do you want to quit this app?",
            #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
            Break
          EndIf
      EndSelect
  EndSelect
ForEver
Post Reply