diff --git a/src/main/java/org/translation/JSONTranslationExample.java b/src/main/java/org/translation/JSONTranslationExample.java index 1483638c4..0400f1a31 100644 --- a/src/main/java/org/translation/JSONTranslationExample.java +++ b/src/main/java/org/translation/JSONTranslationExample.java @@ -1,4 +1,4 @@ -package org.translation; +package java.org.translation; import java.io.IOException; import java.net.URISyntaxException; @@ -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) { @@ -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"); } @@ -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. diff --git a/src/main/resources/country-codes.txt b/src/main/resources/country-codes.txt index e08f1c4c7..8f11fdefc 100644 --- a/src/main/resources/country-codes.txt +++ b/src/main/resources/country-codes.txt @@ -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 \ No newline at end of file +Åland Islands AX ALA 249 \ No newline at end of file