如何防止我的字符串被翻译成英语以外的其他语言?
How to prevent my strings from being translated to other languages than English?
我试图阻止我的应用程序自动翻译成其他语言(例如我的保加利亚语)。我希望我所有的字符串都是英文的。我尝试将时区设置为“Europe\London”(因为我在英国),但这并不奏效。当有人在英国以外的国家/地区安装我的应用程序时,有什么方法可以确保我的应用程序设置(所有设置)不会被翻译?
我在我的应用程序中使用日期并且我正在使用 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);
但这也没有用。
PS:我没有在我的应用程序中添加任何本地化。我只有一个 strings.xml
文件夹,里面的字符串都是英文的。
在 strings.xml 中将您不希望翻译成任何其他语言的每个字符串的可翻译设置为 false
<string name="account_setup_imap" translatable="false">IMAP</string>
如果您只想为 SimpleDateFormat
使用特定的 Locale
,请使用采用 Locale
的构造函数:new SimpleDateFormat(String, Locale):
public static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.UK);
java.util
的日期时间 API 及其格式 API、SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and .
为了坚持 English
,您需要使用日期时间 formatting/parsing API 指定 Locale.ENGLISH
。 无论如何,最佳做法之一是 。
现代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
方法 returns 根据此毫秒值计算的日期时间。由于 java.util.Date
没有时区信息,它会应用 JVM 的时区并显示相同的时区。如果您需要在不同时区打印日期时间,您需要将时区设置为 SimpleDateFomrat
并从中获取格式化字符串。
相比之下,现代日期时间 API 具有特定的 类 仅表示日期、时间或日期时间。而且,对于其中的每一个,with/without 时区信息都有单独的 类。查看以下 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
了解有关现代日期时间 API 的更多信息
我试图阻止我的应用程序自动翻译成其他语言(例如我的保加利亚语)。我希望我所有的字符串都是英文的。我尝试将时区设置为“Europe\London”(因为我在英国),但这并不奏效。当有人在英国以外的国家/地区安装我的应用程序时,有什么方法可以确保我的应用程序设置(所有设置)不会被翻译?
我在我的应用程序中使用日期并且我正在使用 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);
但这也没有用。
PS:我没有在我的应用程序中添加任何本地化。我只有一个 strings.xml
文件夹,里面的字符串都是英文的。
在 strings.xml 中将您不希望翻译成任何其他语言的每个字符串的可翻译设置为 false
<string name="account_setup_imap" translatable="false">IMAP</string>
如果您只想为 SimpleDateFormat
使用特定的 Locale
,请使用采用 Locale
的构造函数:new SimpleDateFormat(String, Locale):
public static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.UK);
java.util
的日期时间 API 及其格式 API、SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and
为了坚持 English
,您需要使用日期时间 formatting/parsing API 指定 Locale.ENGLISH
。 无论如何,最佳做法之一是
现代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
方法 returns 根据此毫秒值计算的日期时间。由于 java.util.Date
没有时区信息,它会应用 JVM 的时区并显示相同的时区。如果您需要在不同时区打印日期时间,您需要将时区设置为 SimpleDateFomrat
并从中获取格式化字符串。
相比之下,现代日期时间 API 具有特定的 类 仅表示日期、时间或日期时间。而且,对于其中的每一个,with/without 时区信息都有单独的 类。查看以下 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
了解有关现代日期时间 API 的更多信息