From f03cac2b477d859b1f5f9725b52c19ecf463e5cb Mon Sep 17 00:00:00 2001 From: Dan Galpin Date: Sun, 24 May 2015 16:16:35 -0700 Subject: [PATCH] Fixed some l8n around weather conditions We were fetching description from the server as a string instead of using the weather ID and an internal string that can be translated. --- .../android/sunshine/app/DetailFragment.java | 4 +- .../android/sunshine/app/ForecastAdapter.java | 9 +- .../example/android/sunshine/app/Utility.java | 178 ++++++++++++++++++ app/src/main/res/values/strings.xml | 59 ++++++ 4 files changed, 244 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java b/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java index 11b09d6..c298122 100644 --- a/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java +++ b/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java @@ -193,8 +193,8 @@ public class DetailFragment extends Fragment implements LoaderManager.LoaderCall mFriendlyDateView.setText(friendlyDateText); mDateView.setText(dateText); - // Read description from cursor and update view - String description = data.getString(COL_WEATHER_DESC); + // Get description from weather condition ID + String description = Utility.getStringForWeatherCondition(getActivity(), weatherId); mDescriptionView.setText(description); // For accessibility, add a content description to the icon field diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java index ada11de..caab205 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java @@ -90,17 +90,18 @@ public class ForecastAdapter extends CursorAdapter { ViewHolder viewHolder = (ViewHolder) view.getTag(); int viewType = getItemViewType(cursor.getPosition()); + int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID); switch (viewType) { case VIEW_TYPE_TODAY: { // Get weather icon viewHolder.iconView.setImageResource(Utility.getArtResourceForWeatherCondition( - cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID))); + weatherId)); break; } case VIEW_TYPE_FUTURE_DAY: { // Get weather icon viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition( - cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID))); + weatherId)); break; } } @@ -110,8 +111,8 @@ public class ForecastAdapter extends CursorAdapter { // Find TextView and set formatted date on it viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis)); - // Read weather forecast from cursor - String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC); + // Get description from weather condition ID + String description = Utility.getStringForWeatherCondition(context, weatherId); // Find TextView and set weather forecast on it viewHolder.descriptionView.setText(description); diff --git a/app/src/main/java/com/example/android/sunshine/app/Utility.java b/app/src/main/java/com/example/android/sunshine/app/Utility.java index d547e0d..2e76fb7 100644 --- a/app/src/main/java/com/example/android/sunshine/app/Utility.java +++ b/app/src/main/java/com/example/android/sunshine/app/Utility.java @@ -251,6 +251,184 @@ public class Utility { return -1; } + /** + * Helper method to provide the string according to the weather + * condition id returned by the OpenWeatherMap call. + * @param context Android context + * @param weatherId from OpenWeatherMap API response + * @return string for the weather condition. null if no relation is found. + */ + public static String getStringForWeatherCondition(Context context, int weatherId) { + // Based on weather code data found at: + // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes + int stringId; + if (weatherId >= 200 && weatherId <= 232) { + stringId = R.string.condition_2xx; + } else if (weatherId >= 300 && weatherId <= 321) { + stringId = R.string.condition_3xx; + } else switch(weatherId) { + case 500: + stringId = R.string.condition_500; + break; + case 501: + stringId = R.string.condition_501; + break; + case 502: + stringId = R.string.condition_502; + break; + case 503: + stringId = R.string.condition_503; + break; + case 504: + stringId = R.string.condition_504; + break; + case 511: + stringId = R.string.condition_511; + break; + case 520: + stringId = R.string.condition_520; + break; + case 531: + stringId = R.string.condition_531; + break; + case 600: + stringId = R.string.condition_600; + break; + case 601: + stringId = R.string.condition_601; + break; + case 602: + stringId = R.string.condition_602; + break; + case 611: + stringId = R.string.condition_611; + break; + case 612: + stringId = R.string.condition_612; + break; + case 615: + stringId = R.string.condition_615; + break; + case 616: + stringId = R.string.condition_616; + break; + case 620: + stringId = R.string.condition_620; + break; + case 621: + stringId = R.string.condition_621; + break; + case 622: + stringId = R.string.condition_622; + break; + case 701: + stringId = R.string.condition_701; + break; + case 711: + stringId = R.string.condition_711; + break; + case 721: + stringId = R.string.condition_721; + break; + case 731: + stringId = R.string.condition_731; + break; + case 741: + stringId = R.string.condition_741; + break; + case 751: + stringId = R.string.condition_751; + break; + case 761: + stringId = R.string.condition_761; + break; + case 762: + stringId = R.string.condition_762; + break; + case 771: + stringId = R.string.condition_771; + break; + case 781: + stringId = R.string.condition_781; + break; + case 800: + stringId = R.string.condition_800; + break; + case 801: + stringId = R.string.condition_801; + break; + case 802: + stringId = R.string.condition_802; + break; + case 803: + stringId = R.string.condition_803; + break; + case 804: + stringId = R.string.condition_804; + break; + case 900: + stringId = R.string.condition_900; + break; + case 901: + stringId = R.string.condition_901; + break; + case 902: + stringId = R.string.condition_902; + break; + case 903: + stringId = R.string.condition_903; + break; + case 904: + stringId = R.string.condition_904; + break; + case 905: + stringId = R.string.condition_905; + break; + case 906: + stringId = R.string.condition_906; + break; + case 951: + stringId = R.string.condition_951; + break; + case 952: + stringId = R.string.condition_952; + break; + case 953: + stringId = R.string.condition_953; + break; + case 954: + stringId = R.string.condition_954; + break; + case 955: + stringId = R.string.condition_955; + break; + case 956: + stringId = R.string.condition_956; + break; + case 957: + stringId = R.string.condition_957; + break; + case 958: + stringId = R.string.condition_958; + break; + case 959: + stringId = R.string.condition_959; + break; + case 960: + stringId = R.string.condition_960; + break; + case 961: + stringId = R.string.condition_961; + break; + case 962: + stringId = R.string.condition_962; + break; + default: + return context.getString(R.string.condition_unknown, weatherId); + } + return context.getString(stringId); + } + /** * Returns true if the network is available or about to become available. * diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8b3a6bb..4ee036f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,4 +123,63 @@ No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine. No weather information available. The location in settings is not recognized by the weather server. + + Storm + Drizzle + Light Rain + Moderate Rain + Heavy Rain + Intense Rain + Extreme Rain + Freezing Rain + Light Shower + Shower + Heavy Shower + Ragged Shower + Light Snow + Snow + Heavy Snow + Sleet + Shower Sleet + Rain and Snow + Rain and Snow + Shower Snow + Shower Snow + Shower Snow + Mist + Smoke + Haze + Sand, Dust + Fog + Sand + Dust + Volcanic Ash + Squalls + Tornado + Clear + Mostly Clear + Scattered Clouds + Broken Clouds + Overcast Clouds + Tornado + Tropical Storm + Hurricane + Cold + Hot + Windy + Hail + Calm + Light Breeze + Gentle Breeze + Breeze + Fresh Breeze + Strong Breeze + High Wind + Gale + Severe Gale + Storm + Violent Storm + Hurricane + + Unknown (%1$s) \ No newline at end of file