0

I am totally stuck.Please help me . I developed a hospital application which runs in two languages English and Arab. I allow the users to select language on two sections. 1.After login stage. 2.On the setting page. Whenever user selects the language ,the app runs on that language(English or Arab). The app is changing language when I run it on locally.I published the app on google playstore for testing.After that I downloaded it.When I select the arab language application is not reading the arabic values stored locally on values-ar folder. I searched the internet for two days.I did not find any answers....

I am giving you my codes....

1.User Selecting language after login stage.

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;

import com.icelab.home.hospital.Activities.Confirm.ConfirmpageActivity;
import com.icelab.home.hospital.Activities.Patientpage.PatientpageActivity;
import com.icelab.home.hospital.R;
import com.icelab.home.hospital.Utils.PrefManager;

import java.util.Locale;

public class SelectLanguages extends AppCompatActivity {
Button btn_English,btn_arab;
PrefManager prefManager;
private static final  int REQUEST_CHANGE_LANGUAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_languages);
        prefManager=new PrefManager();
        btn_English=findViewById(R.id.btn_English);
        btn_arab=findViewById(R.id.btn_arab);


        btn_English.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                prefManager.saveLanguageSelected(0);
                prefManager.languageSelection(1);
                setLocale("en");
               // loadPage();
            }
        });

        btn_arab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                prefManager.saveLanguageSelected(1);
                prefManager.languageSelection(1);
                setLocale("ar");
               // loadPage();

            }
        });
    }

    private void loadPage() {
        Intent intent=new Intent(SelectLanguages.this,PatientpageActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent=new Intent(SelectLanguages.this,ConfirmpageActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, PatientpageActivity.class);
        startActivity(refresh);
        finish();
    }
}

2.Users selecting language from setting page.


import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.icelab.home.hospital.Activities.Patientpage.PatientpageActivity;
import com.icelab.home.hospital.R;
import com.icelab.home.hospital.Utils.PrefManager;

import java.util.Locale;


public class SettingsFragment extends Fragment implements View.OnClickListener {

    RadioButton check_english, check_arab;
    PrefManager prefManager;
    int eng_selected = 0;
    int arab_selected = 1;

    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_setttings, container, false);
        prefManager = new PrefManager();
        intialize(view);
        loadselectedLanguage();
        return view;
    }

    private void loadselectedLanguage() {
        int language = prefManager.getSelectedLanguage();
        switch (language) {
            case 0:
                check_english.setChecked(true);
                check_arab.setChecked(false);
                break;

            case 1:
                check_arab.setChecked(true);
                check_english.setChecked(false);
                break;
            default:
                check_english.setChecked(true);
                check_arab.setChecked(false);
        }
    }

    private void intialize(View view) {
        check_english = view.findViewById(R.id.check_english);
        check_arab = view.findViewById(R.id.check_arab);
        check_english.setOnClickListener(this);
        check_arab.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            //User selects language.
            //Selected language is stored on shared preference as integer values.
            case R.id.check_english:
                check_arab.setChecked(false);
                prefManager.saveLanguageSelected(eng_selected);
                setLocale("en");
                break;

            case R.id.check_arab:
                check_english.setChecked(false);
                prefManager.saveLanguageSelected(arab_selected);
                setLocale("ar");
                break;

        }
    }

    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent intent = new Intent(requireContext(), PatientpageActivity.class);
        startActivity(intent);
        requireActivity().finish();
    }
}

