754
755 private void setCause(Throwable cause) {
756 if (this.cause == null) {
757 this.cause = cause;
758 } else {
759 // Override the cause if the previous one is
760 // ClassNotFoundException.
761 if (this.cause instanceof ClassNotFoundException) {
762 this.cause = cause;
763 }
764 }
765 }
766
767 private Throwable getCause() {
768 return cause;
769 }
770
771 @Override
772 public String toString() {
773 String l = locale.toString();
774 if (l.length() == 0) {
775 if (locale.getVariant().length() != 0) {
776 l = "__" + locale.getVariant();
777 } else {
778 l = "\"\"";
779 }
780 }
781 return "CacheKey[" + name +
782 ", locale=" + l +
783 ", module=" + getModule() +
784 ", callerModule=" + getCallerModule() +
785 ", format=" + format +
786 "]";
787 }
788 }
789
790 /**
791 * The common interface to get a CacheKey in LoaderReference and
792 * BundleReference.
793 */
794 private static interface CacheKeyReference {
795 public CacheKey getCacheKey();
2886 String region = base.getRegion();
2887 String variant = base.getVariant();
2888
2889 // Special handling for Norwegian
2890 boolean isNorwegianBokmal = false;
2891 boolean isNorwegianNynorsk = false;
2892 if (language.equals("no")) {
2893 if (region.equals("NO") && variant.equals("NY")) {
2894 variant = "";
2895 isNorwegianNynorsk = true;
2896 } else {
2897 isNorwegianBokmal = true;
2898 }
2899 }
2900 if (language.equals("nb") || isNorwegianBokmal) {
2901 List<Locale> tmpList = getDefaultList("nb", script, region, variant);
2902 // Insert a locale replacing "nb" with "no" for every list entry
2903 List<Locale> bokmalList = new LinkedList<>();
2904 for (Locale l : tmpList) {
2905 bokmalList.add(l);
2906 if (l.getLanguage().length() == 0) {
2907 break;
2908 }
2909 bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
2910 l.getVariant(), null));
2911 }
2912 return bokmalList;
2913 } else if (language.equals("nn") || isNorwegianNynorsk) {
2914 // Insert no_NO_NY, no_NO, no after nn
2915 List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
2916 int idx = nynorskList.size() - 1;
2917 nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
2918 nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
2919 nynorskList.add(idx++, Locale.getInstance("no", "", ""));
2920 return nynorskList;
2921 }
2922 // Special handling for Chinese
2923 else if (language.equals("zh")) {
2924 if (script.length() == 0 && region.length() > 0) {
2925 // Supply script for users who want to use zh_Hans/zh_Hant
2926 // as bundle names (recommended for Java7+)
2927 switch (region) {
2928 case "TW":
2929 case "HK":
2930 case "MO":
2931 script = "Hant";
2932 break;
2933 case "CN":
2934 case "SG":
2935 script = "Hans";
2936 break;
2937 }
2938 }
2939 }
2940
2941 return getDefaultList(language, script, region, variant);
2942 }
2943
2944 private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
2945 List<String> variants = null;
2946
2947 if (variant.length() > 0) {
2948 variants = new LinkedList<>();
2949 int idx = variant.length();
2950 while (idx != -1) {
2951 variants.add(variant.substring(0, idx));
2952 idx = variant.lastIndexOf('_', --idx);
2953 }
2954 }
2955
2956 List<Locale> list = new LinkedList<>();
2957
2958 if (variants != null) {
2959 for (String v : variants) {
2960 list.add(Locale.getInstance(language, script, region, v, null));
2961 }
2962 }
2963 if (region.length() > 0) {
2964 list.add(Locale.getInstance(language, script, region, "", null));
2965 }
2966 if (script.length() > 0) {
2967 list.add(Locale.getInstance(language, script, "", "", null));
2968 // Special handling for Chinese
2969 if (language.equals("zh")) {
2970 if (region.length() == 0) {
2971 // Supply region(country) for users who still package Chinese
2972 // bundles using old convension.
2973 switch (script) {
2974 case "Hans":
2975 region = "CN";
2976 break;
2977 case "Hant":
2978 region = "TW";
2979 break;
2980 }
2981 }
2982 }
2983
2984 // With script, after truncating variant, region and script,
2985 // start over without script.
2986 if (variants != null) {
2987 for (String v : variants) {
2988 list.add(Locale.getInstance(language, "", region, v, null));
2989 }
2990 }
2991 if (region.length() > 0) {
2992 list.add(Locale.getInstance(language, "", region, "", null));
2993 }
2994 }
2995 if (language.length() > 0) {
2996 list.add(Locale.getInstance(language, "", "", "", null));
2997 }
2998 // Add root locale at the end
2999 list.add(Locale.ROOT);
3000
3001 return list;
3002 }
3003 }
3004
3005 /**
3006 * Returns a <code>Locale</code> to be used as a fallback locale for
3007 * further resource bundle searches by the
3008 * <code>ResourceBundle.getBundle</code> factory method. This method
3009 * is called from the factory method every time when no resulting
3010 * resource bundle has been found for <code>baseName</code> and
3011 * <code>locale</code>, where locale is either the parameter for
3012 * <code>ResourceBundle.getBundle</code> or the previous fallback
3013 * locale returned by this method.
3014 *
3015 * <p>The method returns <code>null</code> if no further fallback
|
754
755 private void setCause(Throwable cause) {
756 if (this.cause == null) {
757 this.cause = cause;
758 } else {
759 // Override the cause if the previous one is
760 // ClassNotFoundException.
761 if (this.cause instanceof ClassNotFoundException) {
762 this.cause = cause;
763 }
764 }
765 }
766
767 private Throwable getCause() {
768 return cause;
769 }
770
771 @Override
772 public String toString() {
773 String l = locale.toString();
774 if (l.isEmpty()) {
775 if (!locale.getVariant().isEmpty()) {
776 l = "__" + locale.getVariant();
777 } else {
778 l = "\"\"";
779 }
780 }
781 return "CacheKey[" + name +
782 ", locale=" + l +
783 ", module=" + getModule() +
784 ", callerModule=" + getCallerModule() +
785 ", format=" + format +
786 "]";
787 }
788 }
789
790 /**
791 * The common interface to get a CacheKey in LoaderReference and
792 * BundleReference.
793 */
794 private static interface CacheKeyReference {
795 public CacheKey getCacheKey();
2886 String region = base.getRegion();
2887 String variant = base.getVariant();
2888
2889 // Special handling for Norwegian
2890 boolean isNorwegianBokmal = false;
2891 boolean isNorwegianNynorsk = false;
2892 if (language.equals("no")) {
2893 if (region.equals("NO") && variant.equals("NY")) {
2894 variant = "";
2895 isNorwegianNynorsk = true;
2896 } else {
2897 isNorwegianBokmal = true;
2898 }
2899 }
2900 if (language.equals("nb") || isNorwegianBokmal) {
2901 List<Locale> tmpList = getDefaultList("nb", script, region, variant);
2902 // Insert a locale replacing "nb" with "no" for every list entry
2903 List<Locale> bokmalList = new LinkedList<>();
2904 for (Locale l : tmpList) {
2905 bokmalList.add(l);
2906 if (l.getLanguage().isEmpty()) {
2907 break;
2908 }
2909 bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
2910 l.getVariant(), null));
2911 }
2912 return bokmalList;
2913 } else if (language.equals("nn") || isNorwegianNynorsk) {
2914 // Insert no_NO_NY, no_NO, no after nn
2915 List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
2916 int idx = nynorskList.size() - 1;
2917 nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
2918 nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
2919 nynorskList.add(idx++, Locale.getInstance("no", "", ""));
2920 return nynorskList;
2921 }
2922 // Special handling for Chinese
2923 else if (language.equals("zh")) {
2924 if (script.isEmpty() && !region.isEmpty()) {
2925 // Supply script for users who want to use zh_Hans/zh_Hant
2926 // as bundle names (recommended for Java7+)
2927 switch (region) {
2928 case "TW":
2929 case "HK":
2930 case "MO":
2931 script = "Hant";
2932 break;
2933 case "CN":
2934 case "SG":
2935 script = "Hans";
2936 break;
2937 }
2938 }
2939 }
2940
2941 return getDefaultList(language, script, region, variant);
2942 }
2943
2944 private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
2945 List<String> variants = null;
2946
2947 if (!variant.isEmpty()) {
2948 variants = new LinkedList<>();
2949 int idx = variant.length();
2950 while (idx != -1) {
2951 variants.add(variant.substring(0, idx));
2952 idx = variant.lastIndexOf('_', --idx);
2953 }
2954 }
2955
2956 List<Locale> list = new LinkedList<>();
2957
2958 if (variants != null) {
2959 for (String v : variants) {
2960 list.add(Locale.getInstance(language, script, region, v, null));
2961 }
2962 }
2963 if (!region.isEmpty()) {
2964 list.add(Locale.getInstance(language, script, region, "", null));
2965 }
2966 if (!script.isEmpty()) {
2967 list.add(Locale.getInstance(language, script, "", "", null));
2968 // Special handling for Chinese
2969 if (language.equals("zh")) {
2970 if (region.isEmpty()) {
2971 // Supply region(country) for users who still package Chinese
2972 // bundles using old convension.
2973 switch (script) {
2974 case "Hans":
2975 region = "CN";
2976 break;
2977 case "Hant":
2978 region = "TW";
2979 break;
2980 }
2981 }
2982 }
2983
2984 // With script, after truncating variant, region and script,
2985 // start over without script.
2986 if (variants != null) {
2987 for (String v : variants) {
2988 list.add(Locale.getInstance(language, "", region, v, null));
2989 }
2990 }
2991 if (!region.isEmpty()) {
2992 list.add(Locale.getInstance(language, "", region, "", null));
2993 }
2994 }
2995 if (!language.isEmpty()) {
2996 list.add(Locale.getInstance(language, "", "", "", null));
2997 }
2998 // Add root locale at the end
2999 list.add(Locale.ROOT);
3000
3001 return list;
3002 }
3003 }
3004
3005 /**
3006 * Returns a <code>Locale</code> to be used as a fallback locale for
3007 * further resource bundle searches by the
3008 * <code>ResourceBundle.getBundle</code> factory method. This method
3009 * is called from the factory method every time when no resulting
3010 * resource bundle has been found for <code>baseName</code> and
3011 * <code>locale</code>, where locale is either the parameter for
3012 * <code>ResourceBundle.getBundle</code> or the previous fallback
3013 * locale returned by this method.
3014 *
3015 * <p>The method returns <code>null</code> if no further fallback
|