0

I am translating my app to Spanish and Arabic. I converted all the English Strings in the values file to the required language and that worked out very well.

My problem is that the activity names are still in the default language! As you see below, I used String activity1 = getResources().getString(R.string.activity1); I defined the 3 activity names in the values file (res/values-es/string.xml). For example my first activity has the id (activity1). For some reason this still does not work! Any suggestion or/and other reading materials!

package com.reportodor.mohamed;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{

    String displayNames[] = { "Report Odor","Info","Contact Us",};
    Class activities[] = {Report.class, Study.class, Contact.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, displayNames));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String cheese = displayNames[position];
        Class ourClass = activities[position];
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);
        //String activity1 = getResources().getString(R.string.activity1);
        setTitle(getResources().getText(R.string.activity1));
        setTitle("activity1 title"); //


    }
}

I read the following and they were quite helpful: Accessing Resources Localizing with Resources

  • 1
    Check [ResourceBundle](https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html) – sidgate Sep 22 '16 at 20:29

2 Answers2

0

If you have defined the string in a values file, it has an id, and any Activity class can get the translation automatically with this :

String mystring = getResources().getString(R.string.mystring);

Same goes for arrays with getResources().getStringArray(R.string.mystringarray)

See this for more info : Accessing Resources

Edit:

I see what is in fact your problem, the setTitle method works perfectly but you never see the title changed.

That's because when you do startActivity(ourIntent), the current activity is added to back stack and the new one is created.

In this case you must call setTitle from each respective activity : Report , Study , and Contact, and use setTitle(R.string.report_title) etc..

  • Thanks a lot SuigetsuSake for your answer! I was very helpful since now I know where I should go. As I mentioned in the editing above, I did define my id (activity1) in the string.xml (under values-es), but this still does not work! Any suggestions. Thank you very much! – Mohamed Eltarkawe Sep 29 '16 at 18:42
  • In the code you posted, you retrieve the activity title ( activity1 ) but you do nothing with it. You must use setTitle(activity1) or you can set it in xml see http://stackoverflow.com/questions/2198410/how-to-change-title-of-activity-in-android – SuigetsuSake Sep 30 '16 at 08:43
  • @ SuigetsuSake thank you for your patience. As suggested, I tried set Title by itself, then I tried setTitle(getResources().getText(R.string.activity1)); but it didn't work! Is there any way to transfer the string: String displayNames[] = { "Report Odor (Reportar Un Olor)","Info (Información)","Contact Us (Contáctenos)",}; to res/values/strings.xml that way I can translate it? Thanks again and forgive my lack of basic knowledge. – Mohamed Eltarkawe Oct 06 '16 at 00:51
0

It finally worked out. In res/values/arrays: I created

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array
        name="listMenu1">
        <item>@string/activity1</item>
        <item>@string/activity2</item>
        <item>@string/activity3</item>
    </string-array>

</resources>

And in res/values-es/strings called the spanish translation...

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="activity1"> Reportar Un Olor </string>
     <string name="activity2"> Información </string>
     <string name="activity3"> Contáctenos </string>                             
</resources> 

Then the array was retrieved in the OnCreate method as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    String[] listM = getResources().getStringArray(R.array.listMenu1);
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, listM));
}

I greatly appreciate the guidance and directions from SuigetsuSake and sidgate.