2

A month ago I started to learn the basics of Android Studio, and I am making an app at the moment. The original language of the app English, but I also wanted it to be in Dutch. I know how to translate the strings.xml file, but I have a hard time translating the words that are in Java. I tried using all the things listed on here, or anywhere on google, but either my app crashes, or in stead of even the English word, there appears a number. So I am wondering, how can I translate the words (one, two and three) that are in my app, (maybe by using strings.xml?)x

public class view_favourite extends Fragment {

public view_favourite() {
    // Required empty public constructor
}

SQLiteDatabase db;
int [] fav_array=  new int[60];
int updated=0;

private static final int START_LEVEL = 1;
private int mLevel;
private Button mNextLevelButton;
private TextView mLevelTextView;

final String[] numberNames = {

        "First","Second","Third"
} ;

If I needed to upload more, sorry, I do not know how this all works and I have a lot to learn. (I know favourite is spelled wrong :))

  • see https://docs.oracle.com/javase/8/docs/technotes/guides/intl/index.html – Scary Wombat Jan 28 '19 at 01:44
  • 1
    I'm thinking that any text you want to internationalize - anything that needs to appear to the user in English or in Dutch - probably shouldn't be coded as a Java string literal in the first place. Q: Am I mistaken? Look here: https://stackoverflow.com/questions/13538260/ and here: https://developer.android.com/guide/topics/resources/localization – paulsm4 Jan 28 '19 at 01:47
  • Yeah, I tried this as well but it all gave errors. – DaanNetherlands Jan 28 '19 at 01:47
  • Hmm, then I think I should start deleting it all and throw it in somewhere else. I don't like it to be almost perfect, I want it all. Thanks both of you! – DaanNetherlands Jan 28 '19 at 01:53

1 Answers1

0

I hope I understand the question correctly. Here's how to get string from resources in java code. If you have files with translation, the string will be one of them.

public class Test extends Fragment {

void test(){
    String first = getString(getContext(), R.string.first);
    String second = getString(getContext(), R.string.second);
    String third = getString(getContext(), R.string.third);
}

String getStringFromResources(Context context, int stringId){
    return context.getString(stringId);
}
}
Zeon
  • 535
  • 8
  • 23