Skip to content

Commit

Permalink
Implement AbstractMessageSource
Browse files Browse the repository at this point in the history
  • Loading branch information
alaugks committed Jun 9, 2024
1 parent 5cc9394 commit 345b67e
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 329 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.alaugks.spring.messagesource.base;

import io.github.alaugks.spring.messagesource.base.catalog.CatalogInterface;
import io.github.alaugks.spring.messagesource.base.records.TransUnitCatalog;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.util.Assert;

public class CatalogMessageSource extends AbstractMessageSource {

public static final String DEFAULT_DOMAIN = "messages";

private final CatalogInterface catalog;

private CatalogMessageSource(CatalogInterface catalog) {
this.catalog = catalog;
}

public static Builder builder(CatalogInterface catalog) {
return new Builder(catalog);
}

public static final class Builder {

private CatalogInterface catalog;
private CatalogInterface catalogCache;

public Builder(CatalogInterface catalog) {
Assert.notNull(catalog, "Argument catalog must not be null");
this.catalog = catalog;
}

public Builder catalogCache(CatalogInterface cacheCatalog) {
Assert.notNull(cacheCatalog, "Argument cacheCatalog must not be null");
this.catalogCache = cacheCatalog;
return this;
}

public CatalogMessageSource build() {
if (catalogCache != null) {
this.catalog = catalogCache.nextHandler(this.catalog);
}

catalog.build();
return new CatalogMessageSource(this.catalog);
}
}

public List<TransUnitCatalog> getAll() {
return this.catalog.getAll();
}

public String get(Locale locale, String code) {
return this.catalog.resolve(locale, code);
}

@Override
protected MessageFormat resolveCode(String code, Locale locale) {
try {
return new MessageFormat(this.get(locale, code), locale);
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.alaugks.spring.messagesource.base.catalog;

import io.github.alaugks.spring.messagesource.base.BaseTranslationMessageSource;
import io.github.alaugks.spring.messagesource.base.records.TransUnitCatalog;
import io.github.alaugks.spring.messagesource.base.CatalogMessageSource;
import io.github.alaugks.spring.messagesource.base.records.TransUnit;
import io.github.alaugks.spring.messagesource.base.records.TransUnitCatalog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -19,7 +19,7 @@ public final class Catalog extends CatalogAbstract {
private final List<TransUnit> transUnits;

public Catalog(List<TransUnit> transUnits, Locale defaultLocale) {
this(transUnits, defaultLocale, BaseTranslationMessageSource.DEFAULT_DOMAIN);
this(transUnits, defaultLocale, CatalogMessageSource.DEFAULT_DOMAIN);
}

public Catalog(List<TransUnit> transUnits, Locale defaultLocale, String defaultDomain) {
Expand All @@ -31,7 +31,6 @@ public Catalog(List<TransUnit> transUnits, Locale defaultLocale, String defaultD
this.transUnits = transUnits;
this.defaultLocale = defaultLocale;
this.defaultDomain = defaultDomain;

}

@Override
Expand All @@ -53,18 +52,18 @@ public List<TransUnitCatalog> getAll() {
}

@Override
public String get(Locale locale, String code) {
public String resolve(Locale locale, String code) {

if (locale.toString().isEmpty() || code.isEmpty()) {
return null;
}

String value = this.fromCatalogMap(locale, code);
String value = this.resolveCode(locale, code);
if (value != null) {
return value;
}

return super.get(locale, code);
return super.resolve(locale, code);
}

@Override
Expand All @@ -76,7 +75,7 @@ public void build() {
private void put(Locale locale, String code, String value, String domain) {
if (!locale.toString().isEmpty() && !code.isEmpty()) {
if (domain == null) {
domain = BaseTranslationMessageSource.DEFAULT_DOMAIN;
domain = CatalogMessageSource.DEFAULT_DOMAIN;
}

String localeKey = super.localeToLocaleKey(locale);
Expand All @@ -92,49 +91,49 @@ private void put(Locale locale, String code, String value, String domain) {
}
}

private String fromCatalogMap(Locale locale, String code) {
private String resolveCode(Locale locale, String code) {
String value;

// Code+LocaleRegion
value = this.findInCatalogMap(locale, code);
value = this.resolveCodeInCatalogMap(locale, code);
if (value != null) {
return value;
}

// Code+LocaleRegion / DomainCode+LanguageRegion
value = this.findInCatalogMap(locale, concatCode(this.defaultDomain, code));
value = this.resolveCodeInCatalogMap(locale, concatCode(this.defaultDomain, code));
if (value != null) {
return value;
}

// Code+Language / DomainCode+Language
value = this.findInCatalogMap(buildLocaleWithoutRegion(locale), code);
value = this.resolveCodeInCatalogMap(buildLocaleWithoutRegion(locale), code);
if (value != null) {
return value;
}

// Code+Language / DomainCode+Language
value = this.findInCatalogMap(buildLocaleWithoutRegion(locale), concatCode(this.defaultDomain, code));
value = this.resolveCodeInCatalogMap(buildLocaleWithoutRegion(locale), concatCode(this.defaultDomain, code));
if (value != null) {
return value;
}

// Code+DefaultLanguageRegion / DomainCode+DefaultLanguageRegion
value = this.findInCatalogMap(this.defaultLocale, code);
value = this.resolveCodeInCatalogMap(this.defaultLocale, code);
if (value != null) {
return value;
}

// Code+DefaultLanguageRegion / DomainCode+DefaultLanguageRegion
value = this.findInCatalogMap(this.defaultLocale, concatCode(this.defaultDomain, code));
value = this.resolveCodeInCatalogMap(this.defaultLocale, concatCode(this.defaultDomain, code));
if (value != null) {
return value;
}

return value;
}

public String findInCatalogMap(Locale locale, String code) {
public String resolveCodeInCatalogMap(Locale locale, String code) {
String localeKey = super.localeToLocaleKey(locale);
if (this.catalogMap.containsKey(localeKey)) {
Map<String, String> languageCatalog = this.catalogMap.get(localeKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public List<TransUnitCatalog> getAll() {
}

@Override
public String get(Locale locale, String code) {
public String resolve(Locale locale, String code) {
if (this.nextHandler == null) {
return null;
}

return this.nextHandler.get(locale, code);
return this.nextHandler.resolve(locale, code);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<TransUnitCatalog> getAll() {
}

@Override
public String get(Locale locale, String code) {
public String resolve(Locale locale, String code) {

if (locale.toString().isEmpty() || code.isEmpty()) {
return null;
Expand All @@ -54,7 +54,7 @@ public String get(Locale locale, String code) {
return value;
}

value = super.get(locale, code);
value = super.resolve(locale, code);
if (value != null) {
this.put(locale, code, value);
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 345b67e

Please sign in to comment.