Mixalis
Mixalis

Reputation: 73

Adding new locales and forcing a locale in an android app (localization)

I try to learn how to program in android studio and i am currently in the second tutorial about how to change language by localizing. When i try to create a second strings.xml file inside the folder values_el it say me i cant cause names should be unique. I try to copy the original strings.xml file from values folder to the new values_el folder, i translate the messages but nothing happens. Also i try to right click the original strings.xml file and i press the translate option and i translate them from there but nothing happens again. When i run the app in my phone the language is English in both of the ways i try above. My phone language is Greek but the letters of my program is English.

question 2.

First why the language do not change in my phone? Second is there a way to change the language of my app by pressing a button while i open it? Some games i play from google play have the option to choise your languange while before you start the game. Sorry about my english if you dont understant something i say please let me know so i try to explain it better with google translate help. Thank for your time anyway.

Thats the code i run

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_my"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/edit_message"   />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test"
        android:onClick="testActivity" />

</LinearLayout>

Upvotes: 7

Views: 9161

Answers (3)

Viral Patel
Viral Patel

Reputation: 33408

Why dont you use the easy way through the IDE?

  1. Open your strings.xml file
  2. Click the Open Editor link as seen below

enter image description here

  1. Add the locales you want to add translations for by selecting like this

enter image description here

  1. This will create the right file for you without you needing to worry about the file name

  2. You dont need to open each strings.xml to put the localized strings. Do it right from this strings.xml editor.

Coming to your second question: The local on app will be the locale selected on device in the device settings. If you have not localized for the device locale it will fall back to the main strings.xml under res/values.

To force a locale in your app irrespective of the device's locale:

Add the following method in your activity class and call it in onCreate

private void setLanguageForApp(String language){

    String languageToLoad  = language; //pass the language code as param
    Locale locale;
    if(languageToLoad.equals("not-set")){
        locale = Locale.getDefault();
    }
    else {
        locale = new Locale(languageToLoad);
    }
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}

Upvotes: 11

Andrea Aranguren
Andrea Aranguren

Reputation: 61

I'm not sure if I understand your problem exactly, but you can add a new language by right clicking on res -> new -Android resource directory -> locale and select greece and a region. The directory will be created and add there the file with the translations. If you at this point change the phone's language it should show the app translated.

The second question, i'm not sure if is the proper way but I would change the locale and reload the app or activity. Like this

String languageToLoad  = "fr_FR";
 Locale locale = new Locale(languageToLoad); 
 Locale.setDefault(locale);
 Configuration config = new Configuration();
 config.locale = locale;
 context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());


Intent intent = new Intent(XYZ.this, XYZ.class); 

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

Check this answer: https://stackoverflow.com/a/15971553/1623224

Upvotes: 0

Arpit Ratan
Arpit Ratan

Reputation: 3026

Your folder name is : values_el which is incorrect. It should be values-el

And yes it is possible to change the language for your application. Please refer the below link.

Change language programmatically in Android

Upvotes: 1

Related Questions