You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by da...@apache.org on 2015/08/10 15:49:59 UTC

wicket git commit: WICKET-5966 Fixed locale from resource filename

Repository: wicket
Updated Branches:
  refs/heads/master e69fb6f4c -> d547fcd4f


WICKET-5966 Fixed locale from resource filename

The previous implementation of getLocaleFromFilename had the order of Locale and minimized
filepart wrong. The de facto standard for filenaming conventions is:

    <filepart>[_locale[_country[_variation]]][.min].extension

The old implementation had the locale part and .min part swapped.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/d547fcd4
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/d547fcd4
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/d547fcd4

Branch: refs/heads/master
Commit: d547fcd4faf3079a3578ab19f93cf976721bea39
Parents: e69fb6f
Author: Martijn Dashorst <ma...@gmail.com>
Authored: Mon Aug 10 15:09:38 2015 +0200
Committer: Martijn Dashorst <ma...@gmail.com>
Committed: Mon Aug 10 15:18:04 2015 +0200

----------------------------------------------------------------------
 .../wicket/util/resource/ResourceUtils.java     | 21 ++++++++++++++------
 .../wicket/util/resource/ResourceUtilsTest.java |  6 ++++--
 2 files changed, 19 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/d547fcd4/wicket-util/src/main/java/org/apache/wicket/util/resource/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/wicket-util/src/main/java/org/apache/wicket/util/resource/ResourceUtils.java b/wicket-util/src/main/java/org/apache/wicket/util/resource/ResourceUtils.java
index edcd96c..ac1d025 100644
--- a/wicket-util/src/main/java/org/apache/wicket/util/resource/ResourceUtils.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/resource/ResourceUtils.java
@@ -34,7 +34,8 @@ public class ResourceUtils
 	/** The default postfix for minified names (ex: /css/mystyle.min.css) **/
 	public static final String MIN_POSTFIX_DEFAULT = "min";
 	/** Regular expression patter to extract the locale from the filename (ex: de_DE) **/
-	private static final Pattern LOCALE_PATTERN = Pattern.compile("_([a-z]{2})(_([A-Z]{2})(_([^_]+))?)?$");
+	private static final Pattern LOCALE_MIN_PATTERN = Pattern
+		.compile("_([a-z]{2})(_([A-Z]{2})(_([^_\\.]+))?)?(\\.min)?$");
 	/** Stores standard ISO country codes from {@code java.util.Locale} **/
 	private final static Set<String> isoCountries = new HashSet<>(
 		Arrays.asList(Locale.getISOCountries()));
@@ -77,7 +78,12 @@ public class ResourceUtils
 	}
 	
 	/**
-	 * Extract the locale from the filename
+	 * Extract the locale from the filename taking into account possible minimized resource name.
+	 * 
+	 * E.g. {@code file_us_EN.min.js} will correctly determine a locale of {@code us_EN} by
+	 * stripping the {@code .min} from the filename, the filename returned will be
+	 * {@code file.min.js}, if you want the {@code .min} to be removed as well, use
+	 * {@link #getLocaleFromMinifiedFilename(String)} instead.
 	 * 
 	 * @param path
 	 *            The file path
@@ -86,7 +92,8 @@ public class ResourceUtils
 	public static PathLocale getLocaleFromFilename(String path)
 	{
 		String extension = "";
-		int pos = path.lastIndexOf('.');
+
+		final int pos = path.lastIndexOf('.');
 		if (pos != -1)
 		{
 			extension = path.substring(pos);
@@ -94,12 +101,13 @@ public class ResourceUtils
 		}
 
 		String filename = Strings.lastPathComponent(path, '/');
-		Matcher matcher = LOCALE_PATTERN.matcher(filename);
+		Matcher matcher = LOCALE_MIN_PATTERN.matcher(filename);
 		if (matcher.find())
 		{
 			String language = matcher.group(1);
 			String country = matcher.group(3);
 			String variant = matcher.group(5);
+			String min = matcher.group(6);
 
 			// did we find a language?
 			if (language != null)
@@ -124,8 +132,9 @@ public class ResourceUtils
 
 			if (language != null)
 			{
-				pos = path.length() - filename.length() + matcher.start();
-				String basePath = path.substring(0, pos) + extension;
+				int languagePos = path.length() - filename.length() + matcher.start();
+				String basePath = path.substring(0, languagePos) + (min == null ? "" : min) +
+					extension;
 
 				Locale locale = new Locale(language, country != null ? country : "",
 					variant != null ? variant : "");

http://git-wip-us.apache.org/repos/asf/wicket/blob/d547fcd4/wicket-util/src/test/java/org/apache/wicket/util/resource/ResourceUtilsTest.java
----------------------------------------------------------------------
diff --git a/wicket-util/src/test/java/org/apache/wicket/util/resource/ResourceUtilsTest.java b/wicket-util/src/test/java/org/apache/wicket/util/resource/ResourceUtilsTest.java
index d1722a3..4174444 100644
--- a/wicket-util/src/test/java/org/apache/wicket/util/resource/ResourceUtilsTest.java
+++ b/wicket-util/src/test/java/org/apache/wicket/util/resource/ResourceUtilsTest.java
@@ -25,6 +25,7 @@ import java.util.Locale;
 import org.junit.Assert;
 import org.junit.Test;
 
+/** */
 public class ResourceUtilsTest extends Assert
 {
 	/**
@@ -47,12 +48,13 @@ public class ResourceUtilsTest extends Assert
 		assertThat(pathLocale.path, is(equalTo("some.min.ext")));
 		assertThat(pathLocale.locale, is(Locale.ENGLISH));
 
-		pathLocale = ResourceUtils.getLocaleFromFilename("some.min_fr_CA.ext");
+		pathLocale = ResourceUtils.getLocaleFromFilename("some_fr_CA.min.ext");
 		assertThat(pathLocale.path, is(equalTo("some.min.ext")));
 		assertThat(pathLocale.locale, is(Locale.CANADA_FRENCH));
 
 		String localeVariant = "blah";
-		pathLocale = ResourceUtils.getLocaleFromFilename("some.min_fr_CA_"+localeVariant+".ext");
+		pathLocale = ResourceUtils
+			.getLocaleFromFilename("some_fr_CA_" + localeVariant + ".min.ext");
 		assertThat(pathLocale.path, is(equalTo("some.min.ext")));
 		assertThat(pathLocale.locale.getLanguage(), is("fr"));
 		assertThat(pathLocale.locale.getCountry(), is("CA"));