Recent

Author Topic: [SOLVED] HowTo: MacOS AppMenu multilingual?  (Read 1305 times)

wittbo

  • Full Member
  • ***
  • Posts: 150
[SOLVED] HowTo: MacOS AppMenu multilingual?
« on: November 18, 2019, 01:16:25 pm »
As described on this platform, I added a MacOS specific "Apple" menu as first menu in the menu bar. Everything works as expected, but: those items, which are added automatically (s. screenshot) are always in english language. Does anyone know, how to get them multilingual, i.e. german?

« Last Edit: November 20, 2019, 08:21:30 pm by wittbo »
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: HowTo: MacOS AppMenu multilingual?
« Reply #1 on: November 18, 2019, 05:48:52 pm »
There was a request to add support for resource strings in Application menu.

I did insist though that macOS would automatically translate menus (which it seemed to be doing for Carbon).
After some research, the automatic translation turned out not to be the truth.

so the plan is to make captions as a resource strings for Cocoa.
This will leave an option for a developer to control the strings.

Translations are available here: https://wiki.freepascal.org/macOS_Translation

wittbo

  • Full Member
  • ***
  • Posts: 150
Re: HowTo: MacOS AppMenu multilingual?
« Reply #2 on: November 19, 2019, 10:40:46 am »
OK, but how can I tell the application to use the new captions?
I cannot find any way to access those strings.
The application itself only has knowledge about the self added items (about..., separator, preferences...); mnuApp.Count = 3.
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: HowTo: MacOS AppMenu multilingual?
« Reply #3 on: November 19, 2019, 02:56:57 pm »
currently the only way to access the menu is via Objective-P interface
Code: Pascal  [Select][+][-]
  1. {$ifdef LCLCocoa}
  2. {$modeswitch objectivec2}
  3. {$endif}
  4.  
  5. uses ...
  6. {$ifdef LCLCocoa}
  7.   CocoaInt
  8. {$endif}
  9.  
  10. {$ifdef LCLCocoa}
  11.   var
  12.     main : NSMenu;
  13.     app  : NSMenu;
  14.     i    : integer;
  15.     mn   : NSMenuItem;
  16.   ...
  17.     main := NSApplication(NSApp).mainmenu;
  18.     app := NSMenuItem(main.itemAtIndex(0)).menu;
  19.     if (app.numberOfItems>0) then
  20.        for i:=0 to app.numberOfItems do begin
  21.          mn :=main.itemAtIndex(i);
  22.          if (mn.Title.UTF8String = 'Quit') then
  23.            mn.setTitle( NSString.stringWithUTF8string('Exit'));
  24.        end;
  25.    
  26. {$endif}
  27.  
  28.  

wittbo

  • Full Member
  • ***
  • Posts: 150
Re: [SOLVED] HowTo: MacOS AppMenu multilingual?
« Reply #4 on: November 20, 2019, 08:33:55 pm »
Thank you very much for your hints. Usage of direct Cocoa calls was new for me, so I needed some time to check what happens and I had to apply some minor changes. Below you will find my procedure, which translates the standard Apple menuitems (English) to their German counterparts.

Code: Pascal  [Select][+][-]
  1. unit...
  2. {$ifdef LCLCocoa}   {$modeswitch objectivec2}   {$endif}
  3.  
  4. uses ...
  5. {$ifdef LCLCocoa}   CocoaAll,  {$endif}
  6.  
  7. procedure TranslateApplicationMenu;
  8. var mnMain: NSMenu;
  9.     mnApp : NSMenu;
  10.     mnItem: NSMenuItem;
  11.     i     : integer;
  12. begin
  13.    mnMain := NSApplication (NSApp).mainmenu;
  14.    mnItem := NSMenuItem (mnMain.itemArray.objectAtIndex(0));
  15.    if (mnItem.hasSubmenu) then begin
  16.       mnApp := mnItem.submenu;
  17.       for i:=0 to mnApp.itemArray.count-1 do begin
  18.          mnItem := NSMenuItem(mnApp.itemArray.objectAtIndex(i));
  19.          if (NOT mnItem.isSeparatorItem)  then begin
  20.             if (mnItem.title.UTF8String = 'Services') then
  21.                mnItem.setTitle (NSString.stringWithUTF8string('Dienste'));
  22.             if (mnItem.title.UTF8String = 'Hide Others') then
  23.                mnItem.setTitle (NSString.stringWithUTF8string('Andere ausblenden'));
  24.             if (mnItem.title.UTF8String = 'Show All') then
  25.                mnItem.setTitle (NSString.stringWithUTF8string('Alle einblenden'));
  26.             if (mnItem.title.UTF8String = 'Hide ' + Application.Title) then
  27.                mnItem.setTitle (NSString.stringWithUTF8string(PChar(Application.Title + ' ausblenden')));
  28.             if (mnItem.title.UTF8String = 'Quit ' + Application.Title) then
  29.                mnItem.setTitle (NSString.stringWithUTF8string(PChar(Application.Title + ' beenden')));
  30.          end
  31.       end
  32.    end
  33. end;
  34.  
  35.  
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

 

TinyPortal © 2005-2018