1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: XResourceBundle.java,v 1.7 2004/02/17 04:22:15 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils.res;
  20. import java.util.ListResourceBundle;
  21. import java.util.Locale;
  22. import java.util.MissingResourceException;
  23. import java.util.ResourceBundle;
  24. /**
  25. * The default (english) resource bundle.
  26. * @xsl.usage internal
  27. */
  28. public class XResourceBundle extends ListResourceBundle
  29. {
  30. /** Error resource constants */
  31. public static final String ERROR_RESOURCES =
  32. "com.sun.org.apache.xalan.internal.res.XSLTErrorResources", XSLT_RESOURCE =
  33. "com.sun.org.apache.xml.internal.utils.res.XResourceBundle", LANG_BUNDLE_NAME =
  34. "com.sun.org.apache.xml.internal.utils.res.XResources", MULT_ORDER =
  35. "multiplierOrder", MULT_PRECEDES = "precedes", MULT_FOLLOWS =
  36. "follows", LANG_ORIENTATION = "orientation", LANG_RIGHTTOLEFT =
  37. "rightToLeft", LANG_LEFTTORIGHT = "leftToRight", LANG_NUMBERING =
  38. "numbering", LANG_ADDITIVE = "additive", LANG_MULT_ADD =
  39. "multiplicative-additive", LANG_MULTIPLIER =
  40. "multiplier", LANG_MULTIPLIER_CHAR =
  41. "multiplierChar", LANG_NUMBERGROUPS = "numberGroups", LANG_NUM_TABLES =
  42. "tables", LANG_ALPHABET = "alphabet", LANG_TRAD_ALPHABET = "tradAlphabet";
  43. /**
  44. * Return a named ResourceBundle for a particular locale. This method mimics the behavior
  45. * of ResourceBundle.getBundle().
  46. *
  47. * @param className Name of local-specific subclass.
  48. * @param locale the locale to prefer when searching for the bundle
  49. */
  50. public static final XResourceBundle loadResourceBundle(
  51. String className, Locale locale) throws MissingResourceException
  52. {
  53. String suffix = getResourceSuffix(locale);
  54. //System.out.println("resource " + className + suffix);
  55. try
  56. {
  57. // first try with the given locale
  58. String resourceName = className + suffix;
  59. return (XResourceBundle) ResourceBundle.getBundle(resourceName, locale);
  60. }
  61. catch (MissingResourceException e)
  62. {
  63. try // try to fall back to en_US if we can't load
  64. {
  65. // Since we can't find the localized property file,
  66. // fall back to en_US.
  67. return (XResourceBundle) ResourceBundle.getBundle(
  68. XSLT_RESOURCE, new Locale("en", "US"));
  69. }
  70. catch (MissingResourceException e2)
  71. {
  72. // Now we are really in trouble.
  73. // very bad, definitely very bad...not going to get very far
  74. throw new MissingResourceException(
  75. "Could not load any resource bundles.", className, "");
  76. }
  77. }
  78. }
  79. /**
  80. * Return the resource file suffic for the indicated locale
  81. * For most locales, this will be based the language code. However
  82. * for Chinese, we do distinguish between Taiwan and PRC
  83. *
  84. * @param locale the locale
  85. * @return an String suffix which canbe appended to a resource name
  86. */
  87. private static final String getResourceSuffix(Locale locale)
  88. {
  89. String lang = locale.getLanguage();
  90. String country = locale.getCountry();
  91. String variant = locale.getVariant();
  92. String suffix = "_" + locale.getLanguage();
  93. if (lang.equals("zh"))
  94. suffix += "_" + country;
  95. if (country.equals("JP"))
  96. suffix += "_" + country + "_" + variant;
  97. return suffix;
  98. }
  99. /**
  100. * Get the association list.
  101. *
  102. * @return The association list.
  103. */
  104. public Object[][] getContents()
  105. {
  106. return contents;
  107. }
  108. /** The association list. */
  109. static final Object[][] contents =
  110. {
  111. { "ui_language", "en" }, { "help_language", "en" }, { "language", "en" },
  112. { "alphabet",
  113. new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  114. 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  115. 'Y', 'Z' } },
  116. { "tradAlphabet",
  117. new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  118. 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  119. 'Y', 'Z' } },
  120. //language orientation
  121. { "orientation", "LeftToRight" },
  122. //language numbering
  123. { "numbering", "additive" },
  124. };
  125. }