Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 214
Mixalis Avatar asked Jun 10 '16 11:06

Mixalis


People also ask

How can I add locale language in Android?

Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have to choose qualifiers as Locale from the available list and select the language as Hindi from the drop-down list.

How do I change localization in Android?

You'll find this screen either in the System Settings app: Languages, or System Settings: System: Languages and input. The Language preference screen should contain one entry called “English (Europe)”. Click Add language and add a fallback language.

How do I change the default locale in Android?

Configuration config = new Configuration(); config. locale = selectedLocale; // set accordingly // eg. if Hindi then selectedLocale = new Locale("hi"); Locale. setDefault(selectedLocale); // has no effect Resources res = getApplicationContext().


2 Answers

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());
}
like image 125
AndroidMechanic - Viral Patel Avatar answered Nov 14 '22 23:11

AndroidMechanic - Viral Patel


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

like image 44
Arpit Ratan Avatar answered Nov 14 '22 23:11

Arpit Ratan