Android多语言字符串在POJO类中

huangapple go评论87阅读模式
英文:

android multilingual strings in POJO class

问题

我的应用程序是一个基本的记事本样式应用程序,其中主要活动是一个列表,每个项目都可以进行编辑。每个项目的数据包括项目的名称和一系列文本字段。我目前有一个用于管理项目的类,如下所示:

public class Item implements Serializable {
  private String name;
  private String review;
  public Item() {
    name = Resources.getSystem().getString(R.string.name_movie);
    review = Resources.getSystem().getString(R.string.description_like);
  }
  public Item(String itemname, String itemreview) {
    //... 后面是getter和setter方法
  }
}

然后在 strings.xml 中:

<string name="name_movie">电影</string>
<string name="name_book">书籍</string>
<string name="name_record">记录</string>
<string name="description_like"></string>
<string name="description_love">很棒</string>

等等。它们都被翻译成其他语言,我可以在代码中引用它们,如下所示:

R.string.name_movie
R.string.description_like

问题在于默认构造函数,我希望基于字符串资源分配值。不幸的是,要获取系统资源需要一个上下文。这些是POJO类,没有上下文。此外,主活动将项目的ArrayList保存到文件中,因此我已将项目序列化。因为它们是可序列化的,所以我需要一个无参数构造函数,所以我不能在构造函数中使用上下文的静态成员变量,如下所示:

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

我的问题是,在Android应用程序中,如何最好地访问可序列化的普通Java对象中的多语言字符串?我已经查看了以下讨论,还有其他讨论:https://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activityhttps://stackoverflow.com/questions/7213924/access-string-xml-resource-file-from-java-android-codehttps://stackoverflow.com/questions/2002288/static-way-to-get-context-in-android

还有哪些其他方法可以在这些对象中使用本地化字符串?我考虑过将资源标识符而不是字符串存储在我的项目中,但我想保持开放性,以修改我的应用程序以接受用户输入的字符串,而不仅仅是从列表中选择。

英文:

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>

&lt;string name=&quot;name_movie&quot;&gt;movie&lt;/string&gt;
&lt;string name=&quot;name_movie&quot;&gt;book&lt;/string&gt;
&lt;string name=&quot;name_movie&quot;&gt;record&lt;/string&gt;
&lt;string name=&quot;description_like&quot;&gt;good&lt;/string&gt;
&lt;string name=&quot;description_love&quot;&gt;great&lt;/string&gt;

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: https://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activity, https://stackoverflow.com/questions/7213924/access-string-xml-resource-file-from-java-android-code, and https://stackoverflow.com/questions/2002288/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.

答案1

得分: 1

使用Application类。
扩展Android Application类,添加一个静态变量Context,并在onCreate方法中将应用程序上下文分配给它。
添加一个方法getContact,返回您的应用程序上下文。
在您的POJO内部,您可以通过Application.MyMethod()等方式访问它。
(不要忘记将您的应用程序类分配给您的清单文件。)

英文:

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.

答案2

得分: 0

解决方案是按照Teddy Smith的建议使用单例类,然后在我的翻译中为每种语言都创建一个字符串。例如,

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

然后在翻译编辑器中,将语言字符串定义为"sp"表示西班牙语等等。然后,当我获取字符串资源"language"的值时,可以确定我的应用程序正在哪种语言下运行。

这样做的目的是创建一个多语言音频提示应用程序,其中包含针对用户语言的特定音频提示列表。

感谢您的帮助。

英文:

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,

 &lt;string name=&quot;language&quot;&gt;en&lt;/string&gt;

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

huangapple
  • 本文由 发表于 2020年7月30日 02:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63159955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定