我正在尝试阻止我的应用自动转换为其他语言(例如我的保加利亚语)。我希望我的所有字符串都是英文的。我尝试将时区设置为“欧洲\伦敦”(因为我在英国),但这并不是很有效。当有人在英国以外的地方安装我的应用程序时,有什么方法可以确保我的应用程序的设置(所有这些)都没有被翻译?先感谢您!
我在我的应用中使用日期,而我正在使用SimpleDateFormatter
。我认为这导致了翻译某些字符串的问题。所以我所做的就是在使用它之前设置时区:
public static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
String time = sdf.format(new Date());
mPurchasedDate.setText(day + " " + numDay + " " + mont + " at " + time);
但这也不起作用......
P.S。:我没有在我的应用中添加任何本地化。我只有一个strings.xml
文件夹,其中的字符串是英文的。
答案 0 :(得分:4)
如果您只想对Locale
使用特定的SimpleDateFormat
,请使用带有Locale
的构造函数:new SimpleDateFormat(String, Locale):
public static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.UK);
答案 1 :(得分:2)
java.util
的日期时间 API 及其格式化 API SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API。出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。如果您正在工作对于 Android 项目并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring 和 How to use ThreeTenABP in Android Project。
为了坚持使用 English
,您需要使用日期时间格式/解析 API 指定 Locale.ENGLISH
。 无论如何,最佳做法之一是use Locale
with the formatting/parsing API in every case。
现代 API:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
旧 API:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
请注意,java.util.Date
对象不是像 modern date-time types 那样的真实日期时间对象;相反,它表示从 Epoch of January 1, 1970
开始的毫秒数。当您打印 java.util.Date
的对象时,其 toString
方法返回根据此毫秒值计算的日期时间。由于 java.util.Date
没有时区信息,它应用您的 JVM 的时区并显示相同的时区。如果您需要在不同的时区打印日期时间,则需要将时区设置为 SimpleDateFomrat
并从中获取格式化的字符串。
相比之下,现代日期时间 API 具有仅表示日期、时间或日期时间的特定类。而且,对于每一个,有/没有时区信息的单独类。查看以下 table 以了解现代日期时间类型:
快速演示:
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalTime time = LocalTime.now(ZoneId.of("Europe/London"));
// Print its default format i.e. the value returned by time#toString
System.out.println(time);
// Custom format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm:ss", Locale.ENGLISH);
System.out.println(time.format(dtf));
}
}
输出:
12:39:07.627763
12:39:07
从 Trail: Date Time 了解有关现代日期时间 API 的更多信息。
答案 2 :(得分:1)
在strings.xml中为每个不希望被翻译成任何其他语言的字符串设置可翻译为false
<string name="account_setup_imap" translatable="false">IMAP</string>