1

My application is a basic notepad style application where the main activity is a list and each item can be edited. The data for each item includes the name of the item and then a list of text fields. I currently have a class to manage the items as follows:

public class Item implements Serializeable {
  private String name;
  private String review;
  public Item() {
    name = Resources.getSystem().getString(R.string.name_movie);
    description = Resources.getSystem().getString(R.string.description_like);
 }
  public Item(String itemname, String itemreview) {
//... followed by getters and setters
}

then in <strings.xml>

<string name="name_movie">movie</string>
<string name="name_movie">book</string>
<string name="name_movie">record</string>
<string name="description_like">good</string>
<string name="description_love">great</string>

etc. The are all translated into other languages and I can refer to them in code as

R.string.name_movie
R.string.description_like

etc. I'm summarizing because you get the idea.

The problem is the default constructor where I'd like to assign values based on string resources. Unfortunately, to get system resources requires a context. These are POJO classes and have no context. Additionally, I the main activity holds an arraylist of items into a file so I've made the items serializeable. Because there are serializeable I need a no argument constructor so I can't use a static member variable for the context with a constructor such as follows:

    public Item(Context context) {
      m_context = context;
    }

My question is in an android application what is the best way to access multilingual strings in a plain old java object that is serializeable? I'm reviewed the following discussions, among others: getString Outside of a Context or Activity, Access string.xml Resource File from Java Android Code, and Static way to get 'Context' in Android?.

What other ways are there that I could use localized strings in these objects? I considered storing the resource identifier instead of the string in my item but I want to leave it open to modify my application to take user entered strings rather than just picking from a list.

2 Answers2

1

Using Application class. Extend Android Application class, add a static variable Context and onCreate Method assign application context to it. Add a method get contact Wich return your application context. And inside your pojo, you can have access it by Application.MyMethod() and so. ( Don't forget to assign your application class to your manifest.

  • For some reason I suspected this would be problematic for having a static reference to the context. This post has more information https://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext. However, this seems to work well in my app. Thank you! – Joseph Rand Jul 29 '20 at 19:58
  • For what i know, I never had any problem with this. No leak or so. I use Application mainly to have an access point to my Singleton. Instead of declaration some static member here and there and so lost track of them if you have a big custom lib. I almost never use static member but when I need static access to a Singleton, I store them in my application extend. I think that one of the way to use Application... –  Jul 29 '20 at 20:46
0

The solution is to have singleton class as Teddy Smith suggested and then in my translations I have a string for each language. For example,

 <string name="language">en</string>

Then in translations editor I have the language string defined as sp for Spanish, etc. then when I get the value of the string resource "language" and can determine in which language my app is running.

The purpose of this is to have a multilingual audio prompting application with a defined list of audio prompts specific to the user's language.

thanks for the help