亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android?實(shí)現(xiàn)APP可切換多語言步驟詳解

 更新時(shí)間:2023年11月29日 08:55:00   作者:Stars-one  
如果是單獨(dú)給app加上國際化,其實(shí)很容易,創(chuàng)建對應(yīng)的國家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語言,需要的朋友可以參考下

如果是單獨(dú)給app加上國際化,其實(shí)很容易,創(chuàng)建對應(yīng)的國家資源文件夾即可,如values-en,values-pt,app會根據(jù)當(dāng)前系統(tǒng)語言去使用對應(yīng)語言資源文件,如果找不到,則使用values文件夾里的資源

但本文講得是另外一種情況,就是app內(nèi)置一個(gè)切換多語言的頁面,可以給用戶切換

步驟

1.添加服務(wù)聲明

此步驟主要是讓我們的app可記錄當(dāng)前應(yīng)用語言,使用的Service是android系統(tǒng)給我們提供的

<!--    國際化多語言    -->
<service
	android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
	android:enabled="false"
	android:exported="false">
	<meta-data
		android:name="autoStoreLocales"
		android:value="true" />
</service>

2.在xml文件夾增加文件locale_config.xml

聲明支持的幾個(gè)語言

<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
    <locale android:name="en" />
    <locale android:name="pt" />
    <locale android:name="es" />
    <locale android:name="de" />
    <locale android:name="fr" />
</locale-config>

3.調(diào)用方法切換多語言

// 切換語言
val langua="en"
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(langua))

補(bǔ)充下其他方法:

//獲取當(dāng)前應(yīng)用使用語言
val locale = AppCompatDelegate.getApplicationLocales()[0]
//語言短標(biāo)轉(zhuǎn)為locale對象
val langua="en"
val locale = Locale.forLanguageTag(langua)

一些坑點(diǎn)

1.上架谷歌市場無法切換語言

上架到谷歌市場,用戶下載只會下載其系統(tǒng)語言包,會導(dǎo)致app內(nèi)置的語言切換功能無效

原因是打包為aab的時(shí)候,gradle的配置,默認(rèn)是開啟了語言分包設(shè)置,我們?nèi)∠@個(gè)設(shè)置就可以解決此問題

gradle配置如下

buildTypes {
	release {
			
		bundle{
			//設(shè)置多語言不分包處理
			language {
				// Specifies that the app bundle should not support
				// configuration APKs for language resources. These
				// resources are instead packaged with each base and
				// feature APK.
				enableSplit = false
			}
			density {
				// This property is set to true by default.
				enableSplit = true
			}
			abi {
				// This property is set to true by default.
				enableSplit = true
			}

		}
	}
}

2.使用StringUtil導(dǎo)致語言切換功能失效

我使用到了Blankj/AndroidUtilCode里面的StringUtil獲取數(shù)據(jù),到時(shí)切換多語言后會存在問題

原因是里面StringUtil里面使用的是application而不是Activity

最終還是更換為使用Activity對象來獲取string文本(activity.getString(R.string.hello))

也看到了issue有人說到這個(gè)問題,說要是更新application的資源文件,但我測試的時(shí)候發(fā)現(xiàn)更新application的語言資源后,會觸發(fā)應(yīng)用閃屏的效果,然后就沒有使用此方法

由于項(xiàng)目進(jìn)度趕,就沒去細(xì)究了

3.使用靜態(tài)數(shù)據(jù)導(dǎo)致后續(xù)沒有文本沒有更新

因?yàn)轫撁嬗袔讉€(gè)使用相同布局的樣式,比如說常見的菜單項(xiàng),我是這樣的做法:

抽取出來的一個(gè)靜態(tài)類來存儲對應(yīng)數(shù)據(jù)(圖標(biāo),文本之類),之后寫一個(gè)xml文件,頁面則是使用include來引用多份相同樣式的item,最終在Activity里給這些item賦值

由于item比較少,又不想用recyclerview,就是采用了上面的這個(gè)方法

但是如果涉及到多語言切換的話,就會導(dǎo)致沒有數(shù)據(jù)及時(shí)更新

原因是更換語言后,是Activity進(jìn)行的重新創(chuàng)建,但我們存儲數(shù)據(jù)的類還是存在的,里面文本數(shù)據(jù)并沒有更新,所以就是導(dǎo)致了這個(gè)問題

解決方法簡單粗暴,就每次Activity的onCreate方法里創(chuàng)建對應(yīng)的數(shù)據(jù)對象即可,這樣,Activity重建之后我們的文本數(shù)據(jù)就會重新調(diào)用activity.getString(R.string.hello)獲取了

到此這篇關(guān)于Android 實(shí)現(xiàn)APP可切換多語言的文章就介紹到這了,更多相關(guān)Android 切換多語言內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論