Skip to content

Commit

Permalink
Fix MessageFormat for the specified locale and pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
alaugks committed Apr 30, 2024
1 parent aa4dc63 commit 491ca71
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ This package provides a **MessageSource** for using translations from XLIFF file
| Version | Description |
|:---------------|:------------------------------------------------------------------------------------------|
| 2.0.0-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 |

[![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.0)
[![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)


<a name="a2"></a>
Expand All @@ -38,13 +39,13 @@ This package provides a **MessageSource** for using translations from XLIFF file
<dependency>
<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-xliff</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```

**Gradle**
```text
implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '1.2.0'
implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '1.2.1'
```


Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ public XliffTranslationMessageSource setTranslationUnitIdentifiersOrdering(List<
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.format(
this.internalMessageWithDefaultMessage(code, defaultMessage, locale),
args
args,
locale
);
}

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);
return this.format(translation.toString(), args, locale);
}

throw new NoSuchMessageException(code, locale);
Expand All @@ -103,14 +104,14 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
for (String code : codes) {
CatalogWrapper.Translation translation = internalMessage(code, locale);
if (translation.exists()) {
return this.format(translation.toString(), resolvable.getArguments());
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());
return this.format(defaultMessage, resolvable.getArguments(), locale);
}
}

Expand All @@ -137,9 +138,9 @@ public void initCache() {
this.catalogWrapper.initCache();
}

private String format(@Nullable String message, @Nullable Object[] args) {
private String format(@Nullable String message, @Nullable Object[] args, Locale locale) {
if (message != null && args != null && args.length > 0) {
return new MessageFormat(message).format(args);
return new MessageFormat(message, locale).format(args);
}
return message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.github.alaugks.spring.messagesource.xliff;

import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class XliffMatchingResourcePatternResolverCasesTest {

Expand Down Expand Up @@ -99,6 +98,36 @@ void test_setBasenamePattern() {
assertEquals("Other Hello EN (otherdomain)", message);
}

@Test
void test_messagesFormat_choice() {
var messageSource = new XliffTranslationMessageSource(TestUtilities.getMockedCacheManager());
messageSource.setBasenamesPattern(List.of("translations/*"));
messageSource.setDefaultLocale(Locale.forLanguageTag("en"));
messageSource.initCache();

assertEquals("There are 10,000 files.", messageSource.getMessage(
"format_choice",
new Object[]{10000L},
Locale.forLanguageTag("en")
));
assertEquals("There is one file.", messageSource.getMessage(
"format_choice",
new Object[]{1},
Locale.forLanguageTag("en")
));

assertEquals("Es gibt 10.000 Dateien.", messageSource.getMessage(
"format_choice",
new Object[]{10000L},
Locale.forLanguageTag("de")
));
assertEquals("Es gibt eine Datei.", messageSource.getMessage(
"format_choice",
new Object[]{1},
Locale.forLanguageTag("de")
));
}

@ParameterizedTest(name = "{index} => translationUnitIdentifiers={0}, code={1}, expected={2}, targetValue={3}")
@MethodSource("dataProvider_setTranslationUnitIdentifiersOrdering")
void test_setTranslationUnitIdentifiersOrdering(ArrayList<String> translationUnitIdentifiers, String code, String expected) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/translations/messages.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
Target
</target>
</trans-unit>
<trans-unit id="format_choice">
<source>
<![CDATA[There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.]]></source>
<target>
<![CDATA[There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.]]></target>
</trans-unit>
</body>
</file>
</xliff>
6 changes: 6 additions & 0 deletions src/test/resources/translations/messages_de.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
{0} und {1}
</target>
</trans-unit>
<trans-unit id="format_choice">
<source>
<![CDATA[There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.]]></source>
<target>
<![CDATA[Es gibt {0,choice,0#keine Datei|1#eine Datei|1<{0,number,integer} Dateien}.]]></target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 491ca71

Please sign in to comment.