欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

步驟

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

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

<!--    國(guó)際化多語(yǔ)言    -->
<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è)語(yǔ)言

<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)用方法切換多語(yǔ)言

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

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

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

一些坑點(diǎn)

1.上架谷歌市場(chǎng)無(wú)法切換語(yǔ)言

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

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

gradle配置如下

buildTypes {
	release {
			
		bundle{
			//設(shè)置多語(yǔ)言不分包處理
			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)致語(yǔ)言切換功能失效

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

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

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

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

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

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

因?yàn)轫?yè)面有幾個(gè)使用相同布局的樣式,比如說(shuō)常見(jiàn)的菜單項(xiàng),我是這樣的做法:

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

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

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

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

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

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

相關(guān)文章

最新評(píng)論