Skip to content

Commit

Permalink
refactoring code for better support for localization
Browse files Browse the repository at this point in the history
initial commit for extracting resources for all hard coded strings
  • Loading branch information
LuGO0 committed Apr 17, 2020
1 parent 0fccb0c commit ae3f78e
Show file tree
Hide file tree
Showing 31 changed files with 278 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ protected void onCreate(Bundle savedInstanceState) {
locationList = new ArrayList<>();
Bundle extras = getIntent().getExtras();
if (extras != null) {
Boolean flag = extras.getBoolean("flag");
String errorReport = extras.getString("error");
Boolean flag = extras.getBoolean(getString(R.string.boolean_flag));
String errorReport = extras.getString(getString(R.string.error_report));
if (flag) {
showAppCrashDialog(errorReport);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else if (NetworkUtils.hasNetwork()) {
OpenMRS.getInstance().setSyncState(true);
setSyncButtonState(true);
Intent intent = new Intent("org.openmrs.mobile.intent.action.SYNC_PATIENTS");
Intent intent = new Intent(getString(R.string.intent_action_sync_patients));
getApplicationContext().sendBroadcast(intent);
ToastUtil.showShortToast(getApplicationContext(), ToastUtil.ToastType.NOTICE, R.string.reconn_server);
if (snackbar != null)
Expand Down Expand Up @@ -390,9 +390,9 @@ public void showAppCrashDialog(String error) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, R.string.error_email_subject_app_crashed);
email.putExtra(Intent.EXTRA_TEXT, error);
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename));
email.putExtra(Intent.EXTRA_STREAM, Uri.parse(getString(R.string.file_uri_prefix) + filename));
//need this to prompts email client only
email.setType("message/rfc822");
email.setType(getString(R.string.MIME_type_encapsulated_message));

startActivity(Intent.createChooser(email, getString(R.string.choose_a_email_client)));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ protected void onCreate(Bundle savedInstanceState) {
try {
applicationInfo = this.getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
Log.e("Package Manager",e.getMessage());
Log.e(getString(R.string.package_manager),e.getMessage());
}
Bundle bundle = applicationInfo.metaData;
String googleMapToken = bundle.getString("com.google.android.geo.API_KEY");
String googleMapToken = bundle.getString(getString(R.string.google_API_key_token));
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), googleMapToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
Expand Down Expand Up @@ -316,7 +315,7 @@ private Patient updatePatientWithData(Patient patient) {
patient.setNames(names);

// Add gender
String[] genderChoices = {"M", "F"};
String[] genderChoices = {getString(R.string.male_gender_indicator), getString(R.string.female_gender_indicator)};
int index = gen.indexOfChild(getActivity().findViewById(gen.getCheckedRadioButtonId()));
if (index != -1) {
patient.setGender(genderChoices[index]);
Expand Down Expand Up @@ -490,9 +489,9 @@ private void fillFields(final Patient patient) {
DateUtils.DEFAULT_DATE_FORMAT));
}

if (("M").equals(patient.getGender())) {
if ((getString(R.string.male_gender_indicator)).equals(patient.getGender())) {
gen.check(R.id.male);
} else if (("F").equals(patient.getGender())) {
} else if ((getString(R.string.female_gender_indicator)).equals(patient.getGender())) {
gen.check(R.id.female);
}

Expand Down Expand Up @@ -603,7 +602,7 @@ public void afterTextChanged(Editable s) {
else
i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
i.setType(getString(R.string.MIME_type_image));
startActivityForResult(i, GALLERY_IMAGE_REQUEST);
} else {
patientImageView.setImageResource(R.drawable.ic_person_grey_500_48dp);
Expand All @@ -620,14 +619,14 @@ public void afterTextChanged(Editable s) {
patientImageView.setOnClickListener(view -> {
if (output != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
i.setDataAndType(Uri.fromFile(output), getString(R.string.MIME_type_image_jpeg));
startActivity(i);
} else if (patientPhoto != null) {
Intent intent = new Intent(getContext(), PatientPhotoActivity.class);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
patientPhoto.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
intent.putExtra("photo", byteArrayOutputStream.toByteArray());
intent.putExtra("name", patientName);
intent.putExtra(getString(R.string.intent_extras_key_photo), byteArrayOutputStream.toByteArray());
intent.putExtra(getString(R.string.intent_extras_key_patient_name), patientName);
startActivity(intent);

}
Expand Down Expand Up @@ -765,7 +764,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

try {
ParcelFileDescriptor parcelFileDescriptor =
getActivity().getContentResolver().openFileDescriptor(data.getData(), "r");
getActivity().getContentResolver().openFileDescriptor(data.getData(), getString(R.string.file_read_mode));
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
Expand All @@ -775,7 +774,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
patientImageView.setImageBitmap(bitmap);
patientImageView.invalidate();
} catch (Exception e) {
logger.e("Error getting image from gallery.", e);
logger.e(getString(R.string.gallery_image_request_failed_message), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {

binding.contactEmailButton.setOnClickListener(v -> {
Intent sendMailIntent = new Intent(Intent.ACTION_SENDTO);
String mailTo = "mailto:".concat(binding.contactEmailText.getText().toString());
String mailTo = getString(R.string.MIME_type_mail).concat(binding.contactEmailText.getText().toString());
sendMailIntent.setData(Uri.parse(mailTo));
try {
startActivity(sendMailIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DashboardFragment extends ACBaseFragment<DashboardContract.Presente
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

final String PREFS_NAME = "OpenMRSPrefFile";
final String PREFS_NAME = getString(R.string.openmrs_pref_filename);

SharedPreferences settings2 = getActivity().getSharedPreferences(PREFS_NAME, 0);

Expand All @@ -82,8 +82,8 @@ private void showOverlayTutorialOne() {
Target viewTarget = new ViewTarget(R.id.findPatientView, this.getActivity());
new ShowcaseView.Builder(this.getActivity())
.setTarget(viewTarget)
.setContentTitle("Find Patients")
.setContentText("Click here to search through all the patients")
.setContentTitle(getString(R.string.find_patient_title))
.setContentText(getString(R.string.search_through_all_patients_introduction_message))
.hideOnTouchOutside()
.setStyle(R.style.CustomShowcaseTheme)
.setShowcaseEventListener(new OnShowcaseEventListener() {
Expand Down Expand Up @@ -116,8 +116,8 @@ private void showOverlayTutorialTwo() {
Target viewTarget = new ViewTarget(R.id.activeVisitsView, this.getActivity());
new ShowcaseView.Builder(this.getActivity())
.setTarget(viewTarget)
.setContentTitle("Active Visits")
.setContentText("Click here to get the list of all the currently active visits")
.setContentTitle(getString(R.string.active_visits_title))
.setContentText(getString(R.string.current_active_visits_introduction_message))
.hideOnTouchOutside()
.setStyle(R.style.CustomShowcaseTheme)
.setShowcaseEventListener(new OnShowcaseEventListener() {
Expand Down Expand Up @@ -149,8 +149,8 @@ private void showOverlayTutorialThree() {
Target viewTarget = new ViewTarget(R.id.registryPatientView, this.getActivity());
new ShowcaseView.Builder(this.getActivity())
.setTarget(viewTarget)
.setContentTitle("Register Patient")
.setContentText("Click here to register a new patient")
.setContentTitle(getString(R.string.register_patient_title))
.setContentText(getString(R.string.register_patient_introduction_message))
.hideOnTouchOutside()
.setStyle(R.style.CustomShowcaseTheme)
.setShowcaseEventListener(new OnShowcaseEventListener() {
Expand Down Expand Up @@ -182,8 +182,8 @@ private void showOverlayTutorialFour() {
Target viewTarget = new ViewTarget(R.id.captureVitalsView, this.getActivity());
new ShowcaseView.Builder(this.getActivity())
.setTarget(viewTarget)
.setContentTitle("Form Entry")
.setContentText("Click here to capture vitals for a patient on a visit")
.setContentTitle(getString(R.string.form_entry_title))
.setContentText(getString(R.string.capture_vitals_for_patient_introduction_message))
.hideOnTouchOutside()
.setStyle(R.style.CustomShowcaseThemeExit)
.setShowcaseEventListener(new OnShowcaseEventListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static CameraOrGalleryPickerDialog getInstance(DialogInterface.OnClickLis
public Dialog onCreateDialog(Bundle savedInstanceState) {


String[] textResources = {"Take a photo", "Choose another", "Remove photo"};
String[] textResources = {getString(R.string.take_a_photo_alert_dialog_option), getString(R.string.choose_another_photo_alert_dialog_message), getString(R.string.remove_photo_alert_dialog_message)};
int[] imageResources = {R.drawable.ic_photo_camera, R.drawable.ic_photo_library, R.drawable.ic_photo_delete};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public final void setOnBackListener() {
getDialog().setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK && getActivity().getClass().equals(LoginActivity.class)) {
if (OpenMRS.getInstance().getServerUrl().equals(ApplicationConstants.EMPTY_STRING)) {
OpenMRS.getInstance().getOpenMRSLogger().d("Exit application");
OpenMRS.getInstance().getOpenMRSLogger().d(getString(R.string.application_exit_logger_message));
getActivity().onBackPressed();
dismiss();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@

package org.openmrs.mobile.activities.formdisplay;

import android.content.res.Resources;
import android.os.Bundle;
import android.util.SparseArray;

import org.joda.time.LocalDateTime;
import org.openmrs.mobile.R;
import org.openmrs.mobile.activities.BasePresenter;
import org.openmrs.mobile.api.EncounterService;
import org.openmrs.mobile.dao.PatientDAO;
import org.openmrs.mobile.listeners.retrofit.DefaultResponseCallbackListener;
import org.openmrs.mobile.models.Encountercreate;
import org.openmrs.mobile.models.Obscreate;
import org.openmrs.mobile.models.Patient;
import org.openmrs.mobile.models.Resource;
import org.openmrs.mobile.utilities.ApplicationConstants;
import org.openmrs.mobile.utilities.InputField;
import org.openmrs.mobile.utilities.SelectOneField;
Expand Down Expand Up @@ -118,8 +121,7 @@ public void createEncounter() {
if(!mPatient.isSynced()) {
mPatient.addEncounters(encountercreate.getId());
new PatientDAO().updatePatient(mPatient.getId(),mPatient);
ToastUtil.error("Patient not yet registered. Form data is saved locally " +
"and will sync when internet connection is restored. ");
ToastUtil.error(Resources.getSystem().getString(R.string.patient_not_resistered_error_message)+Resources.getSystem().getString(R.string.form_data_will_be_synced_later_no_internet_connection_error_message));
mFormDisplayView.enableSubmitButton(true);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public boolean checkInputFields() {
}

if (allEmpty) {
ToastUtil.error("All fields cannot be empty");
ToastUtil.error(getString(R.string.all_fields_empty_error_message));
return false;
}
return valid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package org.openmrs.mobile.activities.formlist;


import android.content.res.Resources;

import org.openmrs.mobile.R;
import org.openmrs.mobile.activities.BasePresenter;
import org.openmrs.mobile.dao.EncounterDAO;
import org.openmrs.mobile.models.EncounterType;
Expand Down Expand Up @@ -98,7 +101,7 @@ public void listItemClicked(int position, String formName) {
String encounterType = encType.getUuid();
view.startFormDisplayActivity(formName, patientId, valueRefString, encounterType);
} else {
view.showError("There is no encounter type called " + formName);
view.showError(Resources.getSystem().getString(R.string.no_such_form_name_error_message,formName));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public void onResponse() {

@Override
public void onErrorResponse(String errorMessage) {
ToastUtil.error("Failed to fetch patient data");
ToastUtil.error(mContext.getString(R.string.failed_fetching_patient_data_error_message));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mPresenter.refresh();
mAdapter.finishActionMode();
}else {
ToastUtil.error("No Internet Connection");
ToastUtil.error(getString(R.string.no_internet_connection_message));
getActivity().finish();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public void setUrl(String url) {
if (result.isURLValid()) {
mPresenter.loadLocations(result.getUrl());
} else {
showInvalidURLSnackbar("Invalid URL");
showInvalidURLSnackbar(getString(R.string.invalid_URL_message));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.openmrs.mobile.activities.login;

import android.content.res.Resources;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -153,7 +155,7 @@ public void onResponse() {
public void onErrorResponse(String errorMessage) {

OpenMRS.getInstance().setVisitTypeUUID(ApplicationConstants.DEFAULT_VISIT_TYPE_UUID);
loginView.showToast("Failed to fetch visit type",
loginView.showToast(Resources.getSystem().getString(R.string.failed_fetching_visit_type_error_message),
ToastUtil.ToastType.ERROR);
}
});
Expand Down Expand Up @@ -235,7 +237,7 @@ public void onResponse(@NonNull Call<Results<Location>> call, @NonNull Response<
loginView.startFormListService();
loginView.setLocationErrorOccurred(false);
} else {
loginView.showInvalidURLSnackbar("Failed to fetch server's locations");
loginView.showInvalidURLSnackbar(Resources.getSystem().getString(R.string.failed_fetching_servers_location));
loginView.setLocationErrorOccurred(true);
loginView.initLoginForm(new ArrayList<>(), url);
}
Expand All @@ -258,7 +260,7 @@ public void onFailure(@NonNull Call<Results<Location>> call, @NonNull Throwable
loginView.initLoginForm(locations, url);
loginView.setLocationErrorOccurred(false);
} else {
loginView.showToast("Network not available.", ToastUtil.ToastType.ERROR);
loginView.showToast(Resources.getSystem().getString(R.string.no_internet_connection_message), ToastUtil.ToastType.ERROR);
loginView.showOpenMRSLogo();
loginView.setLocationErrorOccurred(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void fabCopyAll(String textLogs) {

fab.setOnClickListener(view -> {
setClipboard(getContext(), textLogs);
Toast.makeText(getContext(), "Logs copied to clipboard",
Toast.makeText(getContext(), R.string.logs_copied_to_clipboard_message,
Toast.LENGTH_SHORT).show();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.openmrs.mobile.activities.matchingpatients;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
Expand Down Expand Up @@ -110,7 +111,7 @@ public PatientViewHolder(View itemView) {
mPresenter.removeSelectedPatient();
cardView.setCardBackgroundColor(Color.WHITE);
} else {
ToastUtil.notify("You can select only one similar patient");
ToastUtil.notify(Resources.getSystem().getString(R.string.only_one_patient_can_be_selected_notification_message));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
Bundle mBundle = this.getIntent().getBundleExtra("bundle");
try {
JSONObject chartData = new JSONObject(mBundle.getString("vitalName"));
JSONObject chartData = new JSONObject(mBundle.getString(getString(R.string.vital_name_bundle_key)));
Iterator<String> dates = chartData.keys();
ArrayList<String> dateList = Lists.newArrayList(dates);
//Sorting the date
Expand All @@ -63,7 +63,7 @@ else if (DateUtils.getDateFromString(lhs).getTime() == DateUtils.getDateFromStri
for (Integer i = 0; i < dataArray.length(); i++) {
entries.add(new Entry(j, Float.parseFloat((String) dataArray.get(i))));
}
LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
LineDataSet dataSet = new LineDataSet(entries, getString(R.string.dataset_entry_label)); // add entries to dataset
dataSet.setCircleColor(R.color.green);
dataSet.setValueTextSize(12);
List<ILineDataSet> ILdataSet = new ArrayList<>();
Expand Down
Loading

0 comments on commit ae3f78e

Please sign in to comment.