Skip to content

Commit

Permalink
Add version 1.1.0
Browse files Browse the repository at this point in the history
- Add handling translation unit identifiers attributes
- Improvement sourcecode
- Improvement and extended README.md
- Extended tests (spy tests for caching vs. create catalog)
  • Loading branch information
alaugks committed Jul 14, 2023
1 parent 7579650 commit a1dc46e
Show file tree
Hide file tree
Showing 26 changed files with 657 additions and 207 deletions.
1 change: 1 addition & 0 deletions .github/workflows/maven-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- main
- stage
- dev
- feature/*
# workflow_dispatch:
jobs:
tests:
Expand Down
285 changes: 230 additions & 55 deletions README.md

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-xliff</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
<description>Provides a MessageSource for translations from XLIFF files for Spring and Spring Boot.</description>
<url>https://github.com/alaugks/spring-xliff-translation</url>
<url>https://github.com/alaugks/spring-messagesource-xliff</url>

<licenses>
<license>
Expand All @@ -27,9 +27,9 @@
</developers>

<scm>
<connection>scm:git:https://github.com/alaugks/spring-xliff-translation</connection>
<developerConnection>scm:git:https://github.com/alaugks/spring-xliff-translation</developerConnection>
<url>https://github.com/alaugks/spring-xliff-translation</url>
<connection>scm:git:https://github.com/alaugks/spring-messagesource-xliff</connection>
<developerConnection>scm:git:https://github.com/alaugks/spring-messagesource-xliff</developerConnection>
<url>https://github.com/alaugks/spring-messagesource-xliff</url>
</scm>

<parent>
Expand Down Expand Up @@ -79,6 +79,18 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-subclass</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
import org.springframework.lang.Nullable;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;

public class XliffTranslationMessageSource implements MessageSource {

private final CatalogWrapper catalogWrapper;
private final ResourcesLoaderInterface resourcesLoader = new ResourcesLoader();
private final XliffCatalogBuilder xliffCatalogBuilder = new XliffCatalogBuilder();

public XliffTranslationMessageSource(CacheManager cacheManager) {
this.catalogWrapper = new CatalogWrapper(
cacheManager,
this.resourcesLoader,
new XliffCatalogBuilder(),
this.xliffCatalogBuilder,
new Catalog()
);
}
Expand All @@ -49,6 +51,11 @@ public XliffTranslationMessageSource setDefaultDomain(String defaultDomain) {
return this;
}

public XliffTranslationMessageSource setTranslationUnitIdentifiersOrdering(List<String> translationUnitIdentifiers) {
this.xliffCatalogBuilder.setTranslationUnitIdentifiersOrdering(translationUnitIdentifiers);
return this;
}

@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.apache.logging.log4j.Logger;
import org.springframework.cache.CacheManager;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Objects;

public final class CatalogWrapper {
public class CatalogWrapper {
private static final Logger logger = LogManager.getLogger();
private final CatalogCache catalogCache;
private final CatalogInterface catalog;
Expand All @@ -23,8 +23,8 @@ public CatalogWrapper(CacheManager cacheManager,
CatalogInterface catalog
) {
this.catalog = catalog;
this.catalogBuilder = catalogBuilder;
this.resourcesLoader = resourcesLoader;
this.catalogBuilder = catalogBuilder;
this.catalogCache = new CatalogCache(cacheManager);
}

Expand Down Expand Up @@ -62,7 +62,7 @@ public Translation get(Locale locale, String code) {
// If exists then init cache, because it was not in the cache. Cache empty?
if (targetValue != null) {
logger.info("Re-init xliff catalog cache");
this.catalogCache.initCache(loadedCatalog);
this.initCache(loadedCatalog);
}

// If not exists then is the targetValue is the code.
Expand Down Expand Up @@ -90,6 +90,11 @@ public void setDefaultDomain(String defaultDomain) {
this.defaultDomain = defaultDomain;
}

public void initCache(CatalogInterface catalog) {
// Without cache do not get load catalog
this.catalogCache.initCache(catalog);
}

public void initCache() {
// Without cache do not get load catalog
this.catalogCache.initCache(this.loadCatalog());
Expand All @@ -101,7 +106,7 @@ private CatalogInterface loadCatalog() {

private String chainLink(CatalogInterface catalog, Locale locale, String code) {
String targetValue;
HashMap<Integer, Locale> locales = new HashMap<>();
LinkedHashMap<Integer, Locale> locales = new LinkedHashMap<>();
// Follow the order
locales.put(0, locale); // First
locales.put(1, this.resourcesLoader.getDefaultLocale()); // Second
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

final class Xliff12 extends XliffAbstract implements XliffInterface {
static final String VERSION = "1.2";
private List<String> translationUnitIdentifiers = new ArrayList<>(Arrays.asList("resname", "id"));

@Override
public boolean support(String version) {
return version.equals(VERSION);
return version.equals("1.2");
}

@Override
public void setTranslationUnitIdentifiersOrdering(List<String> translationUnitIdentifiers) {
this.translationUnitIdentifiers = translationUnitIdentifiers;
}

@Override
public void read(CatalogInterface catalog, Document document, String domain, Locale locale) {
NodeList translationNodes = XliffParserUtility.getTranslationNodes(document, "trans-unit");
this.readItems(VERSION, catalog, domain, locale, translationNodes);
NodeList translationUnits = XliffParserUtility.getTranslationUnits(document, "trans-unit");
this.readItems(catalog, domain, locale, translationUnits, this.translationUnitIdentifiers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.alaugks.spring.messagesource.xliff.catalog.xliff;

import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogInterface;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

final class Xliff2 extends XliffAbstract implements XliffInterface {
private List<String> translationUnitIdentifiers = new ArrayList<>(List.of("id"));

@Override
public boolean support(String version) {
return List.of("2.0", "2.1").contains(version);
}

@Override
public void setTranslationUnitIdentifiersOrdering(List<String> translationUnitIdentifiers) {
this.translationUnitIdentifiers = translationUnitIdentifiers;
}

@Override
public void read(CatalogInterface catalog, Document document, String domain, Locale locale) {
NodeList translationUnits = XliffParserUtility.getTranslationUnits(document, "segment");
this.readItems(catalog, domain, locale, translationUnits, this.translationUnitIdentifiers);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,29 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.List;
import java.util.Locale;

// http://docs.oasis-open.org/xliff/xliff-core/v2.0/csprd01/xliff-core-v2.0-csprd01.html#segment
// http://docs.oasis-open.org/xliff/xliff-core/v2.1/os/xliff-core-v2.1-os.html#segment
// http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html#General_Identifiers
public class XliffAbstract {
// https://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html#General_Identifiers
// https://docs.oasis-open.org/xliff/xliff-core/v2.0/csprd01/xliff-core-v2.0-csprd01.html#segment
// https://docs.oasis-open.org/xliff/xliff-core/v2.1/os/xliff-core-v2.1-os.html#segment
abstract class XliffAbstract {
protected void readItems(
String version,
CatalogInterface catalog,
String domain,
Locale locale,
NodeList translationNodes
NodeList translationNodes,
List<String> translationUnitIdentifiers
) {
for (int item = 0; item < translationNodes.getLength(); item++) {
Node translationNode = translationNodes.item(item);

/* <trans-unit id=""> */
String id = XliffParserUtility.getId(translationNode);

