From d40fbb776c4a9601d19b36ab268cb3359acc7617 Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Sat, 30 Mar 2019 16:02:38 -0700 Subject: [PATCH] + Added localization to 4 files. Revised format to use files as sections. --- phrases.config | 21 ++++++++++++ src/commands/CommandBeansShow.java | 6 ++-- src/commands/CommandBetBeans.java | 14 ++++---- src/commands/CommandBlurry.java | 3 +- src/commands/CommandBoop.java | 10 +++--- src/core/Localizer.java | 52 +++++++++++++++++++++--------- src/main/Main.java | 11 ++++++- 7 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 phrases.config diff --git a/phrases.config b/phrases.config new file mode 100644 index 0000000..b810ae8 --- /dev/null +++ b/phrases.config @@ -0,0 +1,21 @@ +[CommandBeansShow] +Displays how many beans you have = +You have %s beans! = + +[CommandBetBeans] +Please bet with at least 50 beans! = +Allows you to bet your beans, set your amount after the command! Warning! House always wins!\nSets of 3 will get 5x, 4 will get 10x, and 5 will get 1000x = +That's not a valid bet! = +Sorry, you didn't win, try again! = +You don't have enough beans! = +You won %s beans! = + +[CommandBoop] +%s booped %s! = +Kitty will react with a counter = +Woah! %s booped me! That's %s total! = +%s booped several others - %s! = + +[CommandBlurry] +Blurs your icon or the icon of a friend you mentioned = + diff --git a/src/commands/CommandBeansShow.java b/src/commands/CommandBeansShow.java index e1c63ca..e832ddb 100644 --- a/src/commands/CommandBeansShow.java +++ b/src/commands/CommandBeansShow.java @@ -1,6 +1,7 @@ package commands; import core.Command; +import core.Localizer; import dataStructures.*; public class CommandBeansShow extends Command @@ -8,12 +9,11 @@ public class CommandBeansShow extends Command public CommandBeansShow(KittyRole level, KittyRating rating) { super(level, rating); } @Override - public String HelpText() { return "Displays how many beans you have"; }; + public String HelpText() { return Localizer.Stub("Displays how many beans you have"); }; @Override public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { - String beans = "You have " + user.GetBeans() + " beans!"; - res.Call(beans); + res.Call(String.format(Localizer.Stub("You have %s beans!"), user.GetBeans())); } } diff --git a/src/commands/CommandBetBeans.java b/src/commands/CommandBetBeans.java index 2ffc986..2019ee0 100644 --- a/src/commands/CommandBetBeans.java +++ b/src/commands/CommandBetBeans.java @@ -1,6 +1,7 @@ package commands; import core.Command; +import core.Localizer; import dataStructures.*; public class CommandBetBeans extends Command @@ -8,8 +9,7 @@ public class CommandBetBeans extends Command public CommandBetBeans(KittyRole level, KittyRating rating) { super(level, rating); } @Override - public String HelpText() { return "Allows you to bet your beans, set your amount after the command! Warning: House always wins!" - + "\nSets of 3 will get 5x, 4 will get 10x, and 5 will get 1000x"; } + public String HelpText() { return Localizer.Stub("Allows you to bet your beans, set your amount after the command! Warning! House always wins!\nSets of 3 will get 5x, 4 will get 10x, and 5 will get 1000x"); } @Override public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) @@ -22,19 +22,19 @@ public class CommandBetBeans extends Command bet = Integer.parseInt(input.args); if(bet < 50) { - res.Call("Please bet with at least 50 beans!"); + res.Call(Localizer.Stub("Please bet with at least 50 beans!")); return; } } catch (NumberFormatException e) { - res.Call("That's not a valid bet!"); + res.Call(Localizer.Stub("That's not a valid bet!")); return; } if(user.GetBeans() < bet) { - res.Call("You don't have enough beans!"); + res.Call(Localizer.Stub("You don't have enough beans!")); return; } @@ -49,12 +49,12 @@ public class CommandBetBeans extends Command if(win == 0) { - res.Call("Sorry, you didn't win, try again!"); + res.Call(Localizer.Stub("Sorry, you didn't win, try again!")); return; } user.ChangeBeans(bet*win); - res.Call("You won " + (bet*win) + " beans!"); + res.Call(String.format(Localizer.Stub("You won %s beans!"), "" + (bet*win))); } private int getWinning(int [] slots) diff --git a/src/commands/CommandBlurry.java b/src/commands/CommandBlurry.java index ed67ca7..5bb6ef5 100644 --- a/src/commands/CommandBlurry.java +++ b/src/commands/CommandBlurry.java @@ -8,6 +8,7 @@ import java.io.IOException; import javax.imageio.ImageIO; import core.Command; +import core.Localizer; import dataStructures.*; import utils.ImageUtils; @@ -16,7 +17,7 @@ public class CommandBlurry extends Command public CommandBlurry(KittyRole level, KittyRating rating) { super(level, rating); } @Override - public String HelpText() { return "Blurs your icon or the icon of a friend you mentioned"; }; + public String HelpText() { return Localizer.Stub("Blurs your icon or the icon of a friend you mentioned"); }; private static Long num = 0l; diff --git a/src/commands/CommandBoop.java b/src/commands/CommandBoop.java index 6ecdda9..df81882 100644 --- a/src/commands/CommandBoop.java +++ b/src/commands/CommandBoop.java @@ -60,7 +60,7 @@ public class CommandBoop extends Command } @Override - public String HelpText() { return "Kitty will react with a counter"; } + public String HelpText() { return Localizer.Stub("Kitty will react with a counter"); } // Called when the command is run! @Override @@ -69,16 +69,17 @@ public class CommandBoop extends Command if(input.mentions == null) { boopTracker.ApplyBoop(); - res.Call("Woah! " + user.name + " booped me! That's " + boopTracker.HowMany() + " total!"); + res.Call(String.format(Localizer.Stub("Woah! %s booped me! That's %s total!"), user.name, boopTracker.HowMany())); } else { if(input.mentions.length == 1) { boopTracker.ApplyBoop(); - res.Call(user.name + " booped " + input.mentions[0].name + "!"); + res.Call(String.format(Localizer.Stub("%s booped %s!"), user.name, input.mentions[0].name)); return; } + String booped = ""; for(int i = 0; i < input.mentions.length; i++) { @@ -88,7 +89,8 @@ public class CommandBoop extends Command else booped += "and " + input.mentions[i].name; } - res.Call(user.name + " booped " + booped + "!"); + + res.Call(String.format(Localizer.Stub("%s booped several others - %s!"), user.name, booped)); } } } \ No newline at end of file diff --git a/src/core/Localizer.java b/src/core/Localizer.java index 8da5b7b..fe1973f 100644 --- a/src/core/Localizer.java +++ b/src/core/Localizer.java @@ -2,6 +2,7 @@ package core; import java.io.File; import java.io.IOException; +import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -21,10 +22,9 @@ public class Localizer // Filename public final static String filename = "phrases.config"; public final static String functionName = "Localizer.Stub"; - public final static String section = "phrases"; // Local translation - private static HashMap translated = new HashMap(); + private static HashMap translated = new HashMap(); // Logging private static void Log(String str) { System.out.println("[Log] " + str); } @@ -68,10 +68,23 @@ public class Localizer return items; } - // Do processing on each path in the scraped directory here, assuming it's .java - public static void StripForContents(Path path, ArrayList strings) + public static class LocPair { - if(path.getFileName().toString().contains(".java")) + public String file; + public String phrase; + + public LocPair(String f, String p) + { + this.file = f; + this.phrase = p; + } + } + + // Do processing on each path in the scraped directory here, assuming it's .java + public static void StripForContents(Path path, ArrayList strings) + { + String filename = path.getFileName().toString(); + if(filename.contains(".java")) { String contents = ReadContent(path); String[] split = contents.split(functionName); @@ -91,7 +104,8 @@ public class Localizer { String toLocalize = split[i].substring(2, loc - 1); - strings.add(toLocalize); + strings.add(new LocPair(filename.substring(0, filename.lastIndexOf('.')), toLocalize)); + Log("Found stubbed phrase in " + path + " : " + toLocalize); } catch(IndexOutOfBoundsException e) @@ -110,7 +124,7 @@ public class Localizer { if(translated.containsKey(input)) { - String value = translated.get(input); + String value = translated.get(input).phrase; if(value.trim().length() > 0) return value; } @@ -136,7 +150,7 @@ public class Localizer for(String s : sec.keySet()) { if(!translated.containsKey(s)) - translated.put(s, sec.get(s, String.class)); + translated.put(s, new LocPair(sec.getName(), sec.get(s, String.class))); } } } @@ -157,14 +171,20 @@ public class Localizer Log("Attempting to write updated localization file"); try { + PrintWriter pw = new PrintWriter(filename); + pw.close(); + File file = new File(filename); file.createNewFile(); - + Wini ini = new Wini(file); for(String s : translated.keySet()) - ini.put(section, s, translated.get(s)); - + { + LocPair toWrite = translated.get(s); + ini.put(toWrite.file, s, toWrite.phrase); + } + ini.store(); } catch(InvalidFileFormatException e) @@ -181,15 +201,15 @@ public class Localizer // This stubs out phrases to be localized. public static void ScrapeAll() { - ArrayList localizeList = new ArrayList(); + ArrayList localizeList = new ArrayList(); AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList)); - for(String toStub : localizeList) + for(LocPair toStub : localizeList) { - if(!translated.containsKey(toStub)) + if(!translated.containsKey(toStub.phrase)) { - translated.put(toStub, ""); - Log(" Found new stubbed phrase '" + toStub + "'"); + translated.put(toStub.phrase, new LocPair(toStub.file, "")); + Log("Found new stubbed phrase '" + toStub.phrase + "' in " + toStub.file); } } } diff --git a/src/main/Main.java b/src/main/Main.java index 1faa78a..50ae429 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -14,6 +14,7 @@ import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.guild.*; import net.dv8tion.jda.core.hooks.ListenerAdapter; import offline.*; +import core.Localizer; import utils.GlobalLog; import net.dv8tion.jda.core.*; @@ -26,15 +27,23 @@ public class Main extends ListenerAdapter private static CommandManager commandManager; private static DatabaseManager databaseManager; private static Stats stats; + private static RPManager rpManager; // Main test location public static void main(String[] args) throws InterruptedException, LoginException, Exception { + // Facotry startup. databaseManager = ObjectBuilderFactory.ConstructDatabaseManager(); commandManager = ObjectBuilderFactory.ConstructCommandManager(); - ObjectBuilderFactory.ConstructRPManager(); + rpManager = ObjectBuilderFactory.ConstructRPManager(); stats = ObjectBuilderFactory.ConstructStats(commandManager); + // Localizer startup - Potentially integrate with the factory. + Localizer.UpdateLocFromDisk(); + Localizer.ScrapeAll(); + Localizer.SaveLocToDisk(); + + // Bot startup kitty = new JDABuilder(AccountType.BOT).setToken(Ref.TestToken).buildBlocking(); kitty.getPresence().setGame(Game.playing("with a new build")); kitty.addEventListener(new Main());