Skip to content
Open

V #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/main/java/org/translation/JSONTranslationExample.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.translation;
package java.org.translation;

import java.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -23,7 +23,8 @@ public JSONTranslationExample() {
// which we then create a new JSONArray object from.
// TODO CheckStyle: Line is longer than 120 characters
// (note: you can split a line such that the next line starts with a .method()... call
String jsonString = Files.readString(Paths.get(getClass().getClassLoader().getResource("sample.json").toURI()));
String jsonString = Files.readString(Paths.get(getClass()
.getClassLoader().getResource("sample.json").toURI()));
this.jsonArray = new JSONArray(jsonString);
}
catch (IOException | URISyntaxException ex) {
Expand All @@ -38,7 +39,7 @@ public JSONTranslationExample() {
public String getCanadaCountryNameSpanishTranslation() {

// TODO Checkstyle: '30' is a magic number.
JSONObject canada = jsonArray.getJSONObject(30);
JSONObject canada = jsonArray.getJSONObject(CANADA_INDEX);
return canada.getString("es");
}

Expand All @@ -52,8 +53,16 @@ public String getCanadaCountryNameSpanishTranslation() {
* @return the translation of country to the given language or "Country not found" if there is no translation.
*/
public String getCountryNameTranslation(String countryCode, String languageCode) {
return "Country not found";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject country = jsonArray.getJSONObject(i);
if (countryCode.equalsIgnoreCase(country.getString("alpha3"))) {
if (country.has(languageCode)) {
return country.getString(languageCode);
}
}
}
return "Country not found";
}

/**
* Prints the Spanish translation of Canada.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/country-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,4 @@ Western Sahara EH ESH 732
Yemen YE YEM 887
Zambia ZM ZMB 894
Zimbabwe ZW ZWE 716
Åland Islands AX ALA 248
Åland Islands AX ALA 249