如何在Java中使用不受支持的语言环境 [英] How to use unsupported Locale in Java

查看:70
本文介绍了如何在Java中使用不受支持的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为国际化的一部分,要求支持少数国家喜欢

As part of Internationalisation, got a requirement to support few countries like

安提瓜和巴布达-ISO3166代码-AG&多米尼加共和国-ISO3166代码-DO

Antigua and Barbuda - ISO3166 code - AG & Dominican Republic - ISO3166 code - DO

    Locale loc = new Locale("en", "AG");
    DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, loc); 
    System.out.println("Short format: " + df1.format(new Date()));

Java将以mm/dd/yy格式显示日期,而在那些国家/地区中,日期格式为dd/mm/yy.

Java will display date in format mm/dd/yy, where as date format in those countries are dd/mm/yy.

有什么方法可以实现dd/mm/yy模式?甚至ICU4J图书馆也不支持这些国家?

Is there any way we can achieve the pattern dd/mm/yy? Even ICU4J libraries also not supporting those countries?

预先感谢

推荐答案

您可以通过基于适当的服务提供商接口(SPI)创建扩展来向Java运行时添加其他语言环境.

You can add additional locales to the Java runtime by creating an extension based on the appropriate service provider interface (SPI).

例如,如果您想为安提瓜和巴布达(en_AG)指定短日期格式,则可以实现 java.text.spi.DateFormatProvider SPI,如下所示:

For example, if you wanted to specify the short date format for Antigua and Barbuda (en_AG), you can implement the java.text.spi.DateFormatProvider SPI as follows:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.util.Locale;

public class EnAgDateFormatProvider extends DateFormatProvider {
    private static final Locale EN_AG_LOCALE = new Locale("en", "AG");

    public DateFormat getDateInstance(int style, Locale locale) {
        // if your extension supports multiple locales, you have to take the locale
        // parameter into account as well
        switch (style) {
            case DateFormat.SHORT:
                return new SimpleDateFormat("dd/MM/yy");

            default:
                // TODO implement other styles
                return null;
        }
    }

    public DateFormat getTimeInstance(int style, Locale locale) {
        // TODO implement this method
        return null;
    }

    public DateFormat getDateTimeInstance(int dateStyle, int timeStyle,
            Locale locale) {

        // TODO implement this method
        return null;
    }

    public Locale[] getAvailableLocales() {
        return new Locale[]{EN_AG_LOCALE};
    }
}

这将需要打包在一个JAR文件中,并且在JAR的 META-INF/services 目录中,您将需要创建一个名为 java.text.spi的文件.DateFormatProvider .该文件需要包含您的提供者的全限定名称,在我的情况下就是:

This will need to be packaged in a JAR file, and in the META-INF/services directory of the JAR, you will need to create a file named java.text.spi.DateFormatProvider. That file needs to contain the fully qualified name of your provider, in my case just:

EnAgDateFormatProvider

一旦创建了JAR,则需要将其拖放到JRE的扩展目录中.在我的Ubuntu计算机上,这恰好是/usr/lib/jvm/java-8-oracle/jre/lib/ext/.

Once you have created the JAR, you need to drop it in the extensions directory of your JRE. On my Ubuntu machine this happens to be /usr/lib/jvm/java-8-oracle/jre/lib/ext/.

之后,请输入您的问题的代码段:

After that, the code snippet from your question:

Locale loc = new Locale("en", "AG");
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, loc); 
System.out.println("Short format: " + df1.format(new Date()));

将打印出:

Short format: 16/02/2017


参考文献:


References:

这篇关于如何在Java中使用不受支持的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