Disable localization in Android application
Asked Answered
A

3

6

Is there any way to disable localization in an Android application? It must use only English strings. We have library projects with French localized strings. Some apps that use these libraries must be in English only, some not.

Arbitral answered 13/6, 2012 at 11:27 Comment(0)
C
15

2019 - Gradle-based approach

As pointed out by Nidhin in their answer, this is easier and less error prone now than it was in 2012. In the defaultConfig section of the android section of your build.gradle file, you can set resConfigs to the single language you support. For example:

android {
    defaultConfig {
        resConfigs "en"
    }
}

This isn't just for disabling localization—resConfigs simply tells the build system which resources to keep. It can be set to a single language, or ideally to many, and it can be used to filter resources by something other than language, like screen density, as well. As such, saying "only include the English resources" effectively forces the app to always be in that language.

The programmatic way of loading the resources for a specific language is still sometimes useful, so my 2012 answer is still provided:

2012 - Programmatic approach

Doing the following before calling setContentView() or anything else that loads locale-specific resources should ensure that you always load only the English strings.

Resources res = getApplicationContext().getResources();

Locale locale = new Locale("en");
Locale.setDefault(locale);

Configuration config = new Configuration();
config.locale = locale;

res.updateConfiguration(config, res.getDisplayMetrics());
Caryloncaryn answered 14/6, 2012 at 0:14 Comment(3)
This works but I think https://mcmap.net/q/1586008/-disable-localization-in-android-application is the way to do it correctly throughout the app.Administrative
@Administrative That does look like an easier (and less error-prone) approach for use with the modern Gradle build system. At the time the question was asked and I wrote this answer, Android hadn't moved to Gradle yet (I and some others used ant directly on the command line; many people used Eclipse), and I don't think there was a viable build-system based approach. Thanks for the comment drawing attention to this! I'll upvote that answer and note the Gradle-based approach here.Caryloncaryn
Thanks for updating the answer... :)Administrative
D
9

Add the specific resource config in build.gradle

 defaultConfig {

          resConfigs "en"
}
Dominoes answered 22/1, 2019 at 6:36 Comment(1)
this fix is perfect for my needs! so elegant. thank you!Silvie
G
5

Android's normal behavior is that you define a given language only if you support it. For french this would be values-fr/strings.xml. If you don't want to support french don't include the strings.xml for french and it will fall back to the strings.xml in the values folder

http://developer.android.com/guide/topics/resources/localization.html

If you don't have control over the provided strings or if you want to dynamically set the locales to support you can override the default locale.

http://developer.android.com/reference/java/util/Locale.html#setDefault(java.util.Locale)

EDIT

What I forgot to mention is that you have to update your configuration with the new config

Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Georgetta answered 13/6, 2012 at 11:53 Comment(3)
I've tried calling Locale.setDefault(Locale.US) in the onCreate() methods of the application and the activity - still the French strings are displayedArbitral
onCreate of the application works for me. What version of android are you on, the whole locale business got greatly improved with ICS, if you're running a lower version it might work different.Georgetta
I forgot to mention you have to update the configuration as well, like Darshan mentioned. I added it to my answerGeorgetta

© 2022 - 2024 — McMap. All rights reserved.