Android 产品研发-->实现国际化(多语言)

Posted Kevin_小飞象

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 产品研发-->实现国际化(多语言)相关的知识,希望对你有一定的参考价值。

简介

最近工作中突然要求要项目进行国际化,之前没遇到过。但是也很简单呀,只需要把添加一个相应语言的的 strings.xml 的资源文件就好了,不是吗?这样只要切换系统语言就能切换 app 的文字语言了。

效果图

实现

1. 创建对应语言的 string.xml 简体中文,繁体和 English 三个语言。

2. MultiLanguage.java

/**
 * Created on 2021/11/10 17:02
 *
 * @author Gong Youqiang
 */
public class MultiLanguage 
    private static LanguageListener languageLocalListener;

    public static void init(LanguageListener listener) 
        languageLocalListener = listener;
    

    public static Context setLocal(Context context) 
        return updateResources(context, getSetLanguageLocale(context));
    


    /**
     * 设置语言类型
     */
    public static void setApplicationLanguage(Context context) 
        Resources resources = context.getApplicationContext().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        Locale locale = getSetLanguageLocale(context);
        config.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            context.getApplicationContext().createConfigurationContext(config);
            Locale.setDefault(locale);
        
        resources.updateConfiguration(config, dm);
    


    /**
     * @param context
     */
    public static void onConfigurationChanged(Context context) 
        setLocal(context);
        setApplicationLanguage(context);
    


    /**
     * 获取选择的语言
     *
     * @param context
     * @return
     */
    private static Locale getSetLanguageLocale(Context context) 
        if (languageLocalListener != null) 
            return languageLocalListener.getSetLanguageLocale(context);
        
        return Locale.ENGLISH;
    

    /**
     * 更新语言设置
     *
     * @param context
     * @param locale
     * @return
     */
    private static Context updateResources(Context context, Locale locale) 
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) 
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
         else 
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        
        return context;
    

    /**
     * 获取系统语言
     * @param newConfig
     * @return
     */
    public static Locale getSystemLocal(Configuration newConfig) 
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
            locale = newConfig.getLocales().get(0);
         else 
            locale = newConfig.locale;
        
        return locale;
    

    /**
     * 获取系统语言
     * @param context
     * @return
     */
    public static Locale getSystemLocal(Context context) 
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
            locale = LocaleList.getDefault().get(0);
         else 
            locale = Locale.getDefault();
        
        return locale;
    

3. SPUtil.java

/**
 * Created on 2021/11/10 17:04
 *
 * @author Gong Youqiang
 */
public class SPUtil 
    private final String SP_NAME = "language_setting";
    private final String TAG_LANGUAGE = "language_select";
    private final String TAG_SYSTEM_LANGUAGE = "system_language";
    private static volatile SPUtil instance;

    private final SharedPreferences mSharedPreferences;

    private Locale systemCurrentLocal = Locale.ENGLISH;


    public SPUtil(Context context) 
        mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    


    public void saveLanguage(int select) 
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putInt(TAG_LANGUAGE, select);
        edit.commit();
    

    public int getSelectLanguage() 
        return mSharedPreferences.getInt(TAG_LANGUAGE, 0);
    


    public Locale getSystemCurrentLocal() 
        return systemCurrentLocal;
    

    public void setSystemCurrentLocal(Locale local) 
        systemCurrentLocal = local;
    

    public static SPUtil getInstance(Context context) 
        if (instance == null) 
            synchronized (SPUtil.class) 
                if (instance == null) 
                    instance = new SPUtil(context);
                
            
        
        return instance;
    

4. LanguageUtils.java

/**
 * Created on 2021/11/10 16:50
 *
 * @author Gong Youqiang
 */
public class LanguageUtils 
    private static final String TAG = "LocalManageUtil";

    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    public static Locale getSystemLocale(Context context) 
        return SPUtil.getInstance(context).getSystemCurrentLocal();
    

    public static String getSelectLanguage(Context context) 
        switch (SPUtil.getInstance(context).getSelectLanguage()) 
            case 0:
                return context.getString(R.string.language_auto);
            case 1:
                return context.getString(R.string.language_cn);
            case 2:
                return context.getString(R.string.language_traditional);
            case 3:
            default:
                return context.getString(R.string.language_en);
        
    

    /**
     * 获取选择的语言设置
     *
     * @param context
     * @return
     */
    public static Locale getSetLanguageLocale(Context context) 

        switch (SPUtil.getInstance(context).getSelectLanguage()) 
            case 0:
                return getSystemLocale(context);
            case 1:
                return Locale.CHINA;
            case 2:
                return Locale.TAIWAN;
            case 3:
            default:
                return Locale.ENGLISH;
        
    


    public static void saveSystemCurrentLanguage(Context context) 
        SPUtil.getInstance(context).setSystemCurrentLocal(MultiLanguage.getSystemLocal(context));
    

    /**
     * 保存系统语言
     * @param context
     * @param newConfig
     */
    public static void saveSystemCurrentLanguage(Context context, Configuration newConfig) 

        SPUtil.getInstance(context).setSystemCurrentLocal(MultiLanguage.getSystemLocal(newConfig));
    

    public static void saveSelectLanguage(Context context, int select) 
        SPUtil.getInstance(context).saveLanguage(select);
        MultiLanguage.setApplicationLanguage(context);
    

5. LanguageListener.java

/**
 * Created on 2021/11/10 16:49
 *
 * @author Gong Youqiang
 */
public interface LanguageListener 
    /**
     * 获取选择设置语言
     *
     * @param context
     * @return
     */
    Locale getSetLanguageLocale(Context context);

  1. LauncherApp.java
/**
 * Created on 2021/11/10 17:01
 *
 * @author Gong Youqiang
 */
public class LauncherApp extends Application 
    @Override
    protected void attachBaseContext(Context base) 
        //第一次进入app时保存系统选择语言(为了选择随系统语言时使用,如果不保存,切换语言后就拿不到了)
        LanguageUtils.saveSystemCurrentLanguage(base);
        super.attachBaseContext(MultiLanguage.setLocal(base));
    

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
        super.onConfigurationChanged(newConfig);
        //用户在系统设置页面切换语言时保存系统选择语言(为了选择随系统语言时使用,如果不保存,切换语言后就拿不到了)
        LanguageUtils.saveSystemCurrentLanguage(getApplicationContext(), newConfig);
        MultiLanguage.onConfigurationChanged(getApplicationContext());
    

    @Override
    public void onCreate() 
        super.onCreate();
        MultiLanguage.init(new LanguageListener() 
            @Override
            public Locale getSetLanguageLocale(Context context) 
                //返回自己本地保存选择的语言设置
                return LanguageUtils.getSetLanguageLocale(context);
            
        );
        MultiLanguage.setApplicationLanguage(this);
    

7. 布局文件

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

若觉得对您有帮助,您的赞是我最大的动力!

在线转换工具:中文转繁体

以上是关于Android 产品研发-->实现国际化(多语言)的主要内容,如果未能解决你的问题,请参考以下文章

Android:应用多语言切换,国际化实现

Android国际化多语言切换

Android国际化多语言切换

android国家化(多语言版本APP)

Android国际化之多语言(配置及应用内设置)✈️

.NET Core AvaloniaUI实现多语言国际化