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

Fixes localized composite behaviour #5161

Merged
merged 5 commits into from
Nov 17, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.Util;
Expand Down Expand Up @@ -112,19 +113,14 @@ protected InputStream getNonCompressedInputStream(ResourceInfo resource, FacesCo
if (ctx.isProjectStage(Development)) {
ClassLoader loader = Util.getCurrentLoader(getClass());
String path = resource.getPath();
if (loader.getResource(path) != null) {
in = loader.getResource(path).openStream();
}
if (in == null && getClass().getClassLoader().getResource(path) != null) {
in = getClass().getClassLoader().getResource(path).openStream();
URL url = getResourceURL(loader, path, ctx);
if (url != null) {
in = url.openStream();
}
} else {
ClassLoader loader = Util.getCurrentLoader(getClass());
String path = resource.getPath();
in = loader.getResourceAsStream(path);
if (in == null) {
in = getClass().getClassLoader().getResourceAsStream(path);
}
in = getResourceAsStream(loader, path, ctx);
}
return in;
}
Expand Down Expand Up @@ -330,4 +326,44 @@ private URL findPathConsideringContracts(ClassLoader loader, LibraryInfo library
return result;
}

private InputStream getResourceAsStream(ClassLoader loader, String path, FacesContext ctx) {
InputStream in = null;
List<String> localizedPaths = getLocalizedPaths(path, ctx);
for (String path_: localizedPaths) {
in = getResourceAsStream(loader, path_);
if (in != null) {
break;
}
}
return in;
}

private URL getResourceURL(ClassLoader loader, String path, FacesContext ctx) {
List<String> localizedPaths = getLocalizedPaths(path, ctx);
URL url = null;
for (String path_: localizedPaths) {
url = getResource_(loader, path_);
if (url != null) {
break;
}
}
return url;
}

private InputStream getResourceAsStream(ClassLoader loader, String path) {
InputStream in = loader.getResourceAsStream(path);
if (in == null) {
in = getClass().getClassLoader().getResourceAsStream(path);
}
return in;
}

private URL getResource_(ClassLoader loader, String path) {
URL res = loader.getResource(path);
if (res == null) {
res = getClass().getClassLoader().getResource(path);
}
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -705,4 +706,24 @@ private boolean isPropertyValid(String property) {

} // END ELEvaluatingInputStream

protected List<String> getLocalizedPaths(String path, FacesContext ctx) {
Locale loc = (ctx != null && ctx.getViewRoot() != null) ? ctx.getViewRoot().getLocale() : null;
if (!path.endsWith(".properties") || loc == null) {
return Collections.singletonList(path);
}
List<String> list = new ArrayList<>();
String base = path.substring(0, path.lastIndexOf(".properties"));
if (!loc.getVariant().isEmpty()) {
list.add(String.format("%s_%s_%s_%s.properties", base, loc.getLanguage(), loc.getCountry(), loc.getVariant()));
}
if (!loc.getCountry().isEmpty()) {
list.add(String.format("%s_%s_%s.properties", base, loc.getLanguage(), loc.getCountry()));
}
if (!loc.getLanguage().isEmpty()) {
list.add(String.format("%s_%s.properties", base, loc.getLanguage()));
}
list.add(path);
return list;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,21 @@ public String getBaseContractsPath() {
return BASE_CONTRACTS_PATH;
}

/**
/**
* @see ResourceHelper#getNonCompressedInputStream(com.sun.faces.application.resource.ResourceInfo,
* jakarta.faces.context.FacesContext)
*/
@Override
protected InputStream getNonCompressedInputStream(ResourceInfo resource, FacesContext ctx) throws IOException {

return ctx.getExternalContext().getResourceAsStream(resource.getPath());

List<String> localizedPaths = getLocalizedPaths(resource.getPath(), ctx);
InputStream in = null;
for (String path_: localizedPaths) {
in = ctx.getExternalContext().getResourceAsStream(path_);
if (in != null) {
break;
}
}
return in;
}

/**
Expand Down Expand Up @@ -289,5 +295,4 @@ private String findPathConsideringContracts(LibraryInfo library, String resource

return basePath;
}

}