java.util.Calendar 的非标准语言环境

发布于 2024-10-08 17:01:50 字数 1858 浏览 3 评论 0原文

我们有一位瑞典客户,使用英文版软件。所以我们设置Locale(en, SV)。我们确实希望 Calendar 类遵循国家/地区设置,但它使用该语言,并且在此区域设置中它假定美国设置。

因此,我现在正在寻找一种方法,让日历了解新的 firstDayOfWeekminimumDayinFirstWeek 设置,这是标准方法首选的设置,而不是手动设置并因此硬编码。

澄清一下:2010 年 8 月 29 日在瑞典属于 CW 34(德国和英国也有),但在美国则被报告为 CW 36。这与 01.01.2010 是星期五和 29.08 的结果不同。 .2010年的一个星期日。

我无法将语言设置本身更改为瑞典语并使用英语后备,因为我们不支持瑞典语作为语言,但 Sun/Oracle/.. 支持,因此 Swing UI 将混合瑞典语和英语文本,这是不可接受的。

仅添加名为“sun.util.resources.CalendarData_en_SV.properties”的属性文件是行不通的:它不会被读取!手动作为 ResourceBundle 是可能的。不知何故,LocaleData.getCalendarData(Locale) 在读取资源文件方面发挥了自己的魔力,但我无法找到这些资源文件,因为它的源不可用。该方法在此处调用:java.util.Calendar.setWeekCountData(Locale)

我还找到了 java.util.spi 包,但它不提供对firstDayOfWeek 和minimumDaysInFirstWeek 设置的访问。

也许我可以尝试拦截对资源包的调用并使用默认的英语回退,只让对 CalendarData 的调用继续进行!?但这听起来很老套。

package de.drews.i18n;

import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
       // en_GB = 34
       // en_US = 36
       // sv_SV = 34
       // en_SV = 36 --> wrong

       printTest("en", "GB", 34);
       printTest("en", "US", 36);
       printTest("sv", "SV", 34);
       printTest("en", "SV", 34);
    }

    private static void printTest(String language, String country, int expected) {
       Locale locale = new Locale(language, country);

       Calendar cal = Calendar.getInstance(locale);

       cal.set(Calendar.YEAR, 2010);
       cal.set(Calendar.MONTH, Calendar.AUGUST);
       cal.set(Calendar.DATE, 29);

       int actual = cal.get(Calendar.WEEK_OF_YEAR);

       System.out.println(actual + "\t" + expected + "\t"
        + ((actual == expected) ? "Yeah!" : "-") + "\t" + language
        + "\t" + country);
    }
}

We have a customer in Sweden, using the software in English. So we set the Locale(en, SV). We did hope the Calendar class would adhere to the country settings, but it uses the language and with this locale it assumes US settings.

So I am now searching for a way to let the calendar get to know the new firstDayOfWeek and minimumDayinFirstWeek settings preferred by a standard way other than setting it manually and thus hardcoded.

For clarification: The 29. August 2010 is in Sweden in CW 34 (also in Germany and Great Britain) but in the US it is reported as CW 36. The different results from the fact that the 01.01.2010 is a Friday and the 29.08.2010 a Sunday.

I cannot change the language setting itself to Swedish and use the English fallback since we do not support Swedish as language, but Sun/Oracle/.. does, so the Swing UI would have a mixture of Swedish and English texts, which not acceptable.

And just adding a properties file named "sun.util.resources.CalendarData_en_SV.properties" does not work out: it does not get read! Manually as a ResourceBundle that's possible. Somehow LocaleData.getCalendarData(Locale) does its own magic in reading the resourcfiles which i cannot find out since the source of it is not available. The method is called here: java.util.Calendar.setWeekCountData(Locale).

I also found the java.util.spi package but it does not provide access to the firstDayOfWeek and minimumDaysInFirstWeek settings.

Perhaps I can try to intercept the calls to the resourcebundles and use the default fallback to English and only let the calls to CalendarData proceed!? But that sounds hacky.

package de.drews.i18n;

import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
       // en_GB = 34
       // en_US = 36
       // sv_SV = 34
       // en_SV = 36 --> wrong

       printTest("en", "GB", 34);
       printTest("en", "US", 36);
       printTest("sv", "SV", 34);
       printTest("en", "SV", 34);
    }

    private static void printTest(String language, String country, int expected) {
       Locale locale = new Locale(language, country);

       Calendar cal = Calendar.getInstance(locale);

       cal.set(Calendar.YEAR, 2010);
       cal.set(Calendar.MONTH, Calendar.AUGUST);
       cal.set(Calendar.DATE, 29);

       int actual = cal.get(Calendar.WEEK_OF_YEAR);

       System.out.println(actual + "\t" + expected + "\t"
        + ((actual == expected) ? "Yeah!" : "-") + "\t" + language
        + "\t" + country);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-10-15 17:01:50

如何使用 getInstance(TimeZone zone, Locale aLocale) 提供时区来选择日历行为和区域设置来定义语言?

How about using getInstance(TimeZone zone, Locale aLocale) providing a timezone to select calendar behaviour and locale to define language?

只等公子 2024-10-15 17:01:50

我可以提供的一个丑陋的解决方法是反射性地获取 Calendar 类的 cachedLocaleData 静态字段,并在其中添加以下内容:

key = new Locale(..); // your english-swedish locale
value = new int[] {firstDayOfWeek, minimalDaysInFirstWeek};

这可以在初始化时完成,并且将为整个应用程序工作

One ugly workaround that I can offer is to reflectively obtain the cachedLocaleData static field of the Calendar class, and put there the following:

key = new Locale(..); // your english-swedish locale
value = new int[] {firstDayOfWeek, minimalDaysInFirstWeek};

This can be done at init-time and will work for the entire application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文