Android gets the specified language string

Because the recent application has strengthened the adaptation of multiple languages, there are now n languages (more than 10) in the application, and then the statistics show that the product only needs to upload English strings. Therefore, for the data in the application, the uploaded strings should be unified into English without modifying the language

Don 't say too much bullshit, just attach the code

 * Created by panruijie on 2017/12/26.
 * Email :
public class LanguageUtil {

    public static final String TAG = "LanguageUtil";
    private static final String APPNAME = "you app packagename";
    public static final String ERROR_LABEL = "";
    private static final String DEFAULT_COUNTRY = "US";
    private static final String DEFAULT_LANGUAGE = "en";

    public static String getStringByLocale(Context context, int stringId, String language, String country) {
        /*for (Locale locale : getAvailableLocales()) {

            String lang = locale.getLanguage();
            String coun = getCountryPrivate(locale);
            String localeStr = country.equals("") ? language : (language + "_" + country);

            Loger.w("ruijie", "language = " + lang + ", country = " + coun + "," +
                    "localeStr = " + localeStr);
        Resources resources = getApplicationResource(context.getApplicationContext().getPackageManager(),
                APPNAME, new Locale(language, country));
        if (resources == null) {
            return ERROR_LABEL;
        } else {
            try {
                return resources.getString(stringId);
            } catch (Exception e) {
                return ERROR_LABEL;

    public static String getStringToEnglish(Context context, int stringId) {
        return getStringByLocale(context, stringId, DEFAULT_LANGUAGE, DEFAULT_COUNTRY);

    private static Resources getApplicationResource(PackageManager pm, String pkgName, Locale l) {
        Resources resourceForApplication = null;
        try {
            resourceForApplication = pm.getResourcesForApplication(pkgName);
            updateResource(resourceForApplication, l);
        } catch (PackageManager.NameNotFoundException e) {

        return resourceForApplication;

    private static void updateResource(Resources resource, Locale l) {
        Configuration config = resource.getConfiguration();
        config.locale = l;
        resource.updateConfiguration(config, null);


The idea is to find the resources of the application through the application name. Without modifying the resources, the current application language will not be affected and the resources will be obtained. Similarly, as long as the package name of other applications is known, all strings can be obtained

Later in the test, a string id was set for a bean, such as R.string.appname:
Bean bean = new Bean();
After setting like this, an error will be found. ResourceNotFoundException will be thrown, because id acquisition for string or picture drawable requires the following methods:

int stringId = context.getResources().getIdentifier(resourceId, "srting", "your package name");
int drawableId = context.getResources().getIdentifier(resourceId, "drawable", "your package name");

In this way, we can easily get the string through this method. The usage is as follows:
String appName = LanguageUtil.getStringToEnglish(context, stringId);

If you want to get the string of the specified country and language, similarly, add the country and language:
String appName = LanguageUtil.getStringByLocale(context, stringId, "en", "US");

Posted on Sun, 03 May 2020 20:43:01 -0400 by hypuk