diff --git a/modules/client/src/com/haulmont/cuba/client/sys/MessagesClientImpl.java b/modules/client/src/com/haulmont/cuba/client/sys/MessagesClientImpl.java index 0275907b72..71aff0a24f 100644 --- a/modules/client/src/com/haulmont/cuba/client/sys/MessagesClientImpl.java +++ b/modules/client/src/com/haulmont/cuba/client/sys/MessagesClientImpl.java @@ -8,6 +8,7 @@ package com.haulmont.cuba.client.sys; import com.haulmont.cuba.client.ClientConfig; import com.haulmont.cuba.core.app.LocalizedMessageService; +import com.haulmont.cuba.core.global.Configuration; import com.haulmont.cuba.core.global.Messages; import com.haulmont.cuba.core.global.UserSessionSource; import com.haulmont.cuba.core.sys.AbstractMessages; @@ -29,12 +30,21 @@ import java.util.Locale; public class MessagesClientImpl extends AbstractMessages { @Inject - private LocalizedMessageService localizedMessageService; + protected LocalizedMessageService localizedMessageService; @Inject protected UserSessionSource userSessionSource; - private volatile boolean remoteSearch; + protected volatile boolean remoteSearch; + + protected ClientConfig clientConfig; + + @Inject + @Override + public void setConfiguration(Configuration configuration) { + super.setConfiguration(configuration); + clientConfig = configuration.getConfig(ClientConfig.class); + } @Override protected Locale getUserLocale() { @@ -73,6 +83,6 @@ public class MessagesClientImpl extends AbstractMessages { } public void setRemoteSearch(boolean remoteSearch) { - this.remoteSearch = remoteSearch && configuration.getConfig(ClientConfig.class).getRemoteMessagesSearchEnabled(); + this.remoteSearch = remoteSearch && clientConfig.getRemoteMessagesSearchEnabled(); } } diff --git a/modules/global/src/com/haulmont/cuba/core/global/GlobalConfig.java b/modules/global/src/com/haulmont/cuba/core/global/GlobalConfig.java index 5a794ea0aa..4e2d12e8f9 100644 --- a/modules/global/src/com/haulmont/cuba/core/global/GlobalConfig.java +++ b/modules/global/src/com/haulmont/cuba/core/global/GlobalConfig.java @@ -131,5 +131,12 @@ public interface GlobalConfig extends Config { @Property("cuba.allowQueryFromSelected") @DefaultBoolean(true) boolean getAllowQueryFromSelected(); + + /** + * @return If true, localized messages search will use locale language only, ignoring country, variant, etc. + */ + @Property("cuba.useLocaleLanguageOnly") + @DefaultBoolean(true) + boolean getUseLocaleLanguageOnly(); } diff --git a/modules/global/src/com/haulmont/cuba/core/sys/AbstractMessages.java b/modules/global/src/com/haulmont/cuba/core/sys/AbstractMessages.java index 5f9ea5aec1..46fadeb9e3 100644 --- a/modules/global/src/com/haulmont/cuba/core/sys/AbstractMessages.java +++ b/modules/global/src/com/haulmont/cuba/core/sys/AbstractMessages.java @@ -41,19 +41,18 @@ public abstract class AbstractMessages implements Messages { public static final String EXT = ".properties"; public static final String ENCODING = "UTF-8"; + protected Log log = LogFactory.getLog(getClass()); + @Inject protected MessageTools messageTools; - @Inject - protected Configuration configuration; - protected Pattern enumSubclassPattern = Pattern.compile("\\$[1-9]"); - protected Log log = LogFactory.getLog(getClass()); + protected GlobalConfig globalConfig; protected String confDir; - private String mainMessagePack; + protected String mainMessagePack; protected Map strCache = new ConcurrentHashMap<>(); @@ -64,6 +63,12 @@ public abstract class AbstractMessages implements Messages { protected abstract String searchRemotely(String pack, String key, Locale locale); + @Inject + public void setConfiguration(Configuration configuration) { + globalConfig = configuration.getConfig(GlobalConfig.class); + confDir = globalConfig.getConfDir().replaceAll("\\\\", "/"); + } + @PostConstruct protected void init() { mainMessagePack = AppContext.getProperty("cuba.mainMessagePack"); @@ -71,7 +76,7 @@ public abstract class AbstractMessages implements Messages { throw new IllegalStateException("Property cuba.messagePack is not set"); log.debug("Main message pack: " + mainMessagePack); - for (Locale locale : configuration.getConfig(GlobalConfig.class).getAvailableLocales().values()) { + for (Locale locale : globalConfig.getAvailableLocales().values()) { Datatypes.setFormatStrings( locale, new FormatStrings( @@ -186,6 +191,9 @@ public abstract class AbstractMessages implements Messages { if (key == null) throw new IllegalArgumentException("Message key is null"); + if (globalConfig.getUseLocaleLanguageOnly()) + locale = Locale.forLanguageTag(locale.getLanguage()); + String cacheKey = makeCacheKey(packs, key, locale, false); String msg = strCache.get(cacheKey); @@ -277,13 +285,9 @@ public abstract class AbstractMessages implements Messages { log.trace("searchFiles: " + cacheKey); - File file; - if (confDir == null) - confDir = configuration.getConfig(GlobalConfig.class).getConfDir().replaceAll("\\\\", "/"); - String packPath = confDir + "/" + pack.replaceAll("\\.", "/"); while (packPath != null && !packPath.equals(confDir)) { - file = new File(packPath + "/" + BUNDLE_NAME + getLocaleSuffix(defaultLocale ? null : locale) + EXT); + File file = new File(packPath + "/" + BUNDLE_NAME + getLocaleSuffix(defaultLocale ? null : locale) + EXT); if (file.exists()) { try { FileInputStream stream = new FileInputStream(file); @@ -338,14 +342,10 @@ public abstract class AbstractMessages implements Messages { } private void getAllIncludes(List list, String pack, Locale locale, boolean defaultLocale) { - File file; - if (confDir == null) - confDir = configuration.getConfig(GlobalConfig.class).getConfDir().replaceAll("\\\\", "/"); - log.trace("include: " + pack); String packPath = confDir + "/" + pack.replaceAll("\\.", "/"); - file = new File(packPath + "/" + BUNDLE_NAME + getLocaleSuffix(defaultLocale ? null : locale) + EXT); + File file = new File(packPath + "/" + BUNDLE_NAME + getLocaleSuffix(defaultLocale ? null : locale) + EXT); InputStream stream; try { if (file.exists()) {