3.My Values English folder
<resources>
    <string name="app_name">Al Jazira Clinic</string>
    <string name="welcome">WELCOME</string>
    <string name="get_your_appointment_today">get your appointment today</string>
    <string name="headingimage">HeadingImage</string>
    <string name="sign_in">SIGN IN</string>
    <string name="patient_id_phone_no">Patient ID/Phone No.</string>
    <string name="enter_otp">ENTER OTP</string>
    <string name="verify">VERIFY</string>
    <string name="appointment_history">APPOINTMENT HISTORY</string>
    <string name="resend_otp">Resend OTP</string>
    <string name="patient_name">Patient Name</string>
    <string name="_1_yrs_old">1 Yrs old</string>
    <string name="_966_123456789">+966 123456789</string>
    <string name="abc_exampole_com">abc@example.com</string>
    <string name="_8_00pm_8_30pm">8.00pm-8.30pm</string>
    <string name="_7_00pm_7_30pm">7.00pm-7.30pm</string>
    <string name="_6_00pm_6_30pm">6.00pm-6.30pm</string>
    <string name="_5_00pm_5_30pm">5.00pm-5.30pm</string>
    <string name="_4_00pm_4_30pm">4.00pm-4.30pm</string>
    <string name="_3_00pm_3_30pm">3.00pm-3.30pm</string>
    <string name="_2_00pm_2_30pm">2.00pm-2.30pm</string>
    <string name="_1_00pm_1_30pm">1.00pm-1.30pm</string>
    <string name="_12_00pm_12_30pm">12.00pm-12.30pm</string>
    <string name="_11_00am_11_30am">11.00am-11.30am</string>
    <string name="_10_00am_10_30am">10.00am-10.30am</string>
    <string name="_9_00am_9_30am">9.00am-9.30am</string>
    <string name="time_slots">Time Slots</string>
    <string name="departments">Departments</string>
    <string name="new_appointments">New Appointments</string>
    <string name="department">Department</string>
    <string name="contact_us">Contact us</string>
    <string name="about_us">About us</string>
    <string name="book_now">Book now</string>
    <string name="appointment">Appointment History</string>
</resources>

4.My values arab folder.


<resources>
    <string name="app_name">مستشفى</string>
    <string name="welcome">أهلا بك</string>
    <string name="get_your_appointment_today">الحصول على موعدك اليوم</string>
    <string name="headingimage">العنوان الصورة</string>
    <string name="sign_in">تسجيل الدخول</string>
    <string name="patient_id_phone_no">رقم المريض / رقم الهاتف</string>
    <string name="enter_otp">أدخل OTP</string>
    <string name="verify">التحقق</string>
    <string name="appointment_history">تاريخ التعيين</string>
    <string name="resend_otp">إعادة إرسال OTP</string>
    <string name="patient_name">اسم المريض</string>
    <string name="_1_yrs_old">1 عامًا</string>
    <string name="_966_123456789">+966 123456789</string>
    <string name="abc_exampole_com">abc@exampole.com</string>
    <string name="_8_00pm_8_30pm">8.00pm-8.30pm</string>
    <string name="_7_00pm_7_30pm">7.00pm-7.30pm</string>
    <string name="_6_00pm_6_30pm">6.00pm-6.30pm</string>
    <string name="_5_00pm_5_30pm">5.00pm-5.30pm</string>
    <string name="_4_00pm_4_30pm">4.00pm-4.30pm</string>
    <string name="_3_00pm_3_30pm">3.00pm-3.30pm</string>
    <string name="_2_00pm_2_30pm">2.00pm-2.30pm</string>
    <string name="_1_00pm_1_30pm">1.00pm-1.30pm</string>
    <string name="_12_00pm_12_30pm">12.00pm-12.30pm</string>
    <string name="_11_00am_11_30am">11.00am-11.30am</string>
    <string name="_10_00am_10_30am">10.00am-10.30am</string>
    <string name="_9_00am_9_30am">9.00am-9.30am</string>
    <string name="time_slots">فتحات الوقت</string>
    <string name="departments">الإدارات</string>
    <string name="appointment">تاريخ المواعيد</string>
</resources>

5.My gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.icelab.home.hospital"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 4
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        resConfigs "en", "ar"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.picasso:picasso:2.71828'


}

1 Answers1

5

I suspect that you are uploading the app to Google as an App Bundle. When you do that, only necessary language resources for the user are installed. As the user has a non-arabic device, the Arabic resources are not installed. So changing the app locale to Arabic doesn't help, as there are no Arabic resources. If user changes device locale, then Google will download and install language, but changing app locale may be different. The simple solution is to upload to Google as APK, so that all languages are installed for all users. If you want to continue using App Bundles, you can disable the language splitting feature as per this post by adding following to build.gradle

android { 
    bundle { 
        language { 
        // Specifies that the app bundle should not support 
        // configuration APKs for language resources. These 
        // resources are instead packaged with each base and 
        // dynamic feature APK. 
        enableSplit = false 
        } 
    } 
}
lionscribe
  • 3,413
  • 1
  • 16
  • 21