【问题标题】:Get string for translation获取要翻译的字符串
【发布时间】:2017-01-26 13:49:03
【问题描述】:

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

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());
    }
}

我希望“今天”是一个字符串,所以我在 strings.xml 中有它

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

我还有 es\strings.xml

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

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

【问题讨论】:

  • “但它不起作用”定义不起作用。
  • 它说“无法解析符号”

标签: android string translate


【解决方案1】:

至于 android 中的很多东西,你需要一个Context。通常是您的 Activity 或 Service,或包含您的 Fragment 的 Activity。

定义期望上下文参数的方法:

public static String formatForUI(Context context, Calendar today, Calendar date) {

然后使用上下文:

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

(或context.getResources().getString(R.string.today),使用资源可以访问字符串之外的其他内容)

【讨论】:

  • 或者只是context.getString()
【解决方案2】:

这是我的解决方案:

  • 清理并构建您的 android 项目应该可以工作。
  • 如果不行,尝试重启android studio。
  • 如果仍有错误,请输入getcontext().getResources().getstring(int id)

【讨论】:

  • 最后一个项目符号没有任何意义。请编辑它。
  • 嗨@Code-Apprentice 感谢您的评论。我删除了最后一个项目符号,因为它没有回答问题。
猜你喜欢
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多