How to use the Class in which the text is stored. For 3 languages? Android

There is a class

public final class Astro{ String A="asdads"; String B="CCACA"; } 

I need to add all the text from this class to the resources, and then just leave links to resources so that when the language changes, the text changes to need me.

I cannot use getResource because this class is static and it does not have a Context .


 public final class Constants { public static final String REGISTER_PAY_SUM = "pay sum"; public static final String CATEGORY_RESPONSE = "category response"; public static final String COURSE_ID = "courseId"; public static final String SUBCATEGORY_ID = "subcategory id"; public static final String JSON_FILE_NAME = "json file name"; public static final String SUBCATEGORY_NAME = "subcategory name"; public static final String CHAPTER = "chapter"; public static final String LESSON_ID = "lesson id"; public static final String QUIZ_ITEM = "quizItem"; public static final String QUIZ_COUNT = "quizCount"; public static final String SEARCH_QUERY = "search query"; public static final String NUM_OF_QUESTION = "numOfQuestion"; public static final String ANSWER_ID = "numOfAnswer"; public static final String USER_TOKEN = "userToken"; } 

  public class BaseApp extends Application { public static Context context; @Override public void onCreate() { super.onCreate(); context=getApplicationContext(); } } 
  • 2
    And why, in fact, do not use id resources (R.string.blabla) - they are constants themselves, do not depend on the language, the context does not require any problems with switc with intas. And to get a line from resources only where the text is really needed - the context is there for sure (any view stores it). - woesss

2 answers 2

I cannot use getResource because this class is static and it does not have a Context .

I will offer two options:

  1. You can always get the ApplicationContext and call the getResources() method from it:

     public class App extends Application { private static Context sContext; @Override public void onCreate() { super.onCreate(); sContext = this; } public static Context getInstance() { return sContext; } } 

    And further somewhere:

     public static void foo() { Resources resources = App.getInstance().getResources(); // ... } 

    In general, Google does not recommend extending the Application class ( unless there are good reasons for this ).

  2. ( this method is generally bad, but if everything is done carefully, then you can ). Pass in the static Context method, but be careful, sending a link to the activation is fraught with memory leaks .

  • 1) "Google does not recommend expanding the Application class" - yes, all right, you can just as well not recommend extending the Activity class, what other reasons are needed 3) There is a third way - through reflection and you don’t need to expand and transmit anything, but it is absolutely " Fu ", first of all, because the android versions need to be monitored all the time, it’s not looking to stop working and updating. - Rou1997
  • one
    @ Rou1997, yes seriously . And this, please read what is written in brackets. - post_zeew
  • one
    @xTIGRx, I think it's not the only one. You can think of many more perversions (and not very perversions). Inheritance Application not fraught with memory leaks, but the transfer of links to the Activity somewhere - is fraught with. Here you will transfer, save, the activity will be destroyed, and the link to it will hang. - post_zeew
  • one
    @ Rou1997, Maybe so, but in the context of that phrase, I understand those words as не расширяйте... так как требуемый функционал можно получить другими способами . - post_zeew
  • one
    @ Rou1997, Yes, you can, but only if you can call getApplicationContext() . - post_zeew

I suggest not to make bicycles, but to use the standard mechanism for supporting several languages. You can read here

Those. you will need to create several strings.xml files for each language, and from the code, refer to the string resource ID and the android will return the string in the desired language

If you want to change the language during the execution of the program, then read here.

  • You obviously did not carefully read what I needed. Updated the question above. Do you see constants? So instead of the text there should be links to strings. So that when I changed strings.xml or strings-ar.xml, then Android automatically grabbed and substituted the necessary text by language. щяс у меня вопрос такой. Как мне сделать чтобы вместо текста там были ссылки типо getString щяс у меня вопрос такой. Как мне сделать чтобы вместо текста там были ссылки типо getString Andro
  • And where will you use this class is access to the context? - mifkamaz