欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

App内切换语言详解

程序员文章站 2022-07-18 12:24:34
前几天客户提需求,对app增加一个功能,这个功能目前市面上已经很常见,那就是应用内切换语言。啥意思,就是 英、中、法、德、日。。。语言随意切换。 (本案例采用data-b...

前几天客户提需求,对app增加一个功能,这个功能目前市面上已经很常见,那就是应用内切换语言。啥意思,就是 英、中、法、德、日。。。语言随意切换。

(本案例采用data-bingding模式,麻麻再也不用担心我findviewby不到id了哈哈,开个玩笑)

先上示例图:

App内切换语言详解

代码实现:

布局文件(data-binding模式),很简单就是两行文字

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
 <relativelayout xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
tools:context="com.tnnowu.android.switchlanguage.mainactivity">
 <textview
 android:id="@+id/titletextview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerinparent="true"
 android:text="@string/title"
 android:textsize="30sp"
 android:textstyle="bold" />
 <textview
 android:id="@+id/desctextview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/titletextview"
 android:layout_centerhorizontal="true"
 android:layout_margintop="10dp"
 android:text="@string/desc"
 android:textsize="20sp" />
 </relativelayout>
</layout>

从实例中我们可以看到右上角是有menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:context=".mainactivity">

 <item
 android:id="@+id/language_english"
 android:orderincategory="100"
 android:title="@string/menu_english" />
 <item
 android:id="@+id/language_simplified_chinese"
 android:orderincategory="100"
 android:title="@string/menu_simplified_chinese" />
 <item
 android:id="@+id/language_turkish"
 android:orderincategory="100"
 android:title="@string/menu_turkish" />
 <item
 android:id="@+id/language_japanese"
 android:orderincategory="100"
 android:title="@string/menu_japanese" />
</menu>

(既然是多语言,所以就要有n个strings)

App内切换语言详解

本案例我创建了4种语言。

好的,menu的布局写完了,接下来就是实现menu功能,记住实现menu就两套代码,一个 oncreateoptionsmenu , 另一个是 onoptionsitemselected 。

@override
public boolean oncreateoptionsmenu(menu menu) {
 getmenuinflater().inflate(r.menu.menu, menu);
 return true;
}
@override
public boolean onoptionsitemselected(menuitem item) {
 int id = item.getitemid();
 if (id == r.id.language_english) {
 updateviews("en");
 } else if (id == r.id.language_simplified_chinese) {
 updateviews("zh");
 } else if (id == r.id.language_turkish) {
 updateviews("tr");
 } else if (id == r.id.language_japanese) {
 updateviews("ja");
 }
 return super.onoptionsitemselected(item);
}

在这里,可以看到,我们自定义一个 updateviews() 方法,用来实现切换预言时界面的改变

private void updateviews(string languagecode) {
 context context = localehelper.setlocale(this, languagecode);
 resources resources = context.getresources();
 mbinding.titletextview.settext(resources.getstring(r.string.title));
 mbinding.desctextview.settext(resources.getstring(r.string.desc));
 settitle(resources.getstring(r.string.toolbar_title));
}

公布一个 语言判断的类 localehelper

public class localehelper {
 private static final string selected_language = "locale.helper.selected.language";
 public static context onattach(context context) {
 string lang = getpersisteddata(context, locale.getdefault().getlanguage());
 return setlocale(context, lang);
 }
 public static context onattach(context context, string defaultlanguage) {
 string lang = getpersisteddata(context, defaultlanguage);
 return setlocale(context, lang);
 }
 public static string getlanguage(context context) {
 return getpersisteddata(context, locale.getdefault().getlanguage());
 }
 public static context setlocale(context context, string language) {
 persist(context, language);
 if (build.version.sdk_int >= build.version_codes.n) {
  return updateresources(context, language);
 }
 return updateresourceslegacy(context, language);
 }
 private static string getpersisteddata(context context, string defaultlanguage) {
 sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(context);
 return preferences.getstring(selected_language, defaultlanguage);
 }
 private static void persist(context context, string language) {
 sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(context);
 sharedpreferences.editor editor = preferences.edit();
 editor.putstring(selected_language, language);
 editor.apply();
 }
 @targetapi(build.version_codes.n)
 private static context updateresources(context context, string language) {
 locale locale = new locale(language);
 locale.setdefault(locale);
 configuration configuration = context.getresources().getconfiguration();
 configuration.setlocale(locale);
 return context.createconfigurationcontext(configuration);
 }
 @suppresswarnings("deprecation")
 private static context updateresourceslegacy(context context, string language) {
 locale locale = new locale(language);
 locale.setdefault(locale);
 resources resources = context.getresources();
 configuration configuration = resources.getconfiguration();
 configuration.locale = locale;
 resources.updateconfiguration(configuration, resources.getdisplaymetrics());
 return context;
 }
}

最后还要做的操作就是,自定义一个application类,用来设定app的默认语言(当然了,要将这个application应用到manifest中)

public class baseapplication extends application {
 @override
 protected void attachbasecontext(context base) {
 super.attachbasecontext(localehelper.onattach(base, "en"));
 }
}

本案例实现app内语言切换代码量不大,通俗易懂,无垃圾代码。

示例代码下载地址:app内切换语言

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!