Skip to content

Commit

Permalink
Remove finder classes
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Apr 14, 2024
1 parent 750bb67 commit df5cfa8
Show file tree
Hide file tree
Showing 30 changed files with 398 additions and 541 deletions.
52 changes: 12 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,6 @@ Mixing XLIFF versions is possible. Here is an example using XLIFF 1.2 and XLIFF
<source>Postcode</source>
<target>Postcode</target>
</trans-unit>
<trans-unit id="headline-examples">
<source>Examples</source>
<target>Examples</target>
</trans-unit>
<trans-unit id="translation-args-label">
<source>Translation with param</source>
<target>Translation with param</target>
Expand Down Expand Up @@ -285,10 +281,6 @@ Mixing XLIFF versions is possible. Here is an example using XLIFF 1.2 and XLIFF
<source>Postcode</source>
<target>Postleitzahl</target>
</trans-unit>
<trans-unit id="headline-examples">
<source>Examples</source>
<target>Beispiele</target>
</trans-unit>
<trans-unit id="translation-args-label">
<source>Translation with param</source>
<target>Übersetzung mit Parameter</target>
Expand Down Expand Up @@ -391,8 +383,6 @@ Mixing XLIFF versions is possible. Here is an example using XLIFF 1.2 and XLIFF
| messages.headline | Headline | Überschrift | Headline** |
| postcode* | Postcode | Postleitzahl | Zip code |
| messages.postcode | Postcode | Postleitzahl | Zip code |
| headline-examples* | Examples | Beispiele | Examples** |
| messages.headline-examples | Examples | Beispiele | Examples** |
| translation-args-label* | Translation with param | Übersetzung mit Parameter | Translation with param** |
| messages.translation-args-label | Translation with param | Übersetzung mit Parameter | Translation with param** |
| email-notice* | Your email {0} has been registered. | Ihre E-Mail {0} wurde registriert. | Your email {0} has been registered.** |
Expand All @@ -418,8 +408,8 @@ packages and implementations that use the MessageSource.

> [!NOTE]
> Translations in the default domain can be selected using the ```id``` or ```domain.id```.
Translations in other domains can only be selected using the ```otherdomain.id```. See the examples
below.
> Translations in other domains can only be selected using the ```otherdomain.id```. See the examples
> below.

