refactor localization

This commit is contained in:
chschtsch 2015-12-29 17:35:51 +03:00
parent bd6cc22e63
commit 40f00af196
4 changed files with 93 additions and 57 deletions

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe; package org.schabi.newpipe;
import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -33,7 +34,7 @@ class VideoInfoItemViewCreator {
this.inflater = inflater; 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; ViewHolder holder;
if(convertView == null) { if(convertView == null) {
convertView = inflater.inflate(R.layout.video_item, parent, false); convertView = inflater.inflate(R.layout.video_item, parent, false);
@ -59,8 +60,7 @@ class VideoInfoItemViewCreator {
if(!info.upload_date.isEmpty()) { if(!info.upload_date.isEmpty()) {
holder.itemUploadDateView.setText(info.upload_date); holder.itemUploadDateView.setText(info.upload_date);
} else { } else {
//tweak if necessary: This is a hack to prevent having white space in the layout :P holder.itemUploadDateView.setText(Localization.localizeViewCount(info.view_count, context));
holder.itemUploadDateView.setText(String.format("%d", info.view_count));
} }
return convertView; return convertView;

View File

@ -1,9 +1,7 @@
package org.schabi.newpipe; package org.schabi.newpipe;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -35,13 +33,7 @@ import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import java.net.URL; 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.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Vector; import java.util.Vector;
import org.schabi.newpipe.services.VideoExtractor; import org.schabi.newpipe.services.VideoExtractor;
@ -235,7 +227,7 @@ public class VideoItemDetailFragment extends Fragment {
switch (info.errorCode) { switch (info.errorCode) {
case VideoInfo.NO_ERROR: { case VideoInfo.NO_ERROR: {
View nextVideoView = videoItemViewCreator View nextVideoView = videoItemViewCreator
.getViewFromVideoInfoItem(null, nextVideoFrame, info.nextVideo); .getViewFromVideoInfoItem(null, nextVideoFrame, info.nextVideo, getContext());
nextVideoFrame.addView(nextVideoView); nextVideoFrame.addView(nextVideoView);
@ -253,31 +245,20 @@ public class VideoItemDetailFragment extends Fragment {
uploaderView.setText(info.uploader); uploaderView.setText(info.uploader);
actionBarHandler.setChannelName(info.uploader); actionBarHandler.setChannelName(info.uploader);
Locale locale = getPreferredLocale(); String localizedViewCount = Localization.localizeViewCount(info.view_count, getContext());
NumberFormat nf = NumberFormat.getInstance(locale); viewCountView.setText(localizedViewCount);
String localisedViewCount = nf.format(info.view_count);
viewCountView.setText(
String.format(
res.getString(R.string.viewCountText), localisedViewCount));
thumbsUpView.setText(nf.format(info.like_count)); String localizedLikeCount = Localization.localizeNumber(info.like_count, getContext());
thumbsDownView.setText(nf.format(info.dislike_count)); thumbsUpView.setText(localizedLikeCount);
@SuppressLint("SimpleDateFormat") String localizedDislikeCount = Localization.localizeNumber(info.dislike_count, getContext());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); thumbsDownView.setText(localizedDislikeCount);
Date datum = null;
try {
datum = formatter.parse(info.upload_date);
} catch (ParseException e) {
e.printStackTrace();
}
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.setText(Html.fromHtml(info.description));
descriptionView.setMovementMethod(LinkMovementMethod.getInstance()); descriptionView.setMovementMethod(LinkMovementMethod.getInstance());
actionBarHandler.setServiceId(streamingServiceId); 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() { private boolean checkIfLandscape() {
DisplayMetrics displayMetrics = new DisplayMetrics(); DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

View File

@ -96,7 +96,7 @@ class VideoListAdapter extends BaseAdapter {
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { 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)) { if(listView.isItemChecked(position)) {
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.primaryColorYoutube)); convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.primaryColorYoutube));