Search in sources :

Example 11 with ResourceBundle

use of java.util.ResourceBundle in project qi4j-sdk by Qi4j.

the class ResponseWriterDelegator method init.

public void init(@Service Iterable<ServiceReference<ResponseWriter>> resultWriters) throws InitializationException {
    Logger logger = LoggerFactory.getLogger(getClass());
    // Add custom writers first
    for (ServiceReference<ResponseWriter> resultWriter : resultWriters) {
        if (!resultWriter.identity().equals("responsewriterdelegator")) {
            logger.info("Registered result writer:" + resultWriter.identity());
            registerResultWriter(resultWriter.get());
        }
    }
    // Add defaults
    ResourceBundle defaultResultWriters = ResourceBundle.getBundle("org.qi4j.library.rest.server.rest-server");
    String resultWriterClasses = defaultResultWriters.getString("responsewriters");
    logger.info("Using response writers:" + resultWriterClasses);
    for (String className : resultWriterClasses.split(",")) {
        try {
            Class writerClass = module.classLoader().loadClass(className.trim());
            ResponseWriter writer = (ResponseWriter) module.newObject(writerClass);
            registerResultWriter(writer);
        } catch (ClassNotFoundException e) {
            logger.warn("Could not register response writer " + className, e);
        }
    }
}
Also used : ResponseWriter(org.qi4j.library.rest.server.spi.ResponseWriter) ResourceBundle(java.util.ResourceBundle) Logger(org.slf4j.Logger)

Example 12 with ResourceBundle

use of java.util.ResourceBundle in project jersey by jersey.

the class Localizer method localize.

public String localize(Localizable l) {
    String key = l.getKey();
    if (Localizable.NOT_LOCALIZABLE.equals(key)) {
        // this message is not localizable
        return (String) l.getArguments()[0];
    }
    String bundlename = l.getResourceBundleName();
    try {
        ResourceBundle bundle = _resourceBundles.get(bundlename);
        if (bundle == null) {
            try {
                bundle = ResourceBundle.getBundle(bundlename, _locale);
            } catch (MissingResourceException e) {
                // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
                //   all files with an extension different from .class (hence all the .properties files)
                //   get copied to the top level directory instead of being in the package where they
                //   are defined
                // so, since we can't find the bundle under its proper name, we look for it under
                //   the top-level package
                int i = bundlename.lastIndexOf('.');
                if (i != -1) {
                    String alternateBundleName = bundlename.substring(i + 1);
                    try {
                        bundle = ResourceBundle.getBundle(alternateBundleName, _locale);
                    } catch (MissingResourceException e2) {
                        // try OSGi
                        OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();
                        if (osgiRegistry != null) {
                            bundle = osgiRegistry.getResourceBundle(bundlename);
                        } else {
                            final String path = bundlename.replace('.', '/') + ".properties";
                            final URL bundleUrl = ResourceFinder.findEntry(path);
                            if (bundleUrl != null) {
                                try {
                                    bundle = new PropertyResourceBundle(bundleUrl.openStream());
                                } catch (IOException ex) {
                                // ignore
                                }
                            }
                        }
                    }
                }
            }
            if (bundle == null) {
                return getDefaultMessage(l);
            } else {
                _resourceBundles.put(bundlename, bundle);
            }
        }
        if (key == null) {
            key = "undefined";
        }
        String msg;
        try {
            msg = bundle.getString(key);
        } catch (MissingResourceException e) {
            // notice that this may throw a MissingResourceException of its own (caught below)
            msg = bundle.getString("undefined");
        }
        // localize all arguments to the given localizable object
        Object[] args = l.getArguments();
        for (int i = 0; i < args.length; ++i) {
            if (args[i] instanceof Localizable) {
                args[i] = localize((Localizable) args[i]);
            }
        }
        String message = MessageFormat.format(msg, args);
        return message;
    } catch (MissingResourceException e) {
        return getDefaultMessage(l);
    }
}
Also used : OsgiRegistry(org.glassfish.jersey.internal.OsgiRegistry) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 13 with ResourceBundle

use of java.util.ResourceBundle in project blueocean-plugin by jenkinsci.

the class BlueI18n method getBundle.

@CheckForNull
private JSONObject getBundle(BundleParams bundleParams, Locale locale) {
    PluginWrapper plugin = bundleParams.getPlugin();
    if (plugin == null) {
        return null;
    }
    try {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleParams.bundleName, locale, plugin.classLoader);
        JSONObject bundleJSON = new JSONObject();
        for (String key : resourceBundle.keySet()) {
            bundleJSON.put(key, resourceBundle.getString(key));
        }
        return bundleJSON;
    } catch (MissingResourceException e) {
    // fall through and return null.
    }
    return null;
}
Also used : JSONObject(net.sf.json.JSONObject) MissingResourceException(java.util.MissingResourceException) PluginWrapper(hudson.PluginWrapper) ResourceBundle(java.util.ResourceBundle) CheckForNull(javax.annotation.CheckForNull)

Example 14 with ResourceBundle

use of java.util.ResourceBundle in project languagetool by languagetool-org.

the class ResourceBundleTools method getMessageBundle.

/**
   * Gets the ResourceBundle (i18n strings) for the given user interface language.
   */
static ResourceBundle getMessageBundle(Language lang) {
    try {
        ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_BUNDLE, lang.getLocaleWithCountryAndVariant());
        if (!isValidBundleFor(lang, bundle)) {
            bundle = ResourceBundle.getBundle(MESSAGE_BUNDLE, lang.getLocale());
            if (!isValidBundleFor(lang, bundle)) {
                // happens if 'xx' is requested but only a MessagesBundle_xx_YY.properties exists:
                Language defaultVariant = lang.getDefaultLanguageVariant();
                if (defaultVariant != null && defaultVariant.getCountries().length > 0) {
                    Locale locale = new Locale(defaultVariant.getShortCode(), defaultVariant.getCountries()[0]);
                    bundle = ResourceBundle.getBundle(MESSAGE_BUNDLE, locale);
                }
            }
        }
        ResourceBundle fallbackBundle = ResourceBundle.getBundle(MESSAGE_BUNDLE, Locale.ENGLISH);
        return new ResourceBundleWithFallback(bundle, fallbackBundle);
    } catch (MissingResourceException e) {
        return ResourceBundle.getBundle(MESSAGE_BUNDLE, Locale.ENGLISH);
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 15 with ResourceBundle

use of java.util.ResourceBundle in project languagetool by languagetool-org.

the class Main method createGUI.

private void createGUI() {
    loadRecentFiles();
    frame = new JFrame("LanguageTool " + JLanguageTool.VERSION);
    setLookAndFeel();
    openAction = new OpenAction();
    saveAction = new SaveAction();
    saveAsAction = new SaveAsAction();
    checkAction = new CheckAction();
    autoCheckAction = new AutoCheckAction(true);
    showResultAction = new ShowResultAction(true);
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new CloseListener());
    URL iconUrl = JLanguageTool.getDataBroker().getFromResourceDirAsUrl(TRAY_ICON);
    frame.setIconImage(new ImageIcon(iconUrl).getImage());
    textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.addKeyListener(new ControlReturnTextCheckingListener());
    resultArea = new JTextPane();
    undoRedo = new UndoRedoSupport(this.textArea, messages);
    frame.setJMenuBar(createMenuBar());
    GridBagConstraints buttonCons = new GridBagConstraints();
    JPanel insidePanel = new JPanel();
    insidePanel.setOpaque(false);
    insidePanel.setLayout(new GridBagLayout());
    buttonCons.gridx = 0;
    buttonCons.gridy = 0;
    buttonCons.anchor = GridBagConstraints.LINE_START;
    insidePanel.add(new JLabel(messages.getString("textLanguage") + " "), buttonCons);
    //create a ComboBox with flags, do not include hidden languages
    languageBox = LanguageComboBox.create(messages, EXTERNAL_LANGUAGE_SUFFIX, true, false);
    buttonCons.gridx = 1;
    buttonCons.gridy = 0;
    buttonCons.anchor = GridBagConstraints.LINE_START;
    insidePanel.add(languageBox, buttonCons);
    JCheckBox autoDetectBox = new JCheckBox(messages.getString("atd"));
    buttonCons.gridx = 2;
    buttonCons.gridy = 0;
    buttonCons.gridwidth = GridBagConstraints.REMAINDER;
    buttonCons.anchor = GridBagConstraints.LINE_START;
    insidePanel.add(autoDetectBox, buttonCons);
    buttonCons.gridx = 0;
    buttonCons.gridy = 1;
    buttonCons.gridwidth = GridBagConstraints.REMAINDER;
    buttonCons.fill = GridBagConstraints.HORIZONTAL;
    buttonCons.anchor = GridBagConstraints.LINE_END;
    buttonCons.weightx = 1.0;
    insidePanel.add(statusLabel, buttonCons);
    Container contentPane = frame.getContentPane();
    GridBagLayout gridLayout = new GridBagLayout();
    contentPane.setLayout(gridLayout);
    GridBagConstraints cons = new GridBagConstraints();
    cons.gridx = 0;
    cons.gridy = 1;
    cons.fill = GridBagConstraints.HORIZONTAL;
    cons.anchor = GridBagConstraints.FIRST_LINE_START;
    JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
    toolbar.setFloatable(false);
    contentPane.add(toolbar, cons);
    JButton openButton = new JButton(openAction);
    openButton.setHideActionText(true);
    openButton.setFocusable(false);
    toolbar.add(openButton);
    JButton saveButton = new JButton(saveAction);
    saveButton.setHideActionText(true);
    saveButton.setFocusable(false);
    toolbar.add(saveButton);
    JButton saveAsButton = new JButton(saveAsAction);
    saveAsButton.setHideActionText(true);
    saveAsButton.setFocusable(false);
    toolbar.add(saveAsButton);
    JButton spellButton = new JButton(this.checkAction);
    spellButton.setHideActionText(true);
    spellButton.setFocusable(false);
    toolbar.add(spellButton);
    JToggleButton autoSpellButton = new JToggleButton(autoCheckAction);
    autoSpellButton.setHideActionText(true);
    autoSpellButton.setFocusable(false);
    toolbar.add(autoSpellButton);
    JButton clearTextButton = new JButton(new ClearTextAction());
    clearTextButton.setHideActionText(true);
    clearTextButton.setFocusable(false);
    toolbar.add(clearTextButton);
    cons.insets = new Insets(5, 5, 5, 5);
    cons.fill = GridBagConstraints.BOTH;
    cons.weightx = 10.0f;
    cons.weighty = 10.0f;
    cons.gridx = 0;
    cons.gridy = 2;
    cons.weighty = 5.0f;
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(textArea), new JScrollPane(resultArea));
    mainPanel.setLayout(new GridLayout(0, 1));
    contentPane.add(mainPanel, cons);
    mainPanel.add(splitPane);
    cons.fill = GridBagConstraints.HORIZONTAL;
    cons.gridx = 0;
    cons.gridy = 3;
    cons.weightx = 1.0f;
    cons.weighty = 0.0f;
    cons.insets = new Insets(4, 12, 4, 12);
    contentPane.add(insidePanel, cons);
    ltSupport = new LanguageToolSupport(this.frame, this.textArea, this.undoRedo);
    ResultAreaHelper.install(messages, ltSupport, resultArea);
    languageBox.selectLanguage(ltSupport.getLanguage());
    languageBox.setEnabled(!ltSupport.getConfig().getAutoDetect());
    autoDetectBox.setSelected(ltSupport.getConfig().getAutoDetect());
    taggerShowsDisambigLog = ltSupport.getConfig().getTaggerShowsDisambigLog();
    languageBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                // we cannot re-use the existing LT object anymore
                frame.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
                Language lang = languageBox.getSelectedLanguage();
                ComponentOrientation componentOrientation = ComponentOrientation.getOrientation(lang.getLocale());
                textArea.applyComponentOrientation(componentOrientation);
                resultArea.applyComponentOrientation(componentOrientation);
                ltSupport.setLanguage(lang);
            }
        }
    });
    autoDetectBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            boolean selected = e.getStateChange() == ItemEvent.SELECTED;
            languageBox.setEnabled(!selected);
            ltSupport.getConfig().setAutoDetect(selected);
            if (selected) {
                Language detected = ltSupport.autoDetectLanguage(textArea.getText());
                languageBox.selectLanguage(detected);
            }
        }
    });
    ltSupport.addLanguageToolListener(new LanguageToolListener() {

        @Override
        public void languageToolEventOccurred(LanguageToolEvent event) {
            if (event.getType() == LanguageToolEvent.Type.CHECKING_STARTED) {
                String msg = org.languagetool.tools.Tools.i18n(messages, "checkStart");
                statusLabel.setText(msg);
                if (event.getCaller() == getFrame()) {
                    setWaitCursor();
                    checkAction.setEnabled(false);
                }
            } else if (event.getType() == LanguageToolEvent.Type.CHECKING_FINISHED) {
                if (event.getCaller() == getFrame()) {
                    checkAction.setEnabled(true);
                    unsetWaitCursor();
                }
                String msg = org.languagetool.tools.Tools.i18n(messages, "checkDone", event.getSource().getMatches().size(), event.getElapsedTime());
                statusLabel.setText(msg);
            } else if (event.getType() == LanguageToolEvent.Type.LANGUAGE_CHANGED) {
                languageBox.selectLanguage(ltSupport.getLanguage());
            } else if (event.getType() == LanguageToolEvent.Type.RULE_ENABLED) {
            //this will trigger a check and the result will be updated by
            //the CHECKING_FINISHED event
            } else if (event.getType() == LanguageToolEvent.Type.RULE_DISABLED) {
                String msg = org.languagetool.tools.Tools.i18n(messages, "checkDoneNoTime", event.getSource().getMatches().size());
                statusLabel.setText(msg);
            }
        }
    });
    frame.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
    Language lang = ltSupport.getLanguage();
    ComponentOrientation componentOrientation = ComponentOrientation.getOrientation(lang.getLocale());
    textArea.applyComponentOrientation(componentOrientation);
    resultArea.applyComponentOrientation(componentOrientation);
    ResourceBundle textLanguageMessageBundle = JLanguageTool.getMessageBundle(ltSupport.getLanguage());
    textArea.setText(textLanguageMessageBundle.getString("guiDemoText"));
    Configuration config = ltSupport.getConfig();
    if (config.getFontName() != null || config.getFontStyle() != Configuration.FONT_STYLE_INVALID || config.getFontSize() != Configuration.FONT_SIZE_INVALID) {
        String fontName = config.getFontName();
        if (fontName == null) {
            fontName = textArea.getFont().getFamily();
        }
        int fontSize = config.getFontSize();
        if (fontSize == Configuration.FONT_SIZE_INVALID) {
            fontSize = textArea.getFont().getSize();
        }
        Font font = new Font(fontName, config.getFontStyle(), fontSize);
        textArea.setFont(font);
    }
    frame.pack();
    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    frame.setLocationByPlatform(true);
    splitPane.setDividerLocation(200);
    MainWindowStateBean state = localStorage.loadProperty("gui.state", MainWindowStateBean.class);
    if (state != null) {
        if (state.getBounds() != null) {
            frame.setBounds(state.getBounds());
            ResizeComponentListener.setBoundsProperty(frame, state.getBounds());
        }
        if (state.getDividerLocation() != null) {
            splitPane.setDividerLocation(state.getDividerLocation());
        }
        if (state.getState() != null) {
            frame.setExtendedState(state.getState());
        }
    }
    ResizeComponentListener.attachToWindow(frame);
    maybeStartServer();
}
Also used : URL(java.net.URL) Language(org.languagetool.Language) ResourceBundle(java.util.ResourceBundle)

Aggregations

ResourceBundle (java.util.ResourceBundle)605 Locale (java.util.Locale)80 MissingResourceException (java.util.MissingResourceException)80 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)36 ActionMessage (org.apache.struts.action.ActionMessage)29 Test (org.junit.Test)29 File (java.io.File)27 IOException (java.io.IOException)27 Enumeration (java.util.Enumeration)24 PropertyResourceBundle (java.util.PropertyResourceBundle)22 Preferences (java.util.prefs.Preferences)20 URL (java.net.URL)19 Map (java.util.Map)19 HashSet (java.util.HashSet)17 Set (java.util.Set)16 Iterator (java.util.Iterator)15 ListResourceBundle (java.util.ListResourceBundle)14 Engine (jgnash.engine.Engine)14 SSOException (com.iplanet.sso.SSOException)13