From 3ec24460c476cf794660daa7ff24a863e887525e Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Thu, 7 Nov 2019 08:37:21 -0600 Subject: [PATCH] Started internationalization. - Introduced English-language properties file - connect() method uses ResourceBundle Partially addresses #6 --- README.md | 3 +-- .../edu/unl/cse/csce361/socket_chat/Chat.java | 16 ++++++++++++---- src/main/resources/.gitkeep | 0 src/main/resources/socketchat_en.properties | 4 ++++ 4 files changed, 17 insertions(+), 6 deletions(-) delete mode 100644 src/main/resources/.gitkeep create mode 100644 src/main/resources/socketchat_en.properties diff --git a/README.md b/README.md index 0a07ee5..2c0c327 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,4 @@ Socket Chat is a simple 2-way chat program, essentially a bi-directional echo server/client. Only one socket is set up, so only two chatters are possible. -It is remarkably non-robust, as its sole purpose is to demonstrate the use of -Java sockets. + diff --git a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java index 9f5db31..52dace6 100644 --- a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java +++ b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java @@ -3,6 +3,8 @@ package edu.unl.cse.csce361.socket_chat; import java.io.*; import java.net.*; import java.util.InputMismatchException; +import java.util.Locale; +import java.util.ResourceBundle; import java.util.Scanner; import static java.lang.Thread.sleep; @@ -12,8 +14,10 @@ public class Chat { private static final int MAXIMUM_CONNECTION_ATTEMPTS = 10; private Socket socket; private boolean isHost; + private ResourceBundle bundle; public Chat() { + bundle = ResourceBundle.getBundle("socketchat", Locale.US); socket = connect(new Scanner(System.in)); } @@ -21,15 +25,19 @@ public class Chat { @SuppressWarnings("WeakerAccess") public Socket connect(Scanner userInput) { - System.out.print("Are you the chat host? [Y] "); + String yes = bundle.getString("connection.responses.yes"); + String no = bundle.getString("connection.responses.no"); + // "Are you the chat host? [Y] " + System.out.print(bundle.getString("connection.prompts.isHost") + " [" + yes + "] "); String answerString = userInput.nextLine().toUpperCase(); - char answer = answerString.length() > 0 ? answerString.charAt(0) : 'Y'; - isHost = (answer != 'N'); + String answer = answerString.length() > 0 ? answerString.substring(0, no.length()) : yes; + isHost = (!answer.equals(no)); Socket socket = null; try { socket = isHost ? connectAsServer(userInput) : connectAsClient(userInput); } catch (IOException ioException) { - System.err.println("Connection failed: " + ioException); + // "Connection failed" + System.err.println(bundle.getString("connection.errors.generalConnectionFailure") + ": " + ioException); System.exit(1); } return socket; diff --git a/src/main/resources/.gitkeep b/src/main/resources/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/socketchat_en.properties b/src/main/resources/socketchat_en.properties new file mode 100644 index 0000000..c244be2 --- /dev/null +++ b/src/main/resources/socketchat_en.properties @@ -0,0 +1,4 @@ +connection.errors.generalConnectionFailure = Connection failed +connection.prompts.isHost = Are you the chat host? +connection.responses.yes = Y +connection.responses.no = N \ No newline at end of file -- GitLab