How to translate Android app?

Asked

Viewed 2,918 times

14

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

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

  • Suppose you translate also into English. What do you think will happen on your phone? How will he choose between 3 languages?

  • English would be the standard, and I would create a value-pt, wouldn’t I? but the problem that does not work, for now ta padrao portugues, a pasta values Rais, ta com portugues.

  • 1

    You didn’t answer my questions. Put another way: How does the mobile phone know which language to use when an application has set more than one language?

  • I don’t know, what it’s like?

  • 1

    You have to change the language of your mobile phone from Portuguese to Spanish.

  • Yes I did, as I mentioned in the post, but nothing happens.

Show 2 more comments

1 answer

20


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

For example:

/
/res/
/res/values-pt
/res/values-pt-rBR
/res/values-en
/res/values-en-rUS

Inside each folder values there must be a file called xml strings., that contains a single key (within the same file) and the value corresponding to the language of the folder.

I’ll leave a more complete example that can help you:

Standard values (/res/values/strings.xml)

<?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-en/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-es/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 setting the values, we use the keys in the layout for display in the components (in the property text) (/res/layout/layout.xml)

<?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>

Doing this your application will be displayed with the same system language if there is no folder values which corresponds to the selected language, the default (/res/values/strings.xml).

If you want to use a language in the application other than the system, you can do it as follows:

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 method setLocale set the locale in your app settings. In order for the language to be changed, you need to restart the application. You can retrieve key values manually this way:

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

If the user changes the system language frequently, the application settings will be readjusted with each change, one way around this is by using Sharedpreferences, follows 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

  • @Paulohenriqueneryoliveira His two editions in these answers were rejected. The first you change things that change the sense of the answer, the second edition the changes are irrelevant. Edits should try to improve the content of the post, but without changing the context of the author. See some tips at this link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.