From 40f00af1964b60ffa72e680078e84524a35c014c Mon Sep 17 00:00:00 2001 From: chschtsch Date: Tue, 29 Dec 2015 17:35:51 +0300 Subject: [PATCH] refactor localization --- .../java/org/schabi/newpipe/Localization.java | 79 +++++++++++++++++++ .../newpipe/VideoInfoItemViewCreator.java | 6 +- .../newpipe/VideoItemDetailFragment.java | 63 +++------------ .../org/schabi/newpipe/VideoListAdapter.java | 2 +- 4 files changed, 93 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/Localization.java diff --git a/app/src/main/java/org/schabi/newpipe/Localization.java b/app/src/main/java/org/schabi/newpipe/Localization.java new file mode 100644 index 000000000..8b9689585 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/Localization.java @@ -0,0 +1,79 @@ +package org.schabi.newpipe; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.preference.PreferenceManager; + +import java.text.DateFormat; +import java.text.NumberFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Created by chschtsch on 12/29/15. + */ + +public class Localization { + + public static Context contextOfApplication = null; + + public static Locale getPreferredLocale(Context context) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + + String languageCode = sp.getString(String.valueOf(R.string.searchLanguage), "en"); + + if(languageCode.length() == 2) { + return new Locale(languageCode); + } + else if(languageCode.contains("_")) { + String country = languageCode + .substring(languageCode.indexOf("_"), languageCode.length()); + return new Locale(languageCode.substring(0, 2), country); + } + return Locale.getDefault(); + } + + public static String localizeViewCount(long viewCount, Context context) { + Locale locale = getPreferredLocale(context); + + Resources res = context.getResources(); + String viewsString = res.getString(R.string.viewCountText); + + NumberFormat nf = NumberFormat.getInstance(locale); + String formattedViewCount = nf.format(viewCount); + return String.format(viewsString, formattedViewCount); + } + + public static String localizeNumber(long number, Context context) { + Locale locale = getPreferredLocale(context); + NumberFormat nf = NumberFormat.getInstance(locale); + return nf.format(number); + } + + private static String formatDate(String date, Context context) { + Locale locale = getPreferredLocale(context); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + Date datum = null; + try { + datum = formatter.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + + DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); + + return df.format(datum); + } + + public static String localizeDate(String date, Context context) { + Resources res = context.getResources(); + String dateString = res.getString(R.string.uploadDateText); + + String formattedDate = formatDate(date, context); + return String.format(dateString, formattedDate); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java b/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java index b7eaa4285..c2bbb069e 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java +++ b/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java @@ -1,5 +1,6 @@ package org.schabi.newpipe; +import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -33,7 +34,7 @@ class VideoInfoItemViewCreator { this.inflater = inflater; } - public View getViewFromVideoInfoItem(View convertView, ViewGroup parent, VideoPreviewInfo info) { + public View getViewFromVideoInfoItem(View convertView, ViewGroup parent, VideoPreviewInfo info, Context context) { ViewHolder holder; if(convertView == null) { convertView = inflater.inflate(R.layout.video_item, parent, false); @@ -59,8 +60,7 @@ class VideoInfoItemViewCreator { if(!info.upload_date.isEmpty()) { holder.itemUploadDateView.setText(info.upload_date); } else { - //tweak if necessary: This is a hack to prevent having white space in the layout :P - holder.itemUploadDateView.setText(String.format("%d", info.view_count)); + holder.itemUploadDateView.setText(Localization.localizeViewCount(info.view_count, context)); } return convertView; diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java index 0173c07de..92c25e2aa 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java @@ -1,9 +1,7 @@ package org.schabi.newpipe; -import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; -import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -35,13 +33,7 @@ import android.view.MenuItem; import android.widget.Toast; import java.net.URL; -import java.text.DateFormat; -import java.text.NumberFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; -import java.util.Locale; import java.util.Vector; import org.schabi.newpipe.services.VideoExtractor; @@ -235,7 +227,7 @@ public class VideoItemDetailFragment extends Fragment { switch (info.errorCode) { case VideoInfo.NO_ERROR: { View nextVideoView = videoItemViewCreator - .getViewFromVideoInfoItem(null, nextVideoFrame, info.nextVideo); + .getViewFromVideoInfoItem(null, nextVideoFrame, info.nextVideo, getContext()); nextVideoFrame.addView(nextVideoView); @@ -253,31 +245,20 @@ public class VideoItemDetailFragment extends Fragment { uploaderView.setText(info.uploader); actionBarHandler.setChannelName(info.uploader); - Locale locale = getPreferredLocale(); - NumberFormat nf = NumberFormat.getInstance(locale); - String localisedViewCount = nf.format(info.view_count); - viewCountView.setText( - String.format( - res.getString(R.string.viewCountText), localisedViewCount)); + String localizedViewCount = Localization.localizeViewCount(info.view_count, getContext()); + viewCountView.setText(localizedViewCount); - thumbsUpView.setText(nf.format(info.like_count)); - thumbsDownView.setText(nf.format(info.dislike_count)); + String localizedLikeCount = Localization.localizeNumber(info.like_count, getContext()); + thumbsUpView.setText(localizedLikeCount); - @SuppressLint("SimpleDateFormat") - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - Date datum = null; - try { - datum = formatter.parse(info.upload_date); - } catch (ParseException e) { - e.printStackTrace(); - } + String localizedDislikeCount = Localization.localizeNumber(info.dislike_count, getContext()); + thumbsDownView.setText(localizedDislikeCount); - DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); + String localizedDate = Localization.localizeDate(info.upload_date, getContext()); + uploadDateView.setText(localizedDate); - String localisedDate = df.format(datum); - uploadDateView.setText( - String.format(res.getString(R.string.uploadDateText), localisedDate)); descriptionView.setText(Html.fromHtml(info.description)); + descriptionView.setMovementMethod(LinkMovementMethod.getInstance()); actionBarHandler.setServiceId(streamingServiceId); @@ -458,30 +439,6 @@ public class VideoItemDetailFragment extends Fragment { } } - - - /**Returns the java.util.Locale object which corresponds to the locale set in NewPipe's preferences. - * Currently not affected by the device's locale.*/ - private Locale getPreferredLocale() { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext()); - String languageKey = getContext().getString(R.string.searchLanguage); - //i know the following line defaults languageCode to "en", but java is picky about uninitialised values - // Schabi: well lint tels me the value is redundant. I'll suppress it for now. - @SuppressWarnings("UnusedAssignment") - String languageCode = "en"; - languageCode = sp.getString(languageKey, "en"); - - if(languageCode.length() == 2) { - return new Locale(languageCode); - } - else if(languageCode.contains("_")) { - String country = languageCode - .substring(languageCode.indexOf("_"), languageCode.length()); - return new Locale(languageCode.substring(0, 2), country); - } - return Locale.getDefault(); - } - private boolean checkIfLandscape() { DisplayMetrics displayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); diff --git a/app/src/main/java/org/schabi/newpipe/VideoListAdapter.java b/app/src/main/java/org/schabi/newpipe/VideoListAdapter.java index e85e74e22..70672d040 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/VideoListAdapter.java @@ -96,7 +96,7 @@ class VideoListAdapter extends BaseAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { - convertView = viewCreator.getViewFromVideoInfoItem(convertView, parent, videoList.get(position)); + convertView = viewCreator.getViewFromVideoInfoItem(convertView, parent, videoList.get(position), context); if(listView.isItemChecked(position)) { convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.primaryColorYoutube));