LanguageControl.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.i18n;
  16. import java.io.BufferedInputStream;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.Locale;
  21. import java.util.PropertyResourceBundle;
  22. import java.util.ResourceBundle;
  23. import java.util.ResourceBundle.Control;
  24. /**
  25. *
  26. * @author KenM
  27. */
  28. public class LanguageControl extends Control
  29. {
  30. public static final String LANGUAGES_DIRECTORY = "../languages/";
  31. public static final LanguageControl INSTANCE = new LanguageControl();
  32. /**
  33. * prevent instancing, allows sub-classing
  34. */
  35. protected LanguageControl()
  36. {
  37. }
  38. @Override
  39. public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException
  40. {
  41. if (baseName == null || locale == null || format == null || loader == null)
  42. {
  43. throw new NullPointerException();
  44. }
  45. ResourceBundle bundle = null;
  46. if (format.equals("java.properties"))
  47. {
  48. format = "properties";
  49. String bundleName = toBundleName(baseName, locale);
  50. String resourceName = LANGUAGES_DIRECTORY + toResourceName(bundleName, format);
  51. InputStream stream = new FileInputStream(resourceName);
  52. if (stream != null)
  53. {
  54. BufferedInputStream bis = new BufferedInputStream(stream);
  55. try
  56. {
  57. bundle = new PropertyResourceBundle(bis);
  58. }
  59. finally
  60. {
  61. bis.close();
  62. }
  63. }
  64. }
  65. return bundle;
  66. }
  67. }