Android 如何防止我的字符串被翻译成英语以外的其他语言?

Android 如何防止我的字符串被翻译成英语以外的其他语言?,android,localization,simpledateformat,Android,Localization,Simpledateformat,我正试图阻止我的应用程序被自动翻译成其他语言(比如保加利亚语)。我希望我所有的字符串都是英文的。我尝试将时区设置为“欧洲\伦敦”(因为我在英国),但没有完全起作用。当有人在英国以外的国家安装我的应用程序时,有没有办法确保我的应用程序的设置(所有设置)不会被翻译 我在应用程序中使用日期,我使用的是SimpleDateFormatter。我认为这导致了翻译一些字符串的问题。所以我所做的就是在使用它的字符串之前将时区设置为: public static SimpleDateFormat sdf = n

我正试图阻止我的应用程序被自动翻译成其他语言(比如保加利亚语)。我希望我所有的字符串都是英文的。我尝试将时区设置为“欧洲\伦敦”(因为我在英国),但没有完全起作用。当有人在英国以外的国家安装我的应用程序时,有没有办法确保我的应用程序的设置(所有设置)不会被翻译

我在应用程序中使用日期,我使用的是
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中将每个不希望翻译成任何其他语言的字符串的translateable设置为false

    <string name="account_setup_imap" translatable="false">IMAP</string>
IMAP

如果您只想为
SimpleDataFormat
使用特定的
区域设置
,请使用采用
区域设置的构造函数


java.util
的日期时间API及其格式化API
SimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到。无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认

为了坚持使用
English
,您需要使用日期时间格式化/解析API指定
Locale.English
无论如何,最佳实践之一是。

现代API:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
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:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
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
请注意,
java.util.Date
对象不像;相反,它表示从1970年1月1日的
纪元开始的毫秒数。打印
java.util.Date
的对象时,其
toString
方法返回从该毫秒值计算的日期时间。由于
java.util.Date
没有时区信息,因此它应用JVM的时区并显示相同的时区。如果需要在不同的时区中打印日期时间,则需要将时区设置为
SimpleDateFomrat
,并从中获取格式化字符串

相比之下,现代日期时间API有特定的类,只表示日期、时间或日期时间。而且,对于每一个,都有单独的类,用于有时区信息或没有时区信息。查看以下内容,了解现代日期时间类型的概述:

快速演示:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
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
输出:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
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的更多信息。您能否提供一些输出,说明您当前获得的内容和期望值?仅供参考,您使用的日期时间类非常糟糕,多年前被JSR 310中定义的现代java.time类所取代。非常感谢您,Ian。我会测试一下,但从表面上看,我应该做到这一点。谢谢!:)