-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented with a big bad singleton. It features namespaces, though.
- Loading branch information
Showing
15 changed files
with
163 additions
and
21 deletions.
There are no files selected for viewing
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,7 @@ | ||
package ppke.itk.xplang.common; | ||
|
||
/** | ||
* A big bad singleton encapsulating a resource bundle. | ||
*/ | ||
public class Messages { | ||
} |
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,55 @@ | ||
package ppke.itk.xplang.common; | ||
|
||
import java.util.*; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
public final class Translator { | ||
private final static Set<String> LANGUAGES = new HashSet<>(asList("", "hu", "en")); | ||
private final static String DEFAULT_LANGUAGE = ""; | ||
|
||
private static final Map<String, Translator> instances = new HashMap<>(); | ||
private static String language = DEFAULT_LANGUAGE; | ||
|
||
private ResourceBundle messages; | ||
private Translator(String namespace) { | ||
reset(namespace); | ||
} | ||
|
||
private void reset(String namespace) { | ||
Locale locale = new Locale(language); | ||
messages = ResourceBundle.getBundle( | ||
String.format("messages.%s", namespace), | ||
locale, | ||
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES) | ||
); | ||
} | ||
|
||
public String translate(String key) { | ||
return messages.getString(key); | ||
} | ||
|
||
public String translate(String key, Object... args) { | ||
return String.format(messages.getString(key), args); | ||
} | ||
|
||
public static Translator getInstance(String namespace) { | ||
namespace = namespace.toLowerCase(); | ||
if(!instances.containsKey(namespace)) { | ||
instances.put(namespace, new Translator(namespace)); | ||
} | ||
|
||
return instances.get(namespace); | ||
} | ||
|
||
public static void setLanguage(String newLanguage) { | ||
if(!LANGUAGES.contains(newLanguage)) { | ||
throw new IllegalStateException(String.format("Language '%s' is not supported", language)); | ||
} | ||
language = newLanguage; | ||
|
||
for(Map.Entry<String, Translator> instance : instances.entrySet()) { | ||
instance.getValue().reset(instance.getKey()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
package ppke.itk.xplang.parser; | ||
|
||
import ppke.itk.xplang.common.Translator; | ||
|
||
/** | ||
* An error of the {@link Lexer}. Thrown when the Lexer encounters a piece of text it cannot match to any of the | ||
* {@link Symbol}s it knows. | ||
*/ | ||
public class LexerError extends ParseError { | ||
private final static Translator translator = Translator.getInstance("parser"); | ||
|
||
/** | ||
* Signal a lexing error. | ||
* @param token Lexer is supposed to return the rest of the line, starting from the point of error. | ||
*/ | ||
LexerError(Token token) { | ||
super(String.format("Could not tokenize '%s'", token.lexeme()), token.location()); | ||
super(translator.translate("parser.LexerError.message", token.lexeme()), token.location()); | ||
} | ||
} |
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
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,6 @@ | ||
parser.NameClashError.message=Could not declare '%s': name already taken. | ||
parser.LexerError.message=Could not tokenize '%s' | ||
parser.NameError.message='%s' does not exist. | ||
parser.SyntaxError.message.expectMany=Expected any symbol of %s, encountered %s | ||
parser.SyntaxError.message.expectOne=Expected symbol %s, encountered %s | ||
|
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,6 @@ | ||
parser.NameClashError.message=A '%s' név már használatban van. | ||
parser.LexerError.message=Ismeretlen szimbólum: '%s' | ||
parser.NameError.message='%s' nevű objektum nem létezik. | ||
parser.SyntaxError.message.expectMany=A következő szimbólumok valamelyikére számítottam: %s, helyette azt kaptam, hogy %s | ||
parser.SyntaxError.message.expectOne=Arra a szimbólura számítottam, hogy %s, de azt kaptam, hogy %s | ||
|
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,5 @@ | ||
plang.program_keyword_missing=The program must start with the %s keyword | ||
plang.missing_program_name=The name of the program (an identifier) is missing. | ||
plang.missing_end_program=The program must end with the %s keyword | ||
plang.missing_declarations_keyword=The %s keyword is missing | ||
plang.missing_colon_after_declarations_keyword=Missing colon after the %s keyword |
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,5 @@ | ||
plang.program_keyword_missing=A programnak a %s kulcsszóval kell keződnie! | ||
plang.missing_program_name=Hiányzik a program neve (egy azonosító). | ||
plang.missing_end_program=A programot a %s kulcsszóval kell lezárni. | ||
plang.missing_declarations_keyword=Hiányzik a %s kulcsszó | ||
plang.missing_colon_after_declarations_keyword=Hiányzik a %s kulcsszó után a kettőspont. |
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,28 @@ | ||
package ppke.itk.xplang.common; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TranslatorTest { | ||
@Test public void translatorShouldParametrizeMessage() { | ||
Translator.setLanguage(""); | ||
Translator translator = Translator.getInstance("test"); | ||
assertEquals("TEST 1", translator.translate("parser.parametrized.message", 1)); | ||
} | ||
|
||
@Test public void translatorShouldFallBackToDefault() { | ||
Translator.setLanguage("hu"); | ||
Translator translator = Translator.getInstance("test"); | ||
|
||
assertEquals("TEST_FALLBACK", translator.translate("parser.missing.message")); | ||
} | ||
|
||
@Test public void switchingLanguagesShouldChangeInstances() { | ||
Translator.setLanguage(""); | ||
Translator translator = Translator.getInstance("test"); | ||
|
||
Translator.setLanguage("hu"); | ||
assertEquals("DIFO_HU", translator.translate("parser.simple.message")); | ||
} | ||
} |
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,3 @@ | ||
parser.simple.message=DIFO | ||
parser.parametrized.message=TEST %s | ||
parser.missing.message=TEST_FALLBACK |
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,2 @@ | ||
parser.simple.message=DIFO_HU | ||
parser.parametrized.message=TEST HU %s |