<a name="a6.1"></a>
Expand Down Expand Up @@ -448,7 +438,7 @@ the [Full Example](#a7).
<strong th:text="#{payment.expiry_date}"/>

<!-- "Your email [email protected] has been registered." -->
<strong th:text="#{translation-args-label}"/>: <span th:text="#{email-notice('[email protected]')}"/>
<span th:text="#{email-notice('[email protected]')}"/>

<!-- "This is a default message." -->
<span th:text="${#messages.msgOrNull('not-exists-id')} ?: #{default-message}"/>
Expand All @@ -469,45 +459,27 @@ public MyClass(MessageSource messageSource) {
}

// "Headline"
this.messageSource.

getMessage("headline",null,locale);
this.messageSource.

getMessage("messages.headline",null,locale);
this.messageSource.getMessage("headline",null,locale);
this.messageSource.getMessage("messages.headline",null,locale);

// "Postcode"
this.messageSource.

getMessage("postcode",null,locale);
this.messageSource.

getMessage("messages.postcode",null,locale);
this.messageSource.getMessage("postcode",null,locale);
this.messageSource.getMessage("messages.postcode",null,locale);

// "Payment"
this.messageSource.

getMessage("payment.headline",null,locale);
this.messageSource.getMessage("payment.headline",null,locale);

// "Expiry date"
this.messageSource.

getMessage("payment.expiry-date",null,locale);
this.messageSource.getMessage("payment.expiry-date",null,locale);

// "Your email [email protected] has been registered."
Object[] args = {"[email protected]"};
this.messageSource.

getMessage("email-notice",args, locale);
this.messageSource.

getMessage("messages.email-notice",args, locale);
this.messageSource.getMessage("email-notice",args, locale);
this.messageSource.getMessage("messages.email-notice",args, locale);

// "This is a default message."
String defaultMessage = this.messageSource.getMessage("default-message", null, locale);
this.messageSource.

getMessage("not-exists-id",null,defaultMessage, locale);
this.messageSource.getMessage("not-exists-id",null,defaultMessage, locale);
```

<a name="a6.3"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private XliffTranslationMessageSource(Builder builder) {
builder.cache, builder.defaultLocale,
builder.defaultDomain
);

this.catalogHandler.initCache();
}

public static Builder builder(Cache cacheManager) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.alaugks.spring.messagesource.xliff.catalog;

import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogFileAdapter;
import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogFinder;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand All @@ -10,12 +8,12 @@ public final class Catalog extends CatalogAbstractHandler {

private final HashMap<String, Map<String, String>> catalogMap;
private final Locale defaultLocale;
private final String domain;
private final String defaultDomain;

public Catalog(Locale defaultLocal, String domain) {
public Catalog(Locale defaultLocal, String defaultDomain) {
this.catalogMap = new HashMap<>();
this.defaultLocale = defaultLocal;
this.domain = domain;
this.defaultDomain = defaultDomain;
}

@Override
Expand All @@ -29,13 +27,12 @@ public Map<String, Map<String, String>> getAll() {

@Override
public String get(Locale locale, String code) {
CatalogFinder finder = new CatalogFinder(
new CatalogFileAdapter(this.getAll()),
this.defaultLocale,
this.domain
);

String message = finder.find(locale, code);
if (locale.toString().isBlank() || code.isBlank()) {
return null;
}

String message = this.fromCatalog(locale, code);
if (message != null) {
return message;
}
Expand All @@ -45,7 +42,7 @@ public String get(Locale locale, String code) {

@Override
public void put(Locale locale, String domain, String code, String value) {
if (!locale.toString().isEmpty()) {
if (!locale.toString().isBlank() || !code.isBlank()) {
String localeKey = CatalogUtilities.localeToLocaleKey(locale);
this.catalogMap.putIfAbsent(
localeKey,
Expand All @@ -57,4 +54,92 @@ public void put(Locale locale, String domain, String code, String value) {
);
}
}

private String fromCatalog(Locale locale, String code) {
String value;

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

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

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

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

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

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

// Code+DefaultLanguage / DomainCode+DefaultLanguage
value = this.findInCatalog(
CatalogUtilities.buildLocaleWithoutRegion(this.defaultLocale),
code
);
if (value != null) {
return value;
}

// Code+DefaultLanguage / DomainCode+DefaultLanguage
value = this.findInCatalog(
CatalogUtilities.buildLocaleWithoutRegion(this.defaultLocale),
CatalogUtilities.concatCode(this.defaultDomain, code)
);
if (value != null) {
return value;
}

return value;
}

public String findInCatalog(Locale locale, String code) {
if (this.catalogMap.containsKey(CatalogUtilities.localeToLocaleKey(locale))) {
Map<String, String> languageCatalog = this.catalogMap.get(CatalogUtilities.localeToLocaleKey(locale));
if (languageCatalog.containsKey(code)) {
return languageCatalog.get(code);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class CatalogAbstractHandler implements CatalogInterface {

protected CatalogInterface nextHandler;

public CatalogInterface setNextHandler(CatalogInterface handler) {
public CatalogInterface nextHandle(CatalogInterface handler) {
this.nextHandler = handler;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package io.github.alaugks.spring.messagesource.xliff.catalog;

import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogCacheAdapter;
import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogFinder;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.cache.Cache;

public final class CatalogCache extends CatalogAbstractHandler {

private final Cache cache;
private final Locale defaultLocale;
private final String domain;

public CatalogCache(Locale defaultLocal, String domain, Cache cache) {
this.defaultLocale = defaultLocal;
this.domain = domain;
public CatalogCache(Cache cache) {
this.cache = cache;
}

Expand Down Expand Up @@ -45,42 +40,61 @@ public Map<String, Map<String, String>> getAll() {
@Override
public String get(Locale locale, String code) {

CatalogFinder finder = new CatalogFinder(
new CatalogCacheAdapter(this.cache),
this.defaultLocale,
this.domain
);
if (locale.toString().isBlank() || code.isBlank()) {
return null;
}

// Find in Cache
String value = this.find(locale, code);
if (value != null) {
return value;
}

String message = finder.find(locale, code);
if (message != null) {
return message;
// Find in next Handler
value = super.get(locale, code);
if (value != null) {
this.put(locale, code, value);
}

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

@Override
public void put(Locale locale, String domain, String code, String value) {
this.put(locale, CatalogUtilities.concatCode(domain, code), value);
}

public void put(Locale locale, String code, String value) {
if (!locale.toString().isEmpty()) {
public void put(Locale locale, String code, String targetValue) {
if (!locale.toString().isBlank() || !code.isBlank()) {
this.cache.putIfAbsent(
CatalogUtilities.createCode(locale, code),
value
targetValue
);
}
}

public void initCache() {
super.getAll().forEach((langCode, catalogDomain) -> catalogDomain.forEach((code, value) ->
this.put(
Locale.forLanguageTag(langCode.replace("_", "-")),
Locale.forLanguageTag(
CatalogUtilities.normalizeLocaleKey(langCode)
),
code,
value
)
));
}

private String find(Locale locale, String code) {
Cache.ValueWrapper valueWrapper = this.cache.get(
CatalogUtilities.createCode(locale, code)
);

if (valueWrapper != null) {
return Objects.requireNonNull(valueWrapper.get()).toString();
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public CatalogHandler(
Locale defaultLocale,
String defaultDomain
) {
this.catalogCache = new CatalogCache(defaultLocale, defaultDomain, cache);
this.catalogCache.setNextHandler(
this.catalogCache = new CatalogCache(cache);
this.catalogCache.nextHandle(
catalogBuilder.createCatalog(new Catalog(defaultLocale, defaultDomain))
);
}
Expand All @@ -25,16 +25,12 @@ public Map<String, Map<String, String>> getAll() {
}

public String get(Locale locale, String code) {
String value = this.catalogCache.get(locale, code);

if (value != null) {
return value;
if (locale.toString().isEmpty()) {
return null;
}

// If value not exists then init cache, because it was not in the cache.
this.initCache();

return null;
return this.catalogCache.get(locale, code);
}

void put(Locale locale, String code, String value) {
Expand Down
Loading

0 comments on commit df5cfa8

Please sign in to comment.