如何将Spotlight for Help插入Java中的本地化macOS应用程序中? [英] How to insert Spotlight for Help into localized macOS applications in Java?

查看:59
本文介绍了如何将Spotlight for Help插入Java中的本地化macOS应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用macOS上的Swing GUI框架实现Java应用程序.使用系统外观和屏幕菜单栏时,Swing会自动插入名为

  System.setProperty("apple.laf.useScreenMenuBar","true");尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch(Exception e){e.printStackTrace();}JFrame框架=新的JFrame();JMenuBar menuBar =新的JMenuBar();JMenu helpMenu = new JMenu("Help");menuBar.add(helpMenu);frame.setJMenuBar(menuBar); 

随着我的应用程序本地化,英语字符串帮助" 在其他语言环境中也有所不同(例如,法语的"Aide" ).但是在这种情况下,Swing不会插入Spotlight for Help,因为字符串不同:

解决方案

解决方案

  1. 捆绑 .class 文件和应用程序的资源(图像、声音、视频、本地化文件等)位于带有 Oracle 将您的 .jar 文件与Oracle的.app 目录中.oracle.com/javase/7/docs/technotes/guides/jweb/packagingAppsForMac.html"rel =" nofollow noreferrer> AppBundler (适用于Java 7+,它取代了旧的Apple的 setHelpMenu(JMenu) ,但是您会发现 MenuBar 相反,您会看到行为是完全一样的,尽管足够有趣的是,方法有所不同.

    这时我发现了一种解决方法,它将菜单标签设置为"Help" 并显示出来(不要使用.

    检查API,很明显,在Swing中,此功能为字符串的菜单,以便添加OS-localized搜索菜单中的字段;

  2. 否则,OS将应用程序视为已本地化的 en_US ,因此需要一个标有 en_US -localized "Help" 的菜单字符串,以便将 en_US 本地化的搜索字段添加到该菜单.
  3. 现在,您可以在终端中键入 open< application> .app ,您的应用程序将在OS本地化搜索字段添加到帮助菜单的情况下启动.

    请注意,Apple有其自身的机制来强制应用程序使用不同于操作系统语言环境的其他语言环境,并且它使用的是 -AppleLanguages 选项( open< application> .app-args -AppleLanguages``(< locale>)'').语言切换器实用程序在后台具有相同的功能.同样,应该存在适当的 Contents/Resources/< locale> .lproj 目录,否则OS将把应用程序视为本地化的 en_US .

    您如何制作一个 .app 应用程序的 .class 文件和资源(图像,声音,视频,本地化文件等)中的目录超出了此问题的范围,因为它取决于您所使用的平台正在使用,但Oracle提供了 Java存档(以制作中间 .jar 文件)和 AppBundler (用于创建 .app 目录)实用程序.

    屏幕截图

    在此屏幕快照中,该操作系统以西班牙语进行了本地化,但是该应用程序以法语进行了本地化,因为它是通过 -AppleLanguages的(fr)" 选项启动的.

    I am implementing a Java application using the Swing GUI framework on macOS. When using the system look and feel and a screen menu bar, Swing automatically inserts a search field called Spotlight for Help into the first menu labelled "Help" of the menu bar of a frame:

    System.setProperty("apple.laf.useScreenMenuBar", "true");
    
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    JFrame frame = new JFrame();
    JMenuBar menuBar = new JMenuBar();
    JMenu helpMenu = new JMenu("Help");
    menuBar.add(helpMenu);
    frame.setJMenuBar(menuBar);
    

    As my application is localized, the English string "Help" is different in other locales (e.g. "Aide" in French). But in those cases Swing does not insert Spotlight for Help since the string is different:

    解决方案

    Solution

    1. Bundle the .class files and resources (image, sound, video, localization files, etc.) of your application in a .jar file with Oracle's Java Archive.
    2. Bundle your .jar file in an .app directory with Oracle's AppBundler (for Java 7+, which replaces the old Apple's JarBundler for Java 6).
    3. Add to the .app directory the Contents/Resources/<locale>.lproj directories for each locale that should be supported by your application (you can let the locale directories empty since the localization files can be already in the .jar file).
    4. Launch your application (double-click on the application icon in Finder or type open <application>.app in Terminal).

    The search field Spotlight for Help will appear.

    Explanation

    I'm going to make a guess and it's that you're executing the .class files directly (from an IDE, for instance) or the .jar file, because from what I know this should be working.

    Although what most sources say, Swing is deeply rooted on system calls and thus relies on the OS for many features, like in this case. Ideally, this should be covered by the method setHelpMenu(JMenu) but as you'll notice this has never been implemented.

    If you check first, you'll notice that there's no extra component added in your JMenuBar and you have no control over that. If you try using AWT's MenuBar instead you'll see the behavior is exactly the same although, insterestingly enough, the method setHelpMenu(Menu) it is really implemented but doesn't add the search field if the menu is named something different from "Help".

    At this point I found a workaround and it's setting the menu label to "Help" and once displayed (don't use ComponentListener.componentShown(ComponentEvent), this won't work, use AncestorListener.ancestorAdded(AncestorEvent)) changing the menu label to the localized one. This will add the search field to the help menu. However the search field will in English, with the label "Search".

    Checking the API, it's more than clear that in Swing this feature is not implemented and fully relies on AWT. AWT on the other hand has partly implemented the native calls to the OS, but it's not wired to be invokable. Reaching this point and knowing that the search field does appear in our application and that in other ones running in Java it's properly localized lets us hint that this is a trait of the OS itself (I might be wrong at this point and it's really AWT doing the dirty job, but wasn't able to find any piece of code that does it directly, although in Objective C you can define any).

    Reading the documentation about how to localize a Java application in MacOS, we note that:

    • it's a requirement that the application be bundled in an .app directory and contain the Contents/Resources/<os-locale>.lproj directory, so that the OS recognizes the OS locale as supported by the application, and consequently expects a menu labeled with the OS-localized "Help" string in order to add the OS-localized search field to that menu;
    • otherwise, the OS treats the application as en_US localized, and consequently expects a menu labeled with the en_US-localized "Help" string in order to add the en_US-localized search field to that menu.

    Now you can type open <application>.app in Terminal and your application will be launched with the OS-localized search field added to the help menu.

    Note that Apple has its own mechanism for forcing the application to use another locale than the OS locale, and it's using the -AppleLanguages option (open <application>.app --args -AppleLanguages "(<locale>)"). The Language Switcher utility does the same under the hood. Again, the appropriate Contents/Resources/<locale>.lproj directory should exist, otherwise the OS will treat the application as en_US localized.

    How you make an .app directory from the .class files and resources (image, sound, video, localization files, etc.) of your application is beyond the scope of this question because it varies depending on the platform you're using, but Oracle provides the Java Archive (to make the intermediary .jar file) and AppBundler (to make the .app directory) utilities.

    Screenshot

    The OS is localized in Spanish in this screenshot but the application is localized in French, because it's been launched with the -AppleLanguages "(fr)" option.

    这篇关于如何将Spotlight for Help插入Java中的本地化macOS应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