How do I run an application with the specified language in Android Studio?

I am developing a multilingual android application.

I want to run an application from the Android Studio Run button with the specified language. Is it possible?

I expect any option (like application language in Xcode launch options)

Why do I need it?

I want to check the location and word in the app, but launching the Settings app is annoying. So I am looking for an easy way to change the device language settings or Android Studio app options that can specify the language for each launch.

Thank.

+3


source to share


2 answers


It's actually very simple. You just need to create your own resource directories for your language id strings and android will automatically pick the correct one.

Creating Local Directories and Resource Files To add support for more locales, create additional directories inside res /. Each directory name must adhere to the following format:

-b + [+] For example, values-b + es / contains string resources for locales with es code language. Likewise, mipmap-b + es + ES / contains icons for locales with es language code and ES country code. Android loads the appropriate resources according to the device's language settings at runtime. For more information, see the "Providing Alternative Resources" section.

After you have decided what locales will support, create a resource subdirectories and files. For example:

MyProject / res / values ​​/ strings.xml values-b + es / strings.xml mipmap / country_flag.png mipmap-b + es + es / country_flag.png For example, the following are some resource files for different languages:

English strings (default language), / values ​​/ strings.xml:

    Hello World! Spanish strings (es locale), / values-es / strings.xml:

    ¡Hola Mundo! United States flag icon (standard locale), / mipmap / country_flag.png:

United States flag icon Figure 2. Icon used for the default (en_US) locale

Spain flag icon (es_ES locale), / mipmap-b + es + ES / country_flag.png:

Spain flag icon Figure 3. Icon used for es_ES locale

Note. You can use the locale qualifier (or any qualifier configuration) for any type of resource, for example if you want to provide localized versions of your bitmap. For more information, see Localization.

Use resources in your application

As in Android developer page: https://developer.android.com/training/basics/supporting-devices/languages.html

If you don't want Android to automatically select the language of your assets, you can change the language programmatically (not recommended). But you need to change the language for each activity:



 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    String languageToLoad  = "fa"; // your language
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
    getBaseContext().getResources().getDisplayMetrics());
//setContentView...
}

      

Or you install your language via adb if it is for testing only (however via settings it is probably close as fast as this solution):

adb shell "setprop persist.sys.language en; setprop persist.sys.country CA; setprop ctl.restart zygote"

      

-1


source


A common file /values/strings.xml

will be used if the locale does not match one of those present in your application. See these examples for creating locales.

Note that you need to create folders for each locale, for example value-de

German, and then create a strings.xml

file inside.

Common strings.xml

file:

app/src/main/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name" translatable="false">Spin The Bottle</string>
    <string name="text_hint">Tap on screen to spin the bottle</string>
</resources>

      

For Arabic:

app/src/main/res/values-ar/strings.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text_hint">اضغط على الشاشة لتدور الزجاجة</string>
</resources>

      

For German:

app/src/main/res/values-de/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text_hint">Tippen Sie auf den Bildschirm, um die Flasche zu drehen</string>

      

Notes

  • No need to translate everyone <string>

    inside strings.xml

    .
  • Note what I posted translatable="false"

    on app_name

    as I wanted my app name to appear the same for all locales.
  • Also app_name

    not included in any locale other than the public values

    folder.
-1


source







All Articles