How to translate the Android application?

13

I created the values-es folder, with the file strings.xml with items in the string array.

But when I put the translation into Spanish on the phone, I open the application and nothing happens, continues Portuguese.

    
asked by anonymous 29.11.2014 / 19:12

1 answer

19

To make your application work with multiple languages, you must create values folders in your project according to the language and region of interest, languages are identified by two characters, ISO 639-1 and regions are also identified by then characters, preceded by the letter "r" , according to ISO 3166-1-alpha-2 , regions are not required.

For example:

/
/res/
/res/values-pt
/res/values-pt-rBR
/res/values-en
/res/values-en-rUS
Within each values folder, there should be a file called strings.xml , which contains a unique key (within the same file) and the value corresponding to the language of the folder.

I'll leave a more complete example that might help you:

Default values (% with%)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="nome_aplicacao" translatable="false">Multi-idioma</string>
    <string name="opcao1">Padrão</string>
    <string name="opcao2">Inglês</string>
    <string name="opcao3">Espanhol</string>
    <string name="mensagem">Um teste</string>
</resources>

Values in English ( /res/values/strings.xml )

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="opcao1">Default</string>
    <string name="opcao2">English</string>
    <string name="opcao3">Spanish</string>
    <string name="mensagem">A test</string>
</resources>

Values in Spanish ( /res/values-en/strings.xml )

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="opcao1">Estándar</string>
    <string name="opcao2">Inglés</string>
    <string name="opcao3">Español</string>
    <string name="mensagem">Una prueba</string>
</resources>

After defining the values, we use the keys in the layout to display in the components (in the /res/values-es/strings.xml property) ( text )

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mensagem" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/opcao1"
        android:id="@+id/btnOpcao1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/opcao2"
        android:id="@+id/btnOpcao2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/opcao3"
        android:id="@+id/btnOpcao3" />

</LinearLayout>

By doing this your application will be displayed with the same system language, if there is no values folder that matches the selected language, the default ).

If you want to use a different language in the application than the system, you can do the following:

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Locale;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        Button btnOpcao1 = (Button) findViewById(R.id.btnOpcao1);
        Button btnOpcao2 = (Button) findViewById(R.id.btnOpcao2);
        Button btnOpcao3 = (Button) findViewById(R.id.btnOpcao3);

        btnOpcao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLocale("pt");
            }
        });

        btnOpcao2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLocale("en");
            }
        });

        btnOpcao3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLocale("es");
            }
        });

    }

    private void setLocale(String localeName) {
        Locale locale = new Locale(localeName);
        Locale.setDefault(locale);

        Configuration config = getResources().getConfiguration();
        config.locale = locale;
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    }

}

The /res/layout/layout.xml method defines the locale in your application settings. For the language to be changed, you must restart the application. You can manually retrieve the key values in this way:

String nomeAplicacao = getResources().getString(R.string.nome_aplicacao);

If the user changes the system language often, the application settings will be reset with each change, a work around is using SharedPreferences , here is a simple example of its use:

private void setPreferenceLocale(String localeName) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
    prefsEditor.putString("locale", localeName); // "locale" será a sua chave
    prefsEditor.commit();
}

private String getPreferenceLocale() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    // pega o valor da sua chave, caso ela não exista, retorna "pt_BR"
    return sharedPreferences.getString("locale", "pt_BR");
}

References:

Supporting Different Languages
Providing Resources
Change language programatically in Android Android-sharedpreference

Extras:

Configuration
Locale
PreferenceManager

    
15.12.2014 / 03:20