Skip to content

Commit

Permalink
Fix MessageBundle key/file name resolver algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Apr 10, 2024
1 parent 510e89e commit dcec876
Show file tree
Hide file tree
Showing 9 changed files with 179 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;
}

private 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,74 @@
package io.quarkus.qute.deployment.i18n;

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

public class EmailBundles {
@MessageBundle
interface template {
@Message
String title();

String logoUrl();

@Message
String signDocument();

@Message
String viewDocument();

@Message
String addToContacts(String email);

@Message
String about();

@Message
String help();

@Message
String legalInfo();

@Message
String spam();
}

@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,40 @@
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")
.addAsResource("messages/EmailBundles_template.properties")
.addAsResource("messages/EmailBundles_template_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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
addToContacts=Para asegurarte que llegan nuestros correos electrónicos, añade.
logoUrl=https://localhost
title=title
viewDocument=View Document
about=ABOUT
help=HELP
legalInfo=LEGAL CONDITIONS
spam=SPAM
signDocument=Sign Document
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title=title
logoUrl=localhost
viewDocument=View Document
addToContacts=Make sure that our emails are delivered adding the following address
spam=DENUNCIAR TRANSFERENCIA COMO SPAM
legalInfo=LEGAL CONDITIONS
help=AYUDA
about=ABOUT
signDocument=Sign Document

0 comments on commit dcec876

Please sign in to comment.