String resname = null;
if (Xliff12.VERSION.equals(version)) {
resname = XliffParserUtility.getResname(translationNode, "resname");
}

/* <trans-unit> */
/* Translation Node */
Element translationNodeElement = (Element) translationNode;
/* <source> */
String sourceValue = XliffParserUtility.getSource(translationNodeElement);
/* <target> */
String targetValue = XliffParserUtility.getTargetValue(translationNodeElement);
/* code */
String code = XliffParserUtility.getCode(id, resname, sourceValue);
String code = XliffParserUtility.getCode(translationNodeElement, translationUnitIdentifiers);

/* Add catalog */
catalog.put(locale, domain, code, targetValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class XliffCatalogBuilder implements CatalogBuilderInterface {
public class XliffCatalogBuilder implements CatalogBuilderInterface {

private CatalogInterface catalog;
private List<String> translationUnitIdentifiers;

@Override
public CatalogInterface createCatalog(ResourcesLoaderInterface resourceLoader, CatalogInterface catalog) {
Expand All @@ -32,6 +34,10 @@ public CatalogInterface createCatalog(ResourcesLoaderInterface resourceLoader, C
}
}

public void setTranslationUnitIdentifiersOrdering(List<String> translationUnitIdentifiers) {
this.translationUnitIdentifiers = translationUnitIdentifiers;
}

private void readFile(ArrayList<ResourcesLoader.Dto> translationFiles) throws ParserConfigurationException, IOException {

XliffReader xliffReader = new XliffReader();
Expand Down Expand Up @@ -61,6 +67,9 @@ private void readFile(ArrayList<ResourcesLoader.Dto> translationFiles) throws Pa

XliffInterface xliffInterface = xliffReader.getReader(version);
if (xliffInterface != null) {
if (this.translationUnitIdentifiers != null) {
xliffInterface.setTranslationUnitIdentifiersOrdering(this.translationUnitIdentifiers);
}
xliffInterface.read(this.catalog, document, translationFile.getDomain(), translationFile.getLocale());
} else {
throw new XliffMessageSourceVersionSupportException(String.format("XLIFF version \"%s\" not supported.", version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogInterface;
import org.w3c.dom.Document;

import java.util.List;
import java.util.Locale;

public interface XliffInterface {
boolean support(String version);

void setTranslationUnitIdentifiersOrdering(List<String> translationUnitIdentifiers);

void read(CatalogInterface catalog, Document document, String domain, Locale locale);
}
Loading

0 comments on commit a1dc46e

Please sign in to comment.