Android system multi-language settings

The Android system supports multiple national languages ​​by default. To implement language settings for the entire Android system, many people on the Internet use reflection, but it is simpler to implement it through the LocalePicker method provided by the system. This article is based on Android 9, and higher versions have not yet been verified.

Table of contents

1. Permission requirements
2. Implementation by reflection
3. LocalePicker interface implementation
4. Countries Language abbreviation

1. Permission requirements

First, you need system-level permissions and apply for permissions in AndroidManifest.xml:

android:sharedUserId=“android.uid.system”
<!-- 切换语言,修改配置文件 -->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"
        tools:ignore="ProtectedPermissions" />

2. Implementation through reflection

The language can be set through reflection. The reflection method is as follows:

void setLanguage2(Locale setLocaleLauguage) {
    
    
    Log.i(TAG, "setLanguage2: "+setLocaleLauguage);
	try{
    
    
	    Class classActivityManagerNative = Class.forName("android.app.ActivityManagerNative");
    	Method getDefault = classActivityManagerNative.getDeclaredMethod("getDefault");
    	Object objIActivityManager  = getDefault.invoke(classActivityManagerNative);
    	Class classIActivityManager = Class.forName("android.app.IActivityManager");
    	Method getConfiguration = classIActivityManager.getDeclaredMethod("getConfiguration");
    	Configuration config = (Configuration) getConfiguration.invoke(objIActivityManager);
    	config.setLocale(setLocaleLauguage);

    	Class clzConfig = Class.forName("android.content.res.Configuration");
    	Field userSetLocale = clzConfig.getField("userSetLocale");
    	userSetLocale.set(config, true);
    	Class[] clzParams = new Class[]{
    
    Configuration.class};
    	Method updateConfiguration = classIActivityManager.getDeclaredMethod("updateConfiguration", clzParams);
    	updateConfiguration.invoke(objIActivityManager, config);
    	BackupManager.dataChanged("com.android.providers.settings");
	}catch (Exception var12) {
    
    

	}
	}

Based on the above method, the system language setting is implemented, in which the abbreviation of the language needs to be passed in. See the language abbreviations of various countries at the end of the article.

	Locale localeL = Locale.forLanguageTag("hu-HU");
	if(localeL == null) {
    
    
		localeL = new Locale("hu-HU");
	}
	setLanguage2(localeL);

3. LocalePicker interface implementation

You can query the supported languages ​​that have been encapsulated in Locale through the following method:

 final Map<String, LocalePicker.LocaleInfo> mLocaleInfoMap = new HashMap<String, LocalePicker.LocaleInfo>();
 final List<LocalePicker.LocaleInfo> localeInfoList = LocalePicker.getAllAssetLocales(SystemActivity.this, false);

        for (final LocalePicker.LocaleInfo localeInfo : localeInfoList) {
    
    
            final String languageTag = localeInfo.getLocale().toLanguageTag();
            mLocaleInfoMap.put(languageTag, localeInfo);
            Log.d("luyao", "::::"+languageTag);
        }

You can use Locale.forLanguageTag() to obtain the Locale object corresponding to the language, but not all languages ​​can obtain it. If the obtained one is null, you need to create a new Locale object. The key methods to implement language setting are as follows. The most important one is LocalePicker.updateLocale(mNewLocale) to set the system configuration file and modify the system language. The abbreviation of the language needs to be passed in. Please refer to the last chapter of the article for the language abbreviations of various countries.

	void setLanguage(String newlanguage) {
    
    
        Locale localeL = Locale.forLanguageTag(newlanguage);
        if(localeL == null) {
    
    
            localeL = new Locale(newlanguage);
        }
        LocalePicker.updateLocale(localeL);
    }

After setting the system language, you can use the adb command getprop persist.sys.locale to check whether the current system language is set successfully.

mt2712_saic_ec32:/ # getprop persist.sys.locale
hr-HR
mt2712_saic_ec32:/ #

4. Abbreviations of various languages

