-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from kbss-cvut/bug#254-import-does-not-create…
…-document [Bug #254] Ensure document is generated for new vocabulary when it is not provided
- Loading branch information
Showing
7 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/cz/cvut/kbss/termit/service/MessageFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cz.cvut.kbss.termit.service; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import java.util.Objects; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* Formats internationalized messages. | ||
* <p> | ||
* The message bundles should be located in {@code src/main/resources/i18n} and be called {@code messages}. | ||
*/ | ||
public class MessageFormatter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MessageFormatter.class); | ||
|
||
private final ResourceBundle messages; | ||
|
||
public MessageFormatter(@NonNull String lang) { | ||
Objects.requireNonNull(lang); | ||
final Locale locale = new Locale(lang); | ||
this.messages = ResourceBundle.getBundle("i18n/messages", locale); | ||
LOG.info("Loaded message bundle '{}_{}'.", messages.getBaseBundleName(), locale); | ||
} | ||
|
||
/** | ||
* Formats message with the specified identifier using the specified parameters. | ||
* | ||
* @param messageId Message identifier | ||
* @param params Parameters to substitute into the message string | ||
* @return Formatted message | ||
*/ | ||
public String formatMessage(@NonNull String messageId, Object... params) { | ||
Objects.requireNonNull(messageId); | ||
try { | ||
final MessageFormat formatter = new MessageFormat(messages.getString(messageId)); | ||
return formatter.format(params); | ||
} catch (MissingResourceException e) { | ||
LOG.error("No message found for message id '{}'. Returning the message id.", messageId, e); | ||
return messageId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vocabulary.document.label=Dokument pro {0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vocabulary.document.label=Document for {0} |
40 changes: 40 additions & 0 deletions
40
src/test/java/cz/cvut/kbss/termit/service/MessageFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cz.cvut.kbss.termit.service; | ||
|
||
import cz.cvut.kbss.termit.environment.Environment; | ||
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 java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class MessageFormatterTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("i18nFormattingValues") | ||
void formatsMessageWithSpecifiedIdUsingSpecifiedValues(String language, String expectedMessage) { | ||
final MessageFormatter sut = new MessageFormatter(language); | ||
final String vocabularyLabel = "Test"; | ||
assertEquals(expectedMessage, sut.formatMessage("vocabulary.document.label", vocabularyLabel)); | ||
} | ||
|
||
static Stream<Arguments> i18nFormattingValues() { | ||
return Stream.of( | ||
Arguments.of("en", "Document for Test"), | ||
Arguments.of("cs", "Dokument pro Test") | ||
); | ||
} | ||
|
||
@Test | ||
void formatMessageReturnsMessageIdAndLogsErrorWhenMessageIsNotFound() { | ||
final String unknownMessageId = "unknownMessage"; | ||
final MessageFormatter sut = new MessageFormatter(Environment.LANGUAGE); | ||
assertDoesNotThrow(() -> { | ||
final String result = sut.formatMessage(unknownMessageId, "12345"); | ||
assertEquals(unknownMessageId, result); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters