Using Locale to force Android to use a specific strings.xml file for a non-supported language

前端 未结 4 1018
甜味超标
甜味超标 2020-12-06 06:00

...and if so, how?

We make a dedicated Android device for use in an industrial environment. It\'s basically a tablet, but with only one app running. The user is n

4条回答
  •  执笔经年
    2020-12-06 06:29

    Will this work? I.e., can we control which strings.xml file it uses via Locale, even if the device has no native support for that language?

    Yes, you can, by updating Locale within Configuration (see an example below). If you try to use the locale for which there are no corresponding resources (either within your app or system), the default string resources (res/values/strings.xml) of your app will be utilized.

    Since Romanian is not a natively-supported language with this device we assume that system messages will still come up in English. Is this true?

    It is true, if English is the current system locale.

    I just want to make sure that if we set the Locale to Romanian, or Czech or some other language without native support it won't crash the tablet if it does try to issue a system message.

    Your app won't crash. Locale changes made within an app effect locale resources of the app, not system one's.

    An example to answer "if so, how?" The method can be used to test locale changes while an Activity is running*.

    public static void changeLocale(Context context, String locale) {
        Resources res = context.getResources();
        Configuration conf = res.getConfiguration();
        conf.locale = new Locale(locale);
        res.updateConfiguration(conf, res.getDisplayMetrics());
    }
    

    * You might want to call recreate() to see string resource changes "on the fly".

提交回复
热议问题