-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional internationalization support
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
honey-common/src/dev/shiza/honey/message/key/MessageKeyProcessingException.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,14 @@ | ||
package dev.shiza.honey.message.key; | ||
|
||
import java.util.Locale; | ||
|
||
public final class MessageKeyProcessingException extends IllegalArgumentException { | ||
|
||
public MessageKeyProcessingException(final Locale locale) { | ||
super("Could not resolve message for language with tag %s.".formatted(locale)); | ||
} | ||
|
||
public MessageKeyProcessingException(final String key) { | ||
super("Could not resolve message with key %s.".formatted(key)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
honey-common/src/dev/shiza/honey/message/key/MessageKeyProcessor.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,27 @@ | ||
package dev.shiza.honey.message.key; | ||
|
||
import dev.shiza.honey.processor.Processor; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public final class MessageKeyProcessor implements Processor { | ||
|
||
private static final Pattern MESSAGE_PATTERN = Pattern.compile("\\[\\[(\\w+):([^]]+)]]"); | ||
private final MessageSource messageSource; | ||
|
||
public MessageKeyProcessor(final MessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
@Override | ||
public String process(final String content) { | ||
final Matcher matcher = MESSAGE_PATTERN.matcher(content); | ||
if (matcher.find()) { | ||
final Locale locale = Locale.forLanguageTag(matcher.group(1)); | ||
final String key = matcher.group(2); | ||
return messageSource.message(locale, key); | ||
} | ||
return content; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
honey-common/src/dev/shiza/honey/message/key/MessageSource.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,11 @@ | ||
package dev.shiza.honey.message.key; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public interface MessageSource { | ||
|
||
void register(final Locale locale, final Map<String, String> messages); | ||
|
||
String message(final Locale locale, final String key); | ||
} |