...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:
strings.xml
file it uses via Locale
, even if the device has no native support for that language?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).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.
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.
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.
Will this work? I.e., can we control which
strings.xml
file it uses viaLocale
, 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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With