Android 获取用于翻译的字符串

Android 获取用于翻译的字符串,android,string,translate,Android,String,Translate,我的应用程序是这样工作的: public static String formatForUI(Calendar today, Calendar date) { if (today.get(Calendar.YEAR) == date.get(Calendar.YEAR)) { if (today.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)) { return "Today"

我的应用程序是这样工作的:

public static String formatForUI(Calendar today, Calendar date) {
    if (today.get(Calendar.YEAR) == date.get(Calendar.YEAR)) {
        if (today.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)) {
            return "Today";
        }
        return currentYearDateFormat(date.getTimeInMillis());
    } else {
        return format(date.getTimeInMillis());
    }
}
我希望“Today”是一个字符串,因此我将其保存在strings.xml中

<resources>
<string name="today">Today</string>
</resources>
<resources>
<string name="today">Hoy</string>
</resources>

今天
我还有es\strings.xml

<resources>
<string name="today">Today</string>
</resources>
<resources>
<string name="today">Hoy</string>
</resources>

霍伊

我尝试过使用getResources(),但它不起作用。我该怎么做呢?

对于android中的许多东西,你需要一个
上下文。通常,您的活动或服务,或包含您的片段的活动

定义需要上下文参数的方法:

public static String formatForUI(Context context, Calendar today, Calendar date) {
然后使用上下文:

return context.getString(R.string.today);

(或
context.getResources().getString(R.string.today)
,有了这些资源,您可以访问字符串以外的其他内容)

对于android中的许多内容,您需要
上下文。通常,您的活动或服务,或包含您的片段的活动

定义需要上下文参数的方法:

public static String formatForUI(Context context, Calendar today, Calendar date) {
然后使用上下文:

return context.getString(R.string.today);
(或
context.getResources().getString(R.string.today)
,通过这些资源,您可以访问字符串以外的其他内容)

以下是我的解决方案:

  • 清理并构建您的android项目应该可以工作
  • 如果没有,请尝试重新启动android studio
  • 如果仍然出现错误,请放入
    getcontext().getResources().getstring(int-id)
    • 以下是我的解决方案:

      • 清理并构建您的android项目应该可以工作
      • 如果没有,请尝试重新启动android studio
      • 如果仍然出现错误,请放入
        getcontext().getResources().getstring(int-id)

      “但它不起作用”定义不起作用。它说“无法解析符号”“但它不起作用”定义不起作用。它说“无法解析符号”或只是
      context.getString()
      或只是
      context.getString()
      最后一个项目符号没有任何意义。请编辑它。嗨@代码学徒谢谢评论。我删除了最后一个项目,因为它不能回答问题。最后一个项目没有任何意义。请编辑它。嗨@代码学徒谢谢评论。我删除了最后一个项目符号,因为它不能回答问题。