Skip to content

Commit

Permalink
fix: Do not cache translation bundles in dev mode
Browse files Browse the repository at this point in the history
For #20118
  • Loading branch information
Artur- committed Oct 3, 2024
1 parent 1ebd400 commit 6639119
Showing 1 changed file with 30 additions and 5 deletions.
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

0 comments on commit 6639119

Please sign in to comment.