我完全被困住了,请帮助我。 我开发了一种医院应用程序,该应用程序以英语和阿拉伯语两种语言运行。 我允许用户在两个部分中选择语言。 1.登录后 2.在设置页面上。 每当用户选择语言时,该应用就会以该语言(英语或阿拉伯语)运行。 在本地运行时,该应用程序正在更改语言。我将该应用程序发布在google playstore上进行测试。之后,我下载了该应用程序。当我选择阿拉伯语应用程序时,它不会读取本地存储在values-ar文件夹中的阿拉伯语值。 我在互联网上搜索了两天。找不到任何答案。...
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'
}
答案 0 :(得分:0)
我怀疑您正在将应用程序作为应用程序捆绑包上传到Google。这样做时,仅安装用户所需的语言资源。由于用户具有非阿拉伯设备,因此未安装阿拉伯资源。因此,将应用程序的语言环境更改为阿拉伯语无济于事,因为没有阿拉伯语资源。如果用户更改了设备区域设置,则Google 将下载并安装语言,但是更改应用程序区域设置可能会有所不同。一种简单的解决方案是以APK的形式上传到Google,以便为所有用户安装所有语言。 如果您想继续使用App Bundle,可以通过在build.gradle中添加以下内容来禁用this post的语言拆分功能
3