应用程序语言帮助默认语言在应用程序时返回

时间:2018-02-28 11:52:59

标签: android

您好我正在制作GymExercise App并且我添加了两种语言

1.英语 2.Macedonian

另外,我添加了菜单,这样用户可以在选择语言后选择他想要的语言(例如马其顿语)如果我打开其他活动或片段,语言会被更改但是当我关闭应用程序语言时会转到默认英语我想留下来用户选择的语言我该怎么做?

谢谢

MainActivity.java

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.language_en:
                Locale locale = new Locale("en");
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                Toast.makeText(this, "You choosed English Language", Toast.LENGTH_LONG).show();
                break;

            case R.id.language_mk:
                Locale locale2 = new Locale("mk");
                Locale.setDefault(locale2);
                Configuration config2 = new Configuration();
                config2.locale = locale2;
                getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
                Toast.makeText(this, "Одбравте Македонски Јазик", Toast.LENGTH_LONG).show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

2 个答案:

答案 0 :(得分:1)

MainActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.language_en:
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "You choosed English Language", Toast.LENGTH_LONG).show();
            saveLocale("en");
            break;

        case R.id.language_mk:
            Locale locale2 = new Locale("mk");
            Locale.setDefault(locale2);
            Configuration config2 = new Configuration();
            config2.locale = locale2;
            getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "Одбравте Македонски Јазик", Toast.LENGTH_LONG).show();
            saveLocale("mk");
            break;

    }
    Intent intent = getIntent();
    startActivity(intent);
    finish();
    return super.onOptionsItemSelected(item);
}

public void saveLocale(String language) {
    SharedPreferences sharedPreferences = getSharedPreferences("com.Hristijan.Aleksandar.GymAssistant.Exercises.PREFERENCES", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("USER_LANGUAGE", language);
    editor.commit();
}

当您更改语言时,需要刷新。 在更改语言后编写此代码,

Intent intent = getIntent();
startActivity(intent);
finish();

将语言代码存储在 SharedPreference

创建 ChangeLanguageApplication 类,

public class ChangeLanguageApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences sharedPreferences = getSharedPreferences("com.Hristijan.Aleksandar.GymAssistant.Exercises.PREFERENCES", Activity.MODE_PRIVATE);

        Locale locale = new Locale(sharedPreferences.getString("USER_LANGUAGE","en"));//Get from Pref.

        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }
}

清单文件中定义,

<application android:name=".ChangeLanguageApplication"> </application>

答案 1 :(得分:0)

@Bhavya Gandhi

这里我做的不起作用

MainActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.language_en:
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "You choosed English Language", Toast.LENGTH_LONG).show();
            break;

        case R.id.language_mk:
            Locale locale2 = new Locale("mk");
            Locale.setDefault(locale2);
            Configuration config2 = new Configuration();
            config2.locale = locale2;
            getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "Одбравте Македонски Јазик", Toast.LENGTH_LONG).show();
            break;

    }
    Intent intent = getIntent();
    startActivity(intent);
    finish();
    return super.onOptionsItemSelected(item);
}

public void saveLocale(String language) {
    SharedPreferences sharedPreferences = getSharedPreferences("com.Hristijan.Aleksandar.GymAssistant.Exercises.PREFERENCES", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("USER_LANGUAGE", language);
    editor.commit();
}

ChangeLanguageApplication Class

package com.Hristijan.Aleksandar.GymAssistant.Exercises;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import java.util.Locale;

/**
 * Created by Aleksandar on 28.2.2018.
 */

public class ChangeLanguageApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Locale locale = new Locale("USER_LANGUAGE");//Get from Pref.
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.Hristijan.Aleksandar.GymAssistant.Exercises">



    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.flash" android:required="false"/>
    <uses-feature android:name="android.hardware.screen.landscape" android:required="false" />
    <uses-feature android:name="android.hardware.wifi" android:required="false"/>

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"/>

    <application
        android:name=".ChangeLanguageApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:hardwareAccelerated="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.Hristijan.Aleksandar.GymAssistant.Exercises.Splashscreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.Hristijan.Aleksandar.GymAssistant.Exercises.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:hardwareAccelerated="true"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.journeyapps.barcodescanner.CaptureActivity"
            android:screenOrientation="fullSensor"
            tools:replace="screenOrientation" />
        <activity android:name="com.Hristijan.Aleksandar.GymAssistant.Exercises.QrActivity"
            android:screenOrientation="portrait"/>
        <activity android:name="com.Hristijan.Aleksandar.GymAssistant.Exercises.BmiActivity"
            android:windowSoftInputMode="stateHidden"
            android:screenOrientation="portrait"/>
    </application>

</manifest>