Skip to content

Commit

Permalink
Merge pull request #39990 from gastaldi/qute_locale
Browse files Browse the repository at this point in the history
Fix MessageBundle key/file name resolver algorithm
  • Loading branch information
mkouba authored Apr 12, 2024
2 parents e2bc9a4 + 463a411 commit 08d36bd
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
Map<String, Path> localeToMergeCandidate = new HashMap<>();
for (Path messageFile : messageFiles) {
String fileName = messageFile.getFileName().toString();
if (fileName.startsWith(name)) {
if (bundleNameMatchesFileName(fileName, name)) {
final String locale;
int postfixIdx = fileName.indexOf('.');
if (postfixIdx == name.length()) {
Expand Down Expand Up @@ -315,6 +315,30 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
return bundles;
}

static boolean bundleNameMatchesFileName(String fileName, String name) {
int fileSeparatorIdx = fileName.indexOf('.');
// Remove file extension if exists
if (fileSeparatorIdx > -1) {
fileName = fileName.substring(0, fileSeparatorIdx);
}
// Split the filename and the bundle name by underscores
String[] fileNameParts = fileName.split("_");
String[] nameParts = name.split("_");

if (fileNameParts.length < nameParts.length) {
return false;
}

// Compare each part of the filename with the corresponding part of the bundle name
for (int i = 0; i < nameParts.length; i++) {
if (!fileNameParts[i].equals(nameParts[i])) {
return false;
}
}

return true;
}

@Record(value = STATIC_INIT)
@BuildStep
void initBundleContext(MessageBundleRecorder recorder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.qute.deployment;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class MessageBundleProcessorTest {

@Test
void bundleNameMatchesFileName() {
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("messages.properties", "messages"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("started.properties", "started"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("startedValidation.properties", "startedValidation"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation.properties",
"EmailBundles_startedValidation"));
assertTrue(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation_pt_BR.properties",
"EmailBundles_startedValidation"));

assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("startedValidation.properties", "started"));
assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation.properties",
"EmailBundles_started"));
assertFalse(MessageBundleProcessor.bundleNameMatchesFileName("EmailBundles_startedValidation_pt_BR.properties",
"EmailBundles_started"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.qute.deployment.i18n;

import io.quarkus.qute.i18n.Message;
import io.quarkus.qute.i18n.MessageBundle;

public class EmailBundles {
@MessageBundle
interface started {
@Message
String started(String id, String filename);

@Message
String documentAccessUrl(String url);

@Message
String nextNotification();

@Message
String signingProcessStart(String id, String filename);

@Message
String subject(String customer, String filename);

@Message
String signForValidation();
}

@MessageBundle
interface startedValidator {
@Message
String started(String id, String filename);

@Message
String turnEmailWillBeSent();

@Message
String youMayAlreadyAccessDocument();

@Message
String subject(String customer, String filename);

@Message
String signForValidation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.qute.deployment.i18n;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Engine;
import io.quarkus.qute.i18n.MessageBundles;
import io.quarkus.test.QuarkusUnitTest;

public class MessageBundleNameCollisionTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.overrideConfigKey("quarkus.default-locale", "en_US")
.withApplicationRoot((jar) -> jar
.addClasses(EmailBundles.class)
.addAsResource("messages/EmailBundles_started.properties")
.addAsResource("messages/EmailBundles_started_en.properties")
.addAsResource("messages/EmailBundles_startedValidator.properties")
.addAsResource("messages/EmailBundles_startedValidator_en.properties"));

@Inject
Engine engine;

@Test
public void testBundleMethodIsFound() {
EmailBundles.startedValidator startedValidator = MessageBundles.get(EmailBundles.startedValidator.class);
assertEquals("You will be notified with another email when it is your turn to sign.",
startedValidator.turnEmailWillBeSent());
assertEquals("You will be notified with another email when it is your turn to sign.",
engine.parse("{EmailBundles_startedValidator:turnEmailWillBeSent()}").render());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
started=In this process you will sign the document to validate it.
signingProcessStart=you have started a signing process {id} for document "{filename}".
nextNotification=You will be notified with another email when it is your signing turn.
documentAccessUrl=You may access the document in the following link:
subject=Signing process initiated by {customer} for file {filename}.
signForValidation=In this process you will sign the document to validate it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
signForValidation=In this process you will sign the document to validate it.
started=has started a signing process {id} for the document "{filename}".
subject=Signing process initiated by {customer} for file {filename}.
turnEmailWillBeSent=You will be notified with another email when it is your turn to sign.
youMayAlreadyAccessDocument=You can access the document at the following link:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
signForValidation=In this process you will sign the document to validate it.
started=has started a signing process {id} for the document "{filename}".
subject=Signing process initiated by {customer} for file {filename}.
turnEmailWillBeSent=You will be notified with another email when it is your turn to sign.
youMayAlreadyAccessDocument=You can access the document at the following link:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
started=In this process you will sign the document to validate it.
signingProcessStart=you have started a signing process {id} for document "{filename}".
nextNotification=You will be notified with another email when it is your signing turn.
documentAccessUrl=You may access the document in the following link:
subject=Signing process initiated by {customer} for file {filename}.
signForValidation=In this process you will sign the document to validate it.

0 comments on commit 08d36bd

Please sign in to comment.