【问题标题】:Android strings.xml apostrophe, date, and string crash [duplicate]Android strings.xml 撇号、日期和字符串崩溃 [重复]
【发布时间】:2020-12-17 19:19:54
【问题描述】:

我正在 Android Studio 中进行一些翻译,但遇到了错误。

我有一个句子和这种格式的日期。
\'Estimated Time: \'h:mm a
这很好用。

但是,翻译成法语会在 d'atttente 一词中添加一个撇号。
\'Temps d\'attente estimé: \'h:mm a

这最终会提前终止字符串文字,并且我的日期格式化程序崩溃。

有没有一种方法可以在我的 strings.xml 文件中使用带撇号的句子和日期格式?

提前致谢!

编辑:

这是来自 logcat 的信息,

java.lang.IllegalArgumentException: Unknown pattern letter: t
    at org.threeten.bp.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1470)
    at org.threeten.bp.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1382)
    at org.threeten.bp.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:788)
    at com.brunswickbowling.sync.passport.extensions.ContextExtKt.getDateTimeFormatter(ContextExt.kt:41)
    at com.brunswickbowling.sync.passport.extensions.TextViewExtKt.setText(TextViewExt.kt:98)

不知道为什么我得到Unknown pattern letter: t,但是当我删除 d'attente 中的撇号时,它工作正常。可能是什么

这里是 XML 格式,

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
  <string name="format_waitlist_eta">\'Temps d\'attente estimé: \'h:mm a</string>
</resources>

编辑和回答:

原来需要第四个\' 才能不提前终止字符串。所以字符串最终看起来像\'Temps d\'\'attente estimé: \'h:mm a

【问题讨论】:

  • 您说您的日期格式化程序崩溃了。请更新您的问题并添加来自 logcat 的错误文本和堆栈跟踪。此外,发生错误的代码行也会对您有所帮助。
  • 您究竟是如何用 XML 编写导致字符串提前终止的?
  • @RyanM 我已更新问题以包含 xml。感谢您的帮助。
  • @Vadik,我添加了一些 logcat 信息。也许是由于那个撇号之后的日期格式,因为我收到了那个错误?有什么阻碍,但我不确定是什么。

标签: android xml string localization


【解决方案1】:

您总是可以在 xml 中使用禁止文字的替代品尝试使用 & apos;替换撇号和 \ 忽略特殊字符,这样你的字符串就会变成

\'Temps d\&apos; attente estimé: \'h:mm a

【讨论】:

  • 谢谢 Bilal,我试过了,但还是没有运气。奇怪的是我在崩溃日志中收到错误“未知模式字母:t”。当我删除撇号虽然它工作正常。
猜你喜欢
  • 2016-09-20
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2016-10-07
相关资源
最近更新 更多