Skip to content

Commit

Permalink
Remove interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Aug 30, 2024
1 parent ef2d304 commit bea2ba2
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 305 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package io.github.alaugks.spring.messagesource.xliff;

import io.github.alaugks.spring.messagesource.xliff.catalog.Catalog;
import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogCache;
import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogWrapper;
import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogWrapper.Translation;
import io.github.alaugks.spring.messagesource.xliff.catalog.xliff.XliffCatalogBuilder;
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader;
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoaderInterface;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.lang.Nullable;
import org.springframework.context.support.AbstractMessageSource;

@SuppressWarnings("java:S1133")
public class XliffTranslationMessageSource implements MessageSource {
public class XliffTranslationMessageSource extends AbstractMessageSource implements MessageSource {

private final CatalogWrapper catalogWrapper;
private final ResourcesLoaderInterface resourcesLoader = new ResourcesLoader();
Expand All @@ -30,7 +26,6 @@ public class XliffTranslationMessageSource implements MessageSource {
@Deprecated(since = "2.0.0")
public XliffTranslationMessageSource() {
this.catalogWrapper = new CatalogWrapper(
new ConcurrentMapCacheManager(CatalogCache.CACHE_NAME),
this.resourcesLoader,
this.xliffCatalogBuilder,
new Catalog()
Expand Down Expand Up @@ -90,68 +85,22 @@ public XliffTranslationMessageSource setTranslationUnitIdentifiersOrdering(List<
return this;
}

@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.format(
this.internalMessageWithDefaultMessage(code, defaultMessage, locale),
args,
locale
);
}
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
Translation t = this.catalogWrapper.get(locale, code);

public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
CatalogWrapper.Translation translation = this.internalMessage(code, locale);
if (translation.exists()) {
return this.format(translation.toString(), args, locale);
if (t.getTargetValue() != null) {
return new MessageFormat(t.getTargetValue(), locale);
}

throw new NoSuchMessageException(code, locale);
}

public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
String[] codes = resolvable.getCodes();
if (codes != null) {
for (String code : codes) {
CatalogWrapper.Translation translation = internalMessage(code, locale);
if (translation.exists()) {
return this.format(translation.toString(), resolvable.getArguments(), locale);
}
}
}
if (resolvable instanceof DefaultMessageSourceResolvable) {
String defaultMessage = resolvable.getDefaultMessage();
if (defaultMessage != null) {
return this.format(defaultMessage, resolvable.getArguments(), locale);
}
}

throw new NoSuchMessageException(codes != null && codes.length > 0 ? codes[codes.length - 1] : "", locale);
}

private CatalogWrapper.Translation internalMessage(String code, Locale locale) throws NoSuchMessageException {
return this.findInCatalog(locale, code);
}

private String internalMessageWithDefaultMessage(String code, @Nullable String defaultMessage, Locale locale) {
CatalogWrapper.Translation translation = this.findInCatalog(locale, code);
if (translation.exists()) {
return translation.toString();
}
return defaultMessage;
}

private CatalogWrapper.Translation findInCatalog(Locale locale, String code) {
return this.catalogWrapper.get(locale, code);
return null;
}

/**
* @deprecated
*/
@Deprecated(since = "2.0.0")
public void initCache() {
this.catalogWrapper.initCache();
}

private String format(@Nullable String message, @Nullable Object[] args, Locale locale) {
if (message != null && args != null && args.length > 0) {
return new MessageFormat(message, locale).format(args);
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoaderInterface;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.CacheManager;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Objects;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;

public class CatalogWrapper {
private static final Logger logger = LogManager.getLogger(XliffTranslationMessageSource.class.toString());
Expand All @@ -18,15 +18,14 @@ public class CatalogWrapper {
private final ResourcesLoaderInterface resourcesLoader;
private String defaultDomain = "messages";

public CatalogWrapper(CacheManager cacheManager,
ResourcesLoaderInterface resourcesLoader,
public CatalogWrapper(ResourcesLoaderInterface resourcesLoader,
CatalogBuilderInterface catalogBuilder,
CatalogInterface catalog
) {
this.catalog = catalog;
this.resourcesLoader = resourcesLoader;
this.catalogBuilder = catalogBuilder;
this.catalogCache = new CatalogCache(cacheManager);
this.catalogCache = new CatalogCache(new ConcurrentMapCacheManager(CatalogCache.CACHE_NAME));
}

private static Locale buildLocaleWithLanguageRegion(Locale locale) {
Expand Down Expand Up @@ -166,6 +165,13 @@ public Translation(String code, String targetValue) {
this.targetValue = targetValue;
}

public String getTargetValue() {
if(!Objects.equals(this.code, this.targetValue)) {
return this.targetValue;
}
return null;
}

public boolean exists() {
return !Objects.equals(this.code, this.targetValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static CatalogInterface getTestCatalog() {

public static CatalogWrapper getCacheWrapperWithCachedTestCatalog() {
CatalogWrapper catalogWrapper = new CatalogWrapper(
getMockedCacheManager(),
getResourcesLoader(), new XliffCatalogBuilder(), TestUtilities.getTestCatalog()
getResourcesLoader(), new XliffCatalogBuilder(), TestUtilities.getTestCatalog()
);
catalogWrapper.initCache();
return catalogWrapper;
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit bea2ba2

Please sign in to comment.