Skip to content

Commit

Permalink
Add CatalogMessageSource
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Jun 9, 2024
1 parent eb67cf3 commit f013ea5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 91 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ This package provides a [MessageSource](https://docs.spring.io/spring-framework/
<a name="a1"></a>
## 1. Versions

| Version | Description |
|:-------------------|:------------------------------------------------------------------------------------------|
| 2.0.0.rc1-SNAPSHOT | [SNAPSHOT](https://github.com/alaugks/spring-messagesource-xliff/tree/snapshot/2.0.0) |
| 1.2.1 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.1) |
| 1.2.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.0) |
| 1.1.2 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.2) |
| 1.1.1 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.1) |
| 1.1.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.0) |
| 1.0.0 | First public version |
| Version | Description |
|:-----------------|:------------------------------------------------------------------------------------------|
| 2.0.0.2-SNAPSHOT | |
| 1.2.1 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.1) |
| 1.2.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.0) |
| 1.1.2 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.2) |
| 1.1.1 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.1) |
| 1.1.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.0) |
| 1.0.0 | First public version |

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alaugks_spring-xliff-translation&metric=alert_status)](https://sonarcloud.io/summary/overall?id=alaugks_spring-xliff-translation) [![Maven Central](https://img.shields.io/maven-central/v/io.github.alaugks/spring-messagesource-xliff.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.alaugks/spring-messagesource-xliff/1.2.1)

Expand All @@ -41,14 +41,14 @@ This package provides a [MessageSource](https://docs.spring.io/spring-framework/
<dependency>
<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-xliff</artifactId>
<version>2.0.0.rc1-SNAPSHOT</version>
<version>2.0.0.2-SNAPSHOT</version>
</dependency>
```

**Gradle**

```text
implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '2.0.0.rc1-SNAPSHOT'
implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '2.0.0.2-SNAPSHOT'
```


Expand All @@ -70,7 +70,7 @@ The class XliffTranslationMessageSource implements the [MessageSource](https://d

`withCache(Cache cache)` (***recommended***)

* An instance of the [Cache Interface](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/Cache.html) is required. [Supported Cache Providers](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#io.caching.provider) can also be used. These provide this Cache Interface. Here is an example using [Caffeine](https://github.com/alaugks/spring-messagesource-xliff-example/blob/main/src/main/java/io/github/alaugks/config/CacheConfig.java) <span style="color:#ff0000;font-weight:bold;">(@Todo Example for 2.0.0.)</span>.
* An instance of the [Cache Interface](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/Cache.html) is required. [Supported Cache Providers](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#io.caching.provider) can also be used. These provide this Cache Interface. Here is an example using [Caffeine](https://github.com/alaugks/spring-messagesource-xliff-example/blob/main/src/main/java/io/github/alaugks/config/CacheConfig.java) (**@Todo Example for 2.0.0**).


### Example
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>spring-messagesource-xliff</artifactId>
<version>2.0.0.rc1-SNAPSHOT</version>
<version>2.0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -52,8 +52,8 @@
<dependencies>
<dependency>
<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-base</artifactId>
<version>1.0.0.rc1-SNAPSHOT</version>
<artifactId>spring-messagesource-catalog</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.github.alaugks.spring.messagesource.xliff;

import io.github.alaugks.spring.messagesource.base.catalog.Catalog;
import io.github.alaugks.spring.messagesource.base.records.Translation;
import io.github.alaugks.spring.messagesource.base.records.TranslationFile;
import io.github.alaugks.spring.messagesource.base.ressources.ResourcesLoader;
import io.github.alaugks.spring.messagesource.catalog.records.TransUnit;
import io.github.alaugks.spring.messagesource.catalog.records.TranslationFile;
import io.github.alaugks.spring.messagesource.catalog.ressources.ResourcesLoader;
import io.github.alaugks.spring.messagesource.xliff.exception.SaxErrorHandler;
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException;
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceSAXParseException.FatalError;
Expand All @@ -24,9 +23,8 @@

public final class XliffCatalog {

private final String defaultDomain;
private final Locale defaultLocale;
private final List<Translation> translations;
private final List<TransUnit> translations;
private final List<String> fileExtensions;
private final Set<String> basenames;
private final List<XliffIdentifierInterface> identifiers;
Expand All @@ -39,24 +37,21 @@ public final class XliffCatalog {
public XliffCatalog(
Set<String> basenames,
List<String> fileExtensions,
String defaultDomain,
Locale defaultLocale,
List<XliffIdentifierInterface> identifiers
) {
Assert.notNull(basenames, "Basename(s) cant not be null");
Assert.notNull(fileExtensions, "FileExtensions cant not be null");
Assert.notNull(defaultDomain, "Default domain cant not be null");
Assert.notNull(defaultLocale, "Default locale cant not be null");

this.basenames = basenames;
this.defaultDomain = defaultDomain;
this.defaultLocale = defaultLocale;
this.fileExtensions = fileExtensions;
this.translations = new ArrayList<>();
this.identifiers = identifiers == null ? List.of() : identifiers;
}

public Catalog createCatalog() {
public List<TransUnit> getTransUnits() {
try {
ResourcesLoader resourcesLoader = new ResourcesLoader(
this.defaultLocale,
Expand All @@ -65,7 +60,7 @@ public Catalog createCatalog() {
);

this.parseXliffDocuments(resourcesLoader.getTranslationFiles());
return new Catalog(this.translations, this.defaultLocale, this.defaultDomain);
return this.translations;
} catch (ParserConfigurationException | IOException e) {
throw new FatalError(e);
}
Expand Down Expand Up @@ -106,12 +101,11 @@ private void parseXliffDocuments(List<TranslationFile> xliffFiles)
xliffDocument.getTransUnits(
xliffVersionObject.getTransUnitName(),
this.resolveIdentifiers(this.identifiers, xliffVersionObject).list()
).forEach(
transUnit -> this.translations.add(
new Translation(
).forEach((code, value) -> this.translations.add(
new TransUnit(
xliffFile.locale(),
transUnit.code(),
transUnit.value(),
code,
value,
xliffFile.domain()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.alaugks.spring.messagesource.xliff;

import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand All @@ -24,7 +24,7 @@ public XliffDocument(Document document) {
this.root = document.getDocumentElement();
}

public Set<TransUnit> getTransUnits(String transUnitName, List<String> transUnitIdentifiers) {
public Map<String, String> getTransUnits(String transUnitName, List<String> transUnitIdentifiers) {
this.nodeList = this.root.getElementsByTagName(transUnitName);
this.transUnitIdentifiers = transUnitIdentifiers;
return this.getNodes();
Expand All @@ -41,8 +41,8 @@ public String getXliffVersion() {
);
}

private Set<TransUnit> getNodes() {
Set<TransUnit> transUnits = new HashSet<>();
private Map<String, String> getNodes() {
Map<String, String> transUnits = new HashMap<>();

for (int item = 0; item < nodeList.getLength(); item++) {
Element node = (Element) nodeList.item(item);
Expand All @@ -51,12 +51,10 @@ private Set<TransUnit> getNodes() {
node.getAttributes().getNamedItem(value.toString())
))
.filter(Objects::nonNull)
.findFirst().ifPresent(code -> transUnits.add(
new TransUnit(
code,
getCharacterDataFromElement(
node.getElementsByTagName("target").item(0).getFirstChild()
)
.findFirst().ifPresent(code -> transUnits.put(
code,
getCharacterDataFromElement(
node.getElementsByTagName("target").item(0).getFirstChild()
)
));
}
Expand All @@ -77,8 +75,4 @@ private String getAttributeValue(Node node) {
}
return null;
}

public record TransUnit(String code, String value) {

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.alaugks.spring.messagesource.xliff;

import io.github.alaugks.spring.messagesource.base.BaseTranslationMessageSource;
import io.github.alaugks.spring.messagesource.base.catalog.CatalogCache;
import io.github.alaugks.spring.messagesource.base.catalog.CatalogHandler;
import io.github.alaugks.spring.messagesource.catalog.CatalogMessageSource;
import io.github.alaugks.spring.messagesource.catalog.catalog.CatalogBuilder;
import io.github.alaugks.spring.messagesource.catalog.catalog.CatalogCache;
import io.github.alaugks.spring.messagesource.xliff.XliffCatalog.Xliff12Identifier;
import io.github.alaugks.spring.messagesource.xliff.XliffCatalog.Xliff2xIdentifier;
import io.github.alaugks.spring.messagesource.xliff.XliffCatalog.XliffIdentifierInterface;
Expand All @@ -18,10 +18,10 @@

public class XliffTranslationMessageSource implements MessageSource {

private final MessageSource messageSourceDelegate;
private final MessageSource messageSource;

private XliffTranslationMessageSource(MessageSource messageSourceDelegate) {
this.messageSourceDelegate = messageSourceDelegate;
private XliffTranslationMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}

public static Builder builder(Locale defaultLocale, String basename) {
Expand Down Expand Up @@ -71,39 +71,40 @@ public Builder identifier(List<XliffIdentifierInterface> identifier) {
}

public XliffTranslationMessageSource build() {
CatalogHandler.Builder catalogHandler = CatalogHandler.builder(
new XliffCatalog(
this.basenames,
this.fileExtensions,
this.defaultDomain,
this.defaultLocale,
this.identifier
).createCatalog()
);
CatalogBuilder.Builder catalogBuilder = CatalogBuilder.builder(
new XliffCatalog(
this.basenames,
this.fileExtensions,
this.defaultLocale,
this.identifier
).getTransUnits(),
this.defaultLocale
)
.defaultDomain(this.defaultDomain);

if (this.cache != null) {
catalogHandler.catalogCache(new CatalogCache(this.cache));
catalogBuilder.catalogCache(new CatalogCache(this.cache));
}

return new XliffTranslationMessageSource(
new BaseTranslationMessageSource(catalogHandler.build())
new CatalogMessageSource(catalogBuilder.build())
);
}
}

@Nullable
@Override
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.messageSourceDelegate.getMessage(code, args, defaultMessage, locale);
return this.messageSource.getMessage(code, args, defaultMessage, locale);
}

@Override
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
return this.messageSourceDelegate.getMessage(code, args, locale);
return this.messageSource.getMessage(code, args, locale);
}

@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
return this.messageSourceDelegate.getMessage(resolvable, locale);
return this.messageSource.getMessage(resolvable, locale);
}
}
Loading

0 comments on commit f013ea5

Please sign in to comment.