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

fix: Do not cache translation bundles in dev mode #20140

Closed
wants to merge 1 commit into from
Closed
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 org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
Expand Down Expand Up @@ -85,8 +86,10 @@ public boolean synchronizedHandleRequest(VaadinSession session,
return true;
}
Locale locale = getLocale(request);

boolean productionMode = session.getConfiguration().isProductionMode();
ResourceBundle translationPropertyFile = getTranslationPropertyFile(
locale);
locale, productionMode);
if (translationPropertyFile == null) {
handleNotFound(response);
} else {
Expand Down Expand Up @@ -147,7 +150,8 @@ private Locale getLocale(VaadinRequest request) {
return Locale.forLanguageTag(languageTag);
}

private ResourceBundle getTranslationPropertyFile(Locale locale) {
private ResourceBundle getTranslationPropertyFile(Locale locale,
boolean productionMode) {
Locale bestMatchLocale = getBestMatchLocale(locale);
if (bestMatchLocale == null) {
if (FALLBACK_LOCALE.equals(locale)) {
Expand Down Expand Up @@ -176,9 +180,30 @@ private ResourceBundle getTranslationPropertyFile(Locale locale) {
bestMatchLocale.getDisplayName());
}
}
return i18NProvider.getBundle(bestMatchLocale,
ResourceBundle.Control.getNoFallbackControl(
ResourceBundle.Control.FORMAT_PROPERTIES));
ResourceBundle.Control noFallbackControl = ResourceBundle.Control
.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES);

ResourceBundle.Control noCacheNoFallbackControl = new ResourceBundle.Control() {
@Override
public long getTimeToLive(String baseName, Locale locale) {
return TTL_DONT_CACHE;
}

@Override
public Locale getFallbackLocale(String baseName, Locale locale) {
return noFallbackControl.getFallbackLocale(baseName, locale);
}

@Override
public List<String> getFormats(String baseName) {
return noFallbackControl.getFormats(baseName);
}

};

ResourceBundle.Control control = productionMode ? noFallbackControl
: noCacheNoFallbackControl;
return i18NProvider.getBundle(bestMatchLocale, control);
}

private Locale getBestMatchLocale(Locale locale) {
Expand Down
Loading