en English
en-US English (United States)
ar Arabic
ar-AE Arabic ( United Arab Emirates)
ar-BH Arabic (Bahrain)
ar-DZ Arabic (Algeria)
ar-EG Arabic (Egypt)
ar-IQ Arabic (Iraq)
ar-JO Arabic (Jordan)
ar- KW Arabic (Kuwait)
ar-LB Arabic (Lebanon)
ar-LY Arabic (Libya)
ar -MA Arabic (Morocco)
ar-OM Arabic (Oman)
ar-QA Arabic (Qatar)
ar-SA Arabic (Saudi Arabia)
ar-SD Arabic (Sudan)
ar-SY Arabic (Syria)
ar-TN Arabic (Tunisia)
ar-YE Arabic (Yemen)
be Belarusian
be- BY Belarusian (Belarus)
bg Bulgarian
bg-BG Bulgarian (Bulgaria)
ca Catalonia Text
ca-ES Catalan (Spain)
ca-ES_EURO Catalan (Spain, Euro)< a i=27> cs Czech cs-CZ Czech (Czech Republic) da Danish da-DK Denmark text(Denmark) de German de-AT German(Austria) de-AT_EURO German(Austria, Euro) de-CH German (Switzerland) de-DE German (Germany) de-DE_EURO German (Germany, Euro) de-LU German (Luxembourg) de-LU_EURO German (Luxembourg, Euro) el Greek el-GR Greek (Greece) en-AU English (Australia) en-CA English ( Canada) en-GB English (UK) en-IE English (Ireland) en-IE_EURO English (Ireland, Euro) en-NZ English (New Zealand) en-ZA English (South Africa) es Spanish es-BO Spanish (Bolivia) es-AR Spanish (Argentina) es-CL Spanish (Chile) es-DO Spanish (Dominican Republic)< /span> es-PA Spanish (Panama)< /span> fr-CH French (Switzerland) fr-LU French (Luxembourg)< /span> a> mk Macedonian lv-LV Latvian (Leto) (Latvia) lv Latvian (Leto) lt-LT Lithuanian (Lithuania) lt Lithuanian ko-KR Korean (South Korea) ko Korean ja-JP Japanese(Japan) ja Japanese iw-IL Hebrew Text(Israel) iw Hebrew it-IT_EURO Italian (Italy, Euro) it-IT Italian ( Italy) it-CH Italian (Switzerland) it Italian is- IS Icelandic (Iceland) is Icelandic hu-HU Hungarian (Hungary) hu Hungarian hr-HR Croatian (Croatia) hr Croatian fr-LU-EURO French (Luxembourg, Euro) fr-FR-EURO French (France, Euro) fr-FR French (France) fr-CA French (Canada) fi-FI Finnish (Finland)< /span> fr-BE French (Belgium) fr French fi-FI_EURO Finnish (Finland, Euro) fi Finnish et-EE Estonian (Estonia) es-VE Spanish (Venezuela) es-UY Spanish (Uruguay) es-SV Spanish (El Salvador) es-PY Spanish ( Paraguay) es-PR Spanish (Puerto Rico) es-PE Spanish (Peru) et Estonian es-NI Spanish (Nicaragua) es-MX Spanish (Mexico) es-HN Spanish (Honduras) es-GT Spanish (Guatemala) es-ES_EURO Spanish (Spain, Euro) es-ES Spanish (Spain) es-EC Spanish (Ecuador) es-CR Spanish (Costa Rica) es-CO Spanish (Colombia)













































































mk-MK Macedonian (Kingdom of Macedonia)
nl Dutch
nl-BE Dutch (Belgium)
nl-BE-EURO Dutch (Belgium, Euro)
nl-NL Dutch (Netherlands)
nl-NL-EURO Dutch (Netherlands, Euro)
no Norwegian
no-NO Norwegian (Norway)
no-NO_NY Norwegian (Norway, Nynorsk)
pl Polish
pl-PL Polish(Poland)
pt Portuguese
pt-BR Portuguese (Brazil)
pt-PT Portuguese (Portugal)
pt-PT_EURO Portuguese (Portugal, Euro)
ro Romanian
ro-RO Romanian (Romania)
ru Russian
ru-RU Russian ( Russia)
sh Servonis-Croatian
sh-YU Servonis-Croatian (Yugoslavia)
sk Slovakia Text
sk-SK Slovak (Slovakia)
sl Slovenian
sl-SI Slovenian (Slovenia) th Thai sv-SE Swedish (Sweden) sv Swedish sr-YU Serbian (Yugoslavia) sr Serbian sq-AL Albanian (Albania)
sq Albanian tr Turkish tr-TR Turkish (Turkey) uk Ukrainian uk-UA Ukrainian (Ukrainian) zh Chinese zh-CN Chinese (China)












Reprint: https://blog.csdn.net/u012873121/article/details/129934297

Guess you like

Origin blog.csdn.net/gqg_guan/article/details/134134461