1- import java .util .HashMap ;
21import java .text .MessageFormat ;
2+ import java .util .HashMap ;
33
44public class I18n {
5- private static String [] allowedLocales = {"en" , "de" };
6- private static String locale = allowedLocales [0 ];
7- private static HashMap <String , HashMap <String , String >> messages ;
8-
9- public static void setup () {
10- messages = new HashMap <>();
11-
12- for (var allowedLocale : allowedLocales ) {
13- messages .put (allowedLocale , new HashMap <>());
14- }
15-
16- // see MessageFormat on how to write patterns: https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
17- var en = messages .get ("en" );
18- en .put ("item-found" , "You have found an item: {0}!" );
19- en .put ("scroll-water" , "Scroll of Water" );
20- en .put ("scroll-fire" , "Scroll of Fire" );
21- en .put ("tutorial-item-pickup" , "Press E to pick up an item!" );
22- en .put ("tutorial-scroll-fire" , "Press G to throw a fire ball!" );
23- en .put ("need-scroll-water" , "I probably need the water scroll. Maybe it is here somewhere!" );
24-
25-
26- var de = messages .get ("de" );
27- de .put ("item-found" , "Du hast einen Gegenstand gefunden: {0}!" );
28- de .put ("scroll-water" , "Schriftrolle des Wassers" );
29- de .put ("scroll-fire" , "Schriftrolle des Feuers" );
30- de .put ("tutorial-scroll-fire" , "Drücke G, um einen Feuerball zu werfen!" );
31- de .put ("tutorial-item-pickup" , "Drücke E, um das Item aufzuheben!" );
32- de .put ("need-scroll-water" , "Ich brauche Wahrscheinlich die Schriftrolle des Wassers. Vielleicht ist sie hier irgendwo?" );
33- }
34-
35- public static void select (String locale ) {
36- for (var allowedLocale : allowedLocales ) {
37- if (allowedLocale .equals (locale )) {
38- I18n .locale = locale ;
39- return ;
40- }
41- }
5+ private static String [] allowedLocales = {"en" , "de" };
6+ private static String locale = allowedLocales [0 ];
7+ private static HashMap <String , HashMap <String , String >> messages ;
8+
9+ public static void setup () {
10+ messages = new HashMap <>();
11+
12+ for (var allowedLocale : allowedLocales ) {
13+ messages .put (allowedLocale , new HashMap <>());
4214 }
43-
44- public static String get (String key , String ...args ) {
45- var message = messages .get (locale ).getOrDefault (key , "NOT_FOUND" );
46- return MessageFormat .format (message , args );
15+
16+ // see MessageFormat on how to write patterns:
17+ // https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
18+ var en = messages .get ("en" );
19+ en .put ("item-found" , "You have found an item: {0}!" );
20+ en .put ("scroll-water" , "Scroll of Water" );
21+ en .put ("scroll-fire" , "Scroll of Fire" );
22+ en .put ("tutorial-item-pickup" , "Press E to pick up an item!" );
23+ en .put ("tutorial-scroll-fire" , "Press G to throw a fire ball!" );
24+ en .put ("need-scroll-water" , "I probably need the water scroll. Maybe it is here somewhere!" );
25+
26+ var de = messages .get ("de" );
27+ de .put ("item-found" , "Du hast einen Gegenstand gefunden: {0}!" );
28+ de .put ("scroll-water" , "Schriftrolle des Wassers" );
29+ de .put ("scroll-fire" , "Schriftrolle des Feuers" );
30+ de .put ("tutorial-scroll-fire" , "Drücke G, um einen Feuerball zu werfen!" );
31+ de .put ("tutorial-item-pickup" , "Drücke E, um das Item aufzuheben!" );
32+ de .put (
33+ "need-scroll-water" ,
34+ "Ich brauche Wahrscheinlich die Schriftrolle des Wassers. Vielleicht ist sie hier"
35+ + " irgendwo?" );
36+ }
37+
38+ public static void select (String locale ) {
39+ for (var allowedLocale : allowedLocales ) {
40+ if (allowedLocale .equals (locale )) {
41+ I18n .locale = locale ;
42+ return ;
43+ }
4744 }
48- }
45+ }
46+
47+ public static String get (String key , String ... args ) {
48+ var message = messages .get (locale ).getOrDefault (key , "NOT_FOUND" );
49+ return MessageFormat .format (message , args );
50+ }
51+ }
0 commit comments