Skip to content
Snippets Groups Projects
Commit 3ec24460 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Started internationalization.

- Introduced English-language properties file
- connect() method uses ResourceBundle

Partially addresses #6
parent 2e5c98ac
Branches
No related tags found
No related merge requests found
......@@ -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.
......@@ -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;
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment