Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

...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 not expected to access any other features of the device and even the system settings, like WiFi and Time settings are performed through our app instead of through the Android Settings widget. So basically every button and message they see uses our strings.xml file.

Currently all of our customers are satisfied to use the default US-English settings but we will soon have some customers who want local languages and have supplied us with translation files. Currently one of them is Romanian, which is not a language with any native support on this device (a Samsung Galaxy tab 4); another is Czech.

So we want to add strings.xml files in appropriate res folders, for the non-English languages and a dropdown in our app to select which language we're using. Programmatically we think we can use Locale to set which strings.xml file it uses, so for example, if Romanian has been selected from the dropdown we would use Locale to set the tablet into Romanian so all of our app's UI will use the Romanian strings.xml file.

Our settings, including the new dropdown, are inaccessible to customers - they're set at the customer site by a field-service engineer.

Questions:

  1. 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?
  2. 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's not a problem if it does - system messages are rare with our app and the users of our products are trained to contact support if that happens. 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).
like image 644
user316117 Avatar asked Dec 02 '16 23:12

user316117


People also ask

How do I localize a string in Android?

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.

What is the role of strings XML file in an Android application?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

What is locale default Android?

The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched.


1 Answers

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".

like image 74
Onik Avatar answered Oct 13 '22 00:10

Onik