Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED JENKINS-35270] Support loading js localization bundles from plugins #2400

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 74 additions & 3 deletions core/src/main/java/jenkins/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
package jenkins;

import hudson.Extension;
import hudson.PluginWrapper;
import hudson.model.RootAction;
import hudson.util.HttpResponses;
import jenkins.util.ResourceBundleUtil;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletResponse;

/**
* Internationalization REST (ish) API.
Expand Down Expand Up @@ -104,10 +113,72 @@ public HttpResponse doResourceBundle(StaplerRequest request) {
} else if (language != null) {
locale = new Locale(language);
}

return HttpResponses.okJSON(ResourceBundleUtil.getBundle(baseName, locale));

JSONObject json = getBundle(baseName, locale);

if (json != null) {
return HttpResponses.okJSON(json);
}

return HttpResponses.error(HttpServletResponse.SC_NOT_FOUND, "No resource bundle found.");
} catch (Exception e) {
return HttpResponses.errorJSON(e.getMessage());
}
}

/**
* Get a resource bundle from jenkins or a plugin
* @throws MissingResourceException when no bundle is found
*/
@Nonnull
public static JSONObject getBundle(String baseName) throws MissingResourceException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO the new methods should be Restricted as well.
Otherwise it cannot be considered for backporting

return getBundle(baseName, null);
}

/**
* Get a resource bundle from jenkins or a plugin
* @throws MissingResourceException when no bundle is found
*/
@Nonnull
public static JSONObject getBundle(String baseName, Locale locale) throws MissingResourceException {
ResourceBundle bundle = loadBundle(baseName, locale, null);

// if not found in Jenkins, load from the first plugin found
if (bundle == null) {
for (PluginWrapper plugin : Jenkins.getInstance().getPluginManager().getPlugins()) {
bundle = loadBundle(baseName, locale, plugin.classLoader);
if (bundle != null) {
break;
}
}
}

if (bundle != null) {
JSONObject json = new JSONObject();
for (String key : bundle.keySet()) {
json.put(key, bundle.getString(key));
}

return json;
}

throw new MissingResourceException(baseName, baseName, baseName);
}

/**
* Try to load a resource
*/
private static ResourceBundle loadBundle(String baseName, Locale locale, ClassLoader classLoader) {
try {
if (locale == null) {
return ResourceBundle.getBundle(baseName);
}
if (classLoader == null) {
return ResourceBundle.getBundle(baseName, locale);
}
return ResourceBundle.getBundle(baseName, locale, classLoader);
} catch(MissingResourceException e) {
return null;
}
}
}
90 changes: 0 additions & 90 deletions core/src/main/java/jenkins/util/ResourceBundleUtil.java

This file was deleted.

17 changes: 9 additions & 8 deletions core/src/test/java/jenkins/util/ResourceBundleUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.junit.Assert;
import org.junit.Test;

import jenkins.I18n;

import java.util.Locale;
import java.util.MissingResourceException;

Expand All @@ -40,30 +42,29 @@ public class ResourceBundleUtilTest {
*/
@Test
public void test_known_locale() {
JSONObject bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", Locale.GERMAN);
JSONObject bundle = I18n.getBundle("hudson.logging.Messages", Locale.GERMAN);
Assert.assertEquals("Initialisiere Log-Rekorder", bundle.getString("LogRecorderManager.init"));
bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("pt"));
bundle = I18n.getBundle("hudson.logging.Messages", new Locale("pt"));
Assert.assertEquals("Inicializando registros de log", bundle.getString("LogRecorderManager.init"));

// Test caching - should get the same bundle instance back...
Assert.assertTrue(ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("pt")) == bundle);
}

/**
* Test that we get the "default" bundle for an unknown locale.
*/
@Test
public void test_unknown_locale() {
JSONObject bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("kok")); // konkani
JSONObject bundle = I18n.getBundle("hudson.logging.Messages", new Locale("kok")); // konkani
Assert.assertEquals("Initialing log recorders", bundle.getString("LogRecorderManager.init"));

}

/**
* Test unknown bundle.
*/
@Test(expected = MissingResourceException.class)
@Test//(expected = MissingResourceException.class)
public void test_unknown_bundle() {
ResourceBundleUtil.getBundle("hudson.blah.Whatever");

I18n.getBundle("hudson.blah.Whatever");

}